Pdf与itext合并

时间:2019-04-02 21:22:31

标签: c# pdf merge itext

我正在尝试创建一个将现有pdf文件合并为一个的桌面应用程序。

我找到了一些代码,可以帮助我进行设计,选择文件并将它们合并,但是我的代码创建了pdf文件,然后将新文件发送到桌面。我需要我的代码来抓取现有的pdf文件并将它们合并在一起,以将这些文件一起创建一个文件,然后将其发送到我的桌面。随附的是我的代码,请让我知道我需要解决的问题。我对C#还是很陌生,我了解基本知识,但是不确定在该特定领域中应该在哪里进行更改以及如何进行更改。

namespace mergePdf
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnMerge_Click(object sender, EventArgs e)
        {
            //Folder that we'll work from
            string workingFolder = 
            Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string pdf1 = Path.Combine(workingFolder, "pdf1.pdf");//PDF 
            with solid red background color
            string pdf2 = Path.Combine(workingFolder, "pdf2.pdf");//PDF 
            with text
            string pdf3 = Path.Combine(workingFolder, "pdf3.pdf"); 
            //Merged PDF

            //Create a basic PDF filled with red, nothing special
            using (FileStream fs = new FileStream(pdf1, FileMode.Create, 
            FileAccess.Write, FileShare.None))
            {
                using (Document doc = new Document(PageSize.LETTER))
                {
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, 
                    fs))
                    {
                        doc.Open();
                        PdfContentByte cb = writer.DirectContent;
                        cb.SetColorFill(BaseColor.RED);
                        cb.Rectangle(0, 0, doc.PageSize.Width, 
                        doc.PageSize.Height);
                        cb.Fill();
                        doc.Close();
                    }
                }
            }

            //Create a basic PDF with a single line of text, nothing 
            special
            using (FileStream fs = new FileStream(pdf2, FileMode.Create, 
            FileAccess.Write, FileShare.None))
            {
                using (Document doc = new Document(PageSize.LETTER))
                {
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, 
                    fs))
                    {
                        doc.Open();
                        doc.Add(new Paragraph("This is a test"));
                        doc.Close();
                    }
                }
            }

            //Create a basic PDF
            using (FileStream fs = new FileStream(pdf3, FileMode.Create, 
            FileAccess.Write, FileShare.None))
            {
                using (Document doc = new Document(PageSize.LETTER))
                {
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, 
                    fs))
                    {
                        doc.Open();

                        //Get page 1 of the first file
                        PdfImportedPage imp1 = writer.GetImportedPage(new 
                        PdfReader(pdf1), 1);
                        //Get page 2 of the second file
                        PdfImportedPage imp2 = writer.GetImportedPage(new 
                        PdfReader(pdf2), 1);
                        //Add the first file to coordinates 0,0
                        writer.DirectContent.AddTemplate(imp1, 0, 0);
                        //Since we don't call NewPage the next call will 
                        operate on the same page
                        writer.DirectContent.AddTemplate(imp2, 0, 0);
                        doc.Close();

                    }
                }
            }

            this.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBoxPdfFile1Path.Text = 
            System.IO.Path.Combine(Application.StartupPath, 
            @"C:\Users\jesse\Downloads");
            textBoxPdfFile2Path.Text = 
            System.IO.Path.Combine(Application.StartupPath, 
            @"C:\Users\jesse\Downloads");

        }

        private void btnSelectFile1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                textBoxPdfFile1Path.Text = fd.FileName;
            }
        }

        private void btnSelectFile2_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                textBoxPdfFile2Path.Text = fd.FileName;
            }
        }
    }
}

我希望输出将现有文件合并为一个文件,然后将其发送到我的桌面。现在,它会创建两个pdf示例文件并将其合并,但我不知道如何从现有文件中进行选择。

1 个答案:

答案 0 :(得分:0)

使用以下代码可以完成合并文档所需的操作。  要获取各个pdf的实际路径列表,具体取决于您如何操作。  FileStream关闭后,应在指定的“ newPdfPath”路径上创建文档。

using (FileStream stream = new FileStream(newPdfPath, FileMode.Create))
                {
                    Document document = new Document();
                    PdfCopy pdf = new PdfCopy(document, stream);
                    PdfReader reader = null;
                    document.Open();
                    foreach (var item in listOfPathsToPDFs)
                    {                       
                        reader = new PdfReader(item);
                        pdf.AddDocument(reader);
                        reader.Close();                        
                    }
                    document.Close();
                }