Pdf与itextsharp合并

问题描述 投票:1回答:1

我正在尝试创建一个将现有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示例文件并将它们组合在一起,但我不知道如何从现有文件中进行选择。

c# pdf merge itext
1个回答
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();
                }
© www.soinside.com 2019 - 2024. All rights reserved.