【问题标题】:How to merge pdf documents and add pages in between如何合并pdf文档并在其间添加页面
【发布时间】:2017-04-20 20:56:53
【问题描述】:

我有一个要求,我必须合并多个 PDF 文档并添加带有一些文本的页面。 例如我已经从一个 PDF 复制了页面,现在我必须添加一个带有一些文本的页面,然后我需要从第二个 PDF 复制页面,然后我需要再次添加一个带有一些文本的页面...

我尝试过合并 PDF,但它只是合并了我想在每个 PDF 文档之后添加一些文本的 PDF。

我想使用 iTextSharp。下面是代码sn-p:

// 第 1 步:创建文档对象 文档 document = new Document();

        // step 2: we create a writer that listens to the document
        PdfCopy writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create));
        if (writer == null)
        {
            return;
        }

        // step 3: we open the document
        document.Open();

        foreach (string fileName in fileNames)
        {
            // we create a reader for a certain document
            PdfReader reader = new PdfReader(fileName);
            reader.ConsolidateNamedDestinations();

            // step 4: we add content
            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                PdfImportedPage page = writer.GetImportedPage(reader, i);
                writer.AddPage(page);
            }


            //This commented part is not working
            ////Add a new page to the pdf file
            //document.NewPage();

            //Paragraph paragraph = new Paragraph();
            //Font titleFont = new Font(iTextSharp.text.Font.FontFamily.HELVETICA
            //                          , 15
            //                          , iTextSharp.text.Font.BOLD
            //                          , BaseColor.BLACK
            //    );
            //Chunk titleChunk = new Chunk("Comments", titleFont);
            //paragraph.Add(titleChunk);
            //writer.Add(paragraph);

            //paragraph = new Paragraph();
            //Font textFont = new Font(iTextSharp.text.Font.FontFamily.HELVETICA
            //                         , 12
            //                         , iTextSharp.text.Font.NORMAL
            //                         , BaseColor.BLACK
            //    );
            //Chunk textChunk = new Chunk("Hello", textFont);
            //paragraph.Add(textChunk);
            //writer.Add(paragraph);
            //document.Add(paragraph);

            reader.Close();

        }

        // step 5: we close the document and writer
        writer.Close();
        document.Close();

提前致谢。

【问题讨论】:

  • 不知道为什么这会被否决。我赞成它,因为该问题描述了一个技术问题,通过提供源代码来解释,其中空白部分是真正(尽管不正确)尝试解决问题。

标签: pdf merge itext pdfpage


【解决方案1】:

您不能将document.newPage()PdfCopy 结合使用。如果要插入包含动态创建的内容的额外页面,则需要在内存中创建一个新文档:Create PDF in memory instead of physical file

例如,你可以创建这个方法:

private byte[] CreatePdf(String comments)
{
    Document doc = new Document(PageSize.LETTER);
    using (MemoryStream output = new MemoryStream())
    {
        PdfWriter wri = PdfWriter.GetInstance(doc, output);
        doc.Open();
        Paragraph header = new Paragraph("Comments");
        doc.Add(header);
        Paragraph paragraph = new Paragraph(comments);
        doc.Add(paragraph);
        doc.Close();
        return output.ToArray();
    }
}

在您的代码中,您可以像这样使用该方法:

writer.AddDocument(new PdfReader(CreatePdf("Test comment")););

请注意,您无需遍历页面。你有:

for (int i = 1; i <= reader.NumberOfPages; i++)
{
     PdfImportedPage page = writer.GetImportedPage(reader, i);
     writer.AddPage(page);
}

你可以使用:

writer.AddDocument(reader);

【讨论】:

    猜你喜欢
    • 2017-03-24
    • 1970-01-01
    • 2014-03-09
    • 2023-03-12
    • 2015-12-02
    • 2017-04-17
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    相关资源
    最近更新 更多