插入页面到使用iTextSharp的现有的PDF [英] Insert page into existing PDF using itextsharp

查看:2216
本文介绍了插入页面到使用iTextSharp的现有的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用iTextSharp的创建从多个PDF文件为一个PDF。我要如何插入新的页面到已多页文件中的PDF文件?当我使用添加页面被覆盖现有的网页,并只保存1页的选择了。



下面是我使用的网页添加到现有的PDF的代码:

  PdfReader读卡器=新PdfReader(sourcePdfPath); 
文档的文档=新的文件(reader.GetPageSizeWithRotation(1));
PdfCopy pdfCopy =新PdfCopy(文件,新的System.IO.FileStream(outputPdfPath,System.IO.FileMode.Create));
的MemoryStream MemoryStream的=新的MemoryStream();
PdfWriter作家= PdfWriter.GetInstance(文件,MemoryStream的);
document.AddDocListener(作家);
document.Open();

为(INT P = 1; P< = reader.NumberOfPages,P ++)
{
如果(pagesToExtract.FindIndex(S = GT; S == P)= = -1)继续;
document.SetPageSize(reader.GetPageSize(P));
document.NewPage();
PdfContentByte CB = writer.DirectContent;
PdfImportedPage pageImport = writer.GetImportedPage(读卡器,P);

INT腐烂= reader.GetPageRotation(对);
如果(腐== 90 ||腐== 270)
{
cb.AddTemplate(pageImport,0,-1.0F,1.0F,0,0,reader.GetPageSizeWithRotation(对)。高度);
}
,否则
{
cb.AddTemplate(pageImport,1.0F,0,0,1.0F,0,0);
}

pdfCopy.AddPage(pageImport);
}

pdfCopy.Close();


解决方案

此代码工作。你需要有一个不同的文件输出结果。

 私有静态无效AppendToDocument(字符串sourcePdfPath1,串sourcePdfPath2,串outputPdfPath) 
{使用
(VAR sourceDocumentStream1 =新的FileStream(sourcePdfPath1,FileMode.Open))
{
使用(VAR sourceDocumentStream2 =新的FileStream(sourcePdfPath2,FileMode.Open))
{
使用(VAR destinationDocumentStream =新的FileStream(outputPdfPath,FileMode.Create))
{
变种pdfConcat =新PdfConcatenate(destinationDocumentStream);
变种pdfReader =新PdfReader(sourceDocumentStream1);

VAR页=新的List< INT>();
的for(int i = 0; I< pdfReader.NumberOfPages;我++)
{
pages.Add(I)
}

pdfReader.SelectPages(页);
pdfConcat.AddPages(pdfReader);

pdfReader =新PdfReader(sourceDocumentStream2);

页面=新的List< INT>();
的for(int i = 0; I< pdfReader.NumberOfPages;我++)
{
pages.Add(I)
}

pdfReader.SelectPages(页);
pdfConcat.AddPages(pdfReader);

pdfReader.Close();
pdfConcat.Close();
}
}
}
}


We are using itextsharp to create a single PDF from multiple PDF files. How do I insert a new page into a PDF file that has multiple pages already in the file? When I use add page it is overwriting the existing pages and only saves the 1 page that was selected.

Here is the code that I am using to add the page to the existing PDF:

PdfReader reader = new PdfReader(sourcePdfPath);
                Document document = new Document(reader.GetPageSizeWithRotation(1));
                PdfCopy pdfCopy = new PdfCopy(document, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
                MemoryStream memoryStream = new MemoryStream();
                PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
                document.AddDocListener(writer);
                document.Open();

                for (int p = 1; p <= reader.NumberOfPages; p++)
                {
                    if (pagesToExtract.FindIndex(s => s == p) == -1) continue;
                    document.SetPageSize(reader.GetPageSize(p));
                    document.NewPage();
                    PdfContentByte cb = writer.DirectContent;
                    PdfImportedPage pageImport = writer.GetImportedPage(reader, p);

                    int rot = reader.GetPageRotation(p);
                    if (rot == 90 || rot == 270)
                    {
                        cb.AddTemplate(pageImport, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(p).Height);
                    }
                    else
                    {
                        cb.AddTemplate(pageImport, 1.0F, 0, 0, 1.0F, 0, 0);
                    }

                    pdfCopy.AddPage(pageImport);
                }

                pdfCopy.Close();

解决方案

This code works. You need to have a different file to output the results.

private static void AppendToDocument(string sourcePdfPath1, string sourcePdfPath2, string outputPdfPath)
{
    using (var sourceDocumentStream1 = new FileStream(sourcePdfPath1, FileMode.Open))
    {
        using (var sourceDocumentStream2 = new FileStream(sourcePdfPath2, FileMode.Open))
        {
            using (var destinationDocumentStream = new FileStream(outputPdfPath, FileMode.Create))
            {
                var pdfConcat = new PdfConcatenate(destinationDocumentStream);
                var pdfReader = new PdfReader(sourceDocumentStream1);

                var pages = new List<int>();
                for (int i = 0; i < pdfReader.NumberOfPages; i++)
                {
                    pages.Add(i);
                }

                pdfReader.SelectPages(pages);
                pdfConcat.AddPages(pdfReader);

                pdfReader = new PdfReader(sourceDocumentStream2);

                pages = new List<int>();
                for (int i = 0; i < pdfReader.NumberOfPages; i++)
                {
                    pages.Add(i);
                }

                pdfReader.SelectPages(pages);
                pdfConcat.AddPages(pdfReader);

                pdfReader.Close();
                pdfConcat.Close();
            }
        }
    }
}

这篇关于插入页面到使用iTextSharp的现有的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆