是否有使用iTextSharp将一个PDF文档附加到另一个文档的直接方法?

我在网上搜索了如何做到这一点的例子。 我发现了一些似乎需要更多参与的东西。 所以我的问题是,使用iTextSharp,是否有一种相当简洁的方法将一个PDF文档追加到另一个?

最理想的是,这不会涉及第三个文件。 只需打开第一个PDF文档,将第二个PDF文档附加到第一个,然后关闭它们。

我真的可能会遗漏一些东西,但我做了一些更简单的事情。 我承认这个解决方案可能不会更新书签(就像到目前为止最好的答案),但它对我来说完美无缺。 由于我将文档与可填写的表单合并,我使用PdfCopyFields而不是PdfCopy。

这是代码(我已经删除了所有error handling以使实际代码更加可见,如果您打算使用代码,请添加一个try..finally来关闭打开的资源):

void MergePdfStreams(List Source, Stream Dest) { PdfCopyFields copy = new PdfCopyFields(Dest); foreach (Stream source in Source) { PdfReader reader = new PdfReader(source); copy.AddDocument(reader); } copy.Close(); } 

您可以传递任何流,无论是FileStream,MemoryStream(从数据库读取PDF时都很有用,不需要临时文件等)

样品用法:

  void TestMergePdfStreams() { List sources = new List() { new FileStream("template1.pdf", FileMode.Open), new FileStream("template2.pdf", FileMode.Open), new MemoryStream((byte[])someDataRow["PDF_COLUMN_NAME"]) }; MergePdfStreams(sources, new FileStream("MergedOutput.pdf", FileMode.Create)); } 

好吧,这不是直截了当的,但它起作用并且速度惊人。 (它使用第三个文件,没有打开和附加的东西。)我在docs / examples中发现了这个。 这是代码:

 private void CombineMultiplePDFs( string[] fileNames, string outFile ) { int pageOffset = 0; ArrayList master = new ArrayList(); int f = 0; Document document = null; PdfCopy writer = null; while ( f < fileNames.Length ) { // we create a reader for a certain document PdfReader reader = new PdfReader( fileNames[ f ] ); reader.ConsolidateNamedDestinations(); // we retrieve the total number of pages int n = reader.NumberOfPages; ArrayList bookmarks = SimpleBookmark.GetBookmark( reader ); if ( bookmarks != null ) { if ( pageOffset != 0 ) { SimpleBookmark.ShiftPageNumbers( bookmarks, pageOffset, null ); } master.AddRange( bookmarks ); } pageOffset += n; if ( f == 0 ) { // step 1: creation of a document-object document = new Document( reader.GetPageSizeWithRotation( 1 ) ); // step 2: we create a writer that listens to the document writer = new PdfCopy( document, new FileStream( outFile, FileMode.Create ) ); // step 3: we open the document document.Open(); } // step 4: we add content for ( int i = 0; i < n; ) { ++i; if ( writer != null ) { PdfImportedPage page = writer.GetImportedPage( reader, i ); writer.AddPage( page ); } } PRAcroForm form = reader.AcroForm; if ( form != null && writer != null ) { writer.CopyAcroForm( reader ); } f++; } if ( master.Count > 0 && writer != null ) { writer.Outlines = master; } // step 5: we close the document if ( document != null ) { document.Close(); } } 

是。 我在iText论坛上看过一个名为PdfManipulation的课程。 使用该类将涉及第三个文件。

该类最初是在VB.Net中。 我从vbforums.com上的post下载了它。 显然,它没有合并文件function,所以我根据该类中的代码编写了一个。

这是在没有iTextSharp的机器上编写的。 这可能有错误。 我甚至不确定页码是基于0还是基于1。 但试一试。

 public static void MergePdfFiles(IEnumerable files, string output) { iTextSharp.text.Document doc; iTextSharp.text.pdf.PdfCopy pdfCpy; doc = new iTextSharp.text.Document(); pdfCpy = new iTextSharp.text.pdf.PdfCopy(doc, new System.IO.FileStream(output, System.IO.FileMode.Create)); doc.Open(); foreach (string file in files) { // initialize a reader iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(file); int pageCount = reader.NumberOfPages; // set page size for the documents doc.SetPageSize(reader.GetPageSizeWithRotation(1)); for (int pageNum = 1; pageNum <= pageCount; pageNum++) { iTextSharp.text.pdf.PdfImportedPage page = pdfCpy.GetImportedPage(reader, pageNum); pdfCpy.AddPage(page); } reader.Close(); } doc.Close(); } 

我不知道如何为PDF文件做这件事,但对于postscript,你只是连接文件。 如果您安装了pdf2ps和ps2pdf,则以下内容将完成:

 pdf2ps file1.pdf file1.ps pdf2ps file2.pdf file2.ps cat file1.ps file2.ps > combined.ps ps2pdf combined.ps combined.pdf 

我不是pdf2ps或ps2pdf的专家。 我只使用过ps2pdf,当我这样做时,它会将文本保留为文本(我仍然可以从生成的pdf中选择和复制文本)。 当我执行上述步骤(pdf-> ps,combine,ps-> pdf)时,我最终会得到一个像图像一样的pdf。 不知道为什么。