在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 documen

紫衣仙女
浏览 2008回答 3
3回答

PIPIONE

在Itextsharp中合并PDF的代码&nbsp;public static void Merge(List<String> InFiles, String OutFile)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; using (FileStream stream = new FileStream(OutFile, FileMode.Create))&nbsp; &nbsp; &nbsp; &nbsp; using (Document doc = new Document())&nbsp; &nbsp; &nbsp; &nbsp; using (PdfCopy pdf = new PdfCopy(doc, stream))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doc.Open();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PdfReader reader = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PdfImportedPage page = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //fixed typo&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InFiles.ForEach(file =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader = new PdfReader(file);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < reader.NumberOfPages; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; page = pdf.GetImportedPage(reader, i + 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pdf.AddPage(page);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pdf.FreeReader(reader);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader.Close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File.Delete(file);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP