How to merge PDFs into a PDF Portfolio?

前端 未结 2 1969
时光说笑
时光说笑 2020-12-12 03:59

I am looking for the functionality that creates PDF Portfolios:

\"Multiple

The

相关标签:
2条回答
  • 2020-12-12 04:20

    Here is the simple sample to show how we can attach files to a new PDF file:

    using System.Diagnostics;
    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    
    namespace PDFAttachment
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var pdfDoc = new Document(PageSize.A4))
                {
                    var pdfWriter = PdfWriter.GetInstance(pdfDoc, new FileStream("Test.pdf", FileMode.Create));               
                    pdfDoc.Open();
    
                    pdfDoc.Add(new Phrase("Test"));
    
                    var filePath = @"C:\path\logo.png";
                    var fileInfo = new FileInfo(filePath);
                    var pdfDictionary = new PdfDictionary();
                    pdfDictionary.Put(PdfName.MODDATE, new PdfDate(fileInfo.LastWriteTime));
                    var fs = PdfFileSpecification.FileEmbedded(pdfWriter, filePath, fileInfo.Name, null, true, null, pdfDictionary);
                    pdfWriter.AddFileAttachment("desc.", fs);
                }
    
                Process.Start("Test.pdf");
            }
        }
    }
    

    Or to an existing PDF file:

    using System.Diagnostics;
    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    
    namespace PDFAttachment
    {
        class Program
        {
            static void Main(string[] args)
            {
                var reader = new PdfReader("Test.pdf");
                using (var stamper = new PdfStamper(reader, new FileStream("newTest.pdf", FileMode.Create)))
                {
                    var filePath = @"C:\path\logo.png";
                    addAttachment(stamper, filePath, "desc.");
                    stamper.Close();
                }
    
                Process.Start("newTest.pdf");
            }
    
            private static void addAttachment(PdfStamper stamper, string filePath, string description)
            {
                var fileInfo = new FileInfo(filePath);
                var pdfDictionary = new PdfDictionary();
                pdfDictionary.Put(PdfName.MODDATE, new PdfDate(fileInfo.LastWriteTime));
                var pdfWriter = stamper.Writer;
                var fs = PdfFileSpecification.FileEmbedded(pdfWriter, filePath, fileInfo.Name, null, true, null, pdfDictionary);
                stamper.AddFileAttachment(description, fs);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-12 04:27

    The term you're looking for is PDF Portfolios. You can create PDFs like this with iTextSharp. Here are a couple of C# examples from the iText book:

    • Chapter16 - KubrickCollection
    • Chapter16 - KubrickMovies

    If you choose to download the KubrickMovies result file, change the extension to ".pdf". Just noticed it now - will try and fix the error this weekend.

    0 讨论(0)
提交回复
热议问题