在 c#.net 中使用 iTextSharp 合并多个 PDF [英] Merging multiple PDFs using iTextSharp in c#.net

查看:94
本文介绍了在 c#.net 中使用 iTextSharp 合并多个 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在尝试将多个 PDF 合并为一个.

Well i'm trying to merge multiple PDFs in to one.

我在编译时没有出错.我尝试先合并文档,但由于我正在处理表格而出错.

I gives no errors while compiling. I tried to merge the docs first but that went wrong because I'm working with tables.

这是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 class 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;
}
}

推荐答案

我找到了答案:

向第一个输入文件数组中添加更多文件,而不是第二种方法.

Instead of the 2nd Method, add more files to the first array of input files.

public static void CombineMultiplePDFs(string[] fileNames, string outFile)
{
    // step 1: creation of a document-object
    Document document = new Document();
    //create newFileStream object which will be disposed at the end
    using (FileStream newFileStream = new FileStream(outFile, FileMode.Create))
    {
       // step 2: we create a writer that listens to the document
       PdfCopy writer = new PdfCopy(document, newFileStream);

       // 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();
   }//disposes the newFileStream object
}
    

这篇关于在 c#.net 中使用 iTextSharp 合并多个 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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