在c#.net中使用iTextSharp合并多个PDF
那么我试图将多个PDF合并成一个。
编译时我没有提供任何错误。 我试图先合并文档,但是出错了,因为我正在处理表格。
这是asp.net的代码隐藏
if (Button.Equals("PreviewWord")) { String eventTemplate = Server.MapPath("/ERAS/Badges/Template/EventTemp" + EventName + ".doc"); String SinglePreview = Server.MapPath("/ERAS/Badges/Template/PreviewSingle" + EventName + ".doc"); String PDFPreview = Server.MapPath("/ERAS/Badges/Template/PDFPreviewSingle" + EventName + ".pdf"); String previewPDFs = Server.MapPath("/ERAS/Badges/Template/PreviewPDFs" + EventName + ".pdf"); if (System.IO.File.Exists((String)eventTemplate)) { if (vulGegevensIn == true) { //This creates a Worddocument and fills in names etc from database CreateWordDocument(vulGegevensIn, eventTemplate, SinglePreview, false); //This saves the SinglePreview.doc as a PDF @param place of PDFPreview CreatePDF(SinglePreview, PDFPreview); //Trying to merge String[] previewsSmall=new String[1]; previewsSmall[0] = PDFPreview; PDFMergenITextSharp.MergeFiles(previewPDFs, previewsSmall); } // merge PDFs here...........................; //here //no here// //... } }
这是PDFMergenITextSharpClass
公共静态类PDFMergenITextSharp {
public static void MergeFiles(string destinationFile, string[] sourceFiles) { try { int f = 0; // we create a reader for a certain document PdfReader reader = new PdfReader(sourceFiles[f]); // we retrieve the total number of pages int n = reader.NumberOfPages; //Console.WriteLine("There are " + n + " pages in the original file."); // step 1: creation of a document-object Document document = new Document(reader.GetPageSizeWithRotation(1)); // step 2: we create a writer that listens to the document PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create)); // step 3: we open the document document.Open(); PdfContentByte cb = writer.DirectContent; PdfImportedPage page; int rotation; // step 4: we add content while (f < sourceFiles.Length) { int i = 0; while (i < n) { i++; document.SetPageSize(reader.GetPageSizeWithRotation(i)); document.NewPage(); page = writer.GetImportedPage(reader, i); rotation = reader.GetPageRotation(i); if (rotation == 90 || rotation == 270) { cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height); } else { cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); } //Console.WriteLine("Processed page " + i); } f++; if (f < sourceFiles.Length) { reader = new PdfReader(sourceFiles[f]); // we retrieve the total number of pages n = reader.NumberOfPages; //Console.WriteLine("There are " + n + " pages in the original file."); } } // step 5: we close the document document.Close(); } catch (Exception e) { string strOb = e.Message; } } public static int CountPageNo(string strFileName) { // we create a reader for a certain document PdfReader reader = new PdfReader(strFileName); // we retrieve the total number of pages return reader.NumberOfPages; } }
我find了答案:
添加更多文件到第一个input文件数组,而不是第二个方法。
public static void CombineMultiplePDFs(string[] fileNames, string outFile) { // step 1: creation of a document-object Document 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); } PRAcroForm form = reader.AcroForm; if (form != null) { writer.CopyAcroForm(reader); } reader.Close(); } // step 5: we close the document and writer writer.Close(); document.Close(); }
我在这个网站上find了一个很好的解决scheme: http : //weblogs.sqlteam.com/mladenp/archive/2014/01/10/simple-merging-of-pdf-documents-with-itextsharp-5-4-5。 ASPX
我在这种模式下更新方法:
public static bool MergePDFs(IEnumerable<string> fileNames, string targetPdf) { bool merged = true; using (FileStream stream = new FileStream(targetPdf, FileMode.Create)) { Document document = new Document(); PdfCopy pdf = new PdfCopy(document, stream); PdfReader reader = null; try { document.Open(); foreach (string file in fileNames) { reader = new PdfReader(file); pdf.AddDocument(reader); reader.Close(); } } catch (Exception) { merged = false; if (reader != null) { reader.Close(); } } finally { if (document != null) { document.Close(); } } } return merged; }
使用iTextSharp.dll
protected void Page_Load(object sender, EventArgs e) { String[] files = @"C:\ENROLLDOCS\A1.pdf,C:\ENROLLDOCS\A2.pdf".Split(','); MergeFiles(@"C:\ENROLLDOCS\New1.pdf", files); } public void MergeFiles(string destinationFile, string[] sourceFiles) { if (System.IO.File.Exists(destinationFile)) System.IO.File.Delete(destinationFile); string[] sSrcFile; sSrcFile = new string[2]; string[] arr = new string[2]; for (int i = 0; i <= sourceFiles.Length - 1; i++) { if (sourceFiles[i] != null) { if (sourceFiles[i].Trim() != "") arr[i] = sourceFiles[i].ToString(); } } if (arr != null) { sSrcFile = new string[2]; for (int ic = 0; ic <= arr.Length - 1; ic++) { sSrcFile[ic] = arr[ic].ToString(); } } try { int f = 0; PdfReader reader = new PdfReader(sSrcFile[f]); int n = reader.NumberOfPages; Response.Write("There are " + n + " pages in the original file."); Document document = new Document(PageSize.A4); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create)); document.Open(); PdfContentByte cb = writer.DirectContent; PdfImportedPage page; int rotation; while (f < sSrcFile.Length) { int i = 0; while (i < n) { i++; document.SetPageSize(PageSize.A4); document.NewPage(); page = writer.GetImportedPage(reader, i); rotation = reader.GetPageRotation(i); if (rotation == 90 || rotation == 270) { cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height); } else { cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); } Response.Write("\n Processed page " + i); } f++; if (f < sSrcFile.Length) { reader = new PdfReader(sSrcFile[f]); n = reader.NumberOfPages; Response.Write("There are " + n + " pages in the original file."); } } Response.Write("Success"); document.Close(); } catch (Exception e) { Response.Write(e.Message); } }
在Itextsharp中合并PDF的代码
public static void Merge(List<String> InFiles, String OutFile) { using (FileStream stream = new FileStream(OutFile, FileMode.Create)) using (Document doc = new Document()) using (PdfCopy pdf = new PdfCopy(doc, stream)) { doc.Open(); PdfReader reader = null; PdfImportedPage page = null; //fixed typo InFiles.ForEach(file => { reader = new PdfReader(file); for (int i = 0; i < reader.NumberOfPages; i++) { page = pdf.GetImportedPage(reader, i + 1); pdf.AddPage(page); } pdf.FreeReader(reader); reader.Close(); File.Delete(file); }); }