简体   繁体   English

如何使用iTextSharp在多页PDF中的每隔一页之后插入一页PDF

[英]How do I use iTextSharp to insert a single page PDF after every other page in multipage PDF

I am trying to use iTextSharp to take a single page PDF and insert that page after each page of multiple page PDF. 我正在尝试使用iTextSharp获取单页PDF,然后在多页PDF的每一页之后插入该页面。 Currently I use an old version of Aspose to apply overlays and backers to single page and multipage PDF file. 目前,我使用的是Aspose的旧版本,将叠加层和支持层应用于单页和多页PDF文件。 Using it is simple, but I'm trying to get rid of third party controls so I am trying to replace it a library using iTextSharp. 使用它很简单,但是我试图摆脱第三方控件,所以我试图使用iTextSharp将其替换为库。 I have the overlay part working perfectly, but the adding a backer is getting me on multipage PDF files. 我的叠加部分工作正常,但是添加支持者使我可以使用多页PDF文件。

For instance the multipage PDF has pages p1, p2, p3, p4 etc. I need it to have a single page PDF called backer inserted after every page as in p1, backer, p2, backer, p3, backer, p4, backer. 例如,多页PDF具有页面p1,p2,p3,p4等。我需要它在每页之后插入一个称为backer的单页面PDF,如p1,backer,p2,backer,p3,backer,p4,backer。

This should be something simple to do I would think, but I can't find any documentation or examples and it is proving to be difficult. 我认为这应该很简单,但是我找不到任何文档或示例,事实证明这很困难。

Does anybody out there have an example of how this is done? 有没有人举这个例子?

If the backer page does not contain any annotations, you can implement your task like this: 如果支持页面不包含任何注释,则可以执行以下任务:

string sourceMultiPage = PATH_OF_THE_MULTIPAGE_PDF;
string sourceSinglePage = PATH_OF_THE_SINGLEPAGE_PDF;
string dest = PATH_OF_THE_RESULT;

using (PdfReader readerSingle = new PdfReader(sourceSinglePage))
using (PdfReader readerMulti = new PdfReader(sourceMultiPage))
using (PdfStamper stamper = new PdfStamper(readerMulti, new FileStream(dest, FileMode.Create)))
{
    PdfImportedPage singlePage = stamper.GetImportedPage(readerSingle, 1);
    Rectangle pageRect = readerSingle.GetPageSizeWithRotation(1);
    for (int page = readerMulti.NumberOfPages + 1; page > 1; page--)
    {
        stamper.InsertPage(page, pageRect);
        stamper.GetOverContent(page).AddTemplate(singlePage, pageRect.Left, pageRect.Bottom);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM