为什么不合并2个pdf文件?

时间:2019-04-28 20:37:15

标签: c# asp.net asp.net-mvc-4 c#-4.0 itext

我正在将2个PDF文件合并为一个,但是即使没有显式错误也不会合并任何文件。我已经尝试了很多,但仍然无法解决问题。这是我正在使用的项目中的文件夹。允许写权限,它允许在其中写入和创建文件。

public static void MergeFiles(string destinationFile, string[] sourceFiles) {
    try {
        sourceFiles = new string[2] {
            HostingEnvironment.MapPath(@"/Downloads/Certificates/InspectionReport(78).pdf"),
            HostingEnvironment.MapPath(@"/Downloads/Certificates/InspectionReport(78).pdf")
        };
        //outputPdfPath = Path.GetFileName("~/Downloads/Certificates/119.FDV-3686.pdf");
        destinationFile = HostingEnvironment.MapPath(@"/Downloads/Certificates/InspectionReport(78).pdf");

        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;
    }
}

1 个答案:

答案 0 :(得分:1)

我将多个pdf文件与using语句合并以完成此操作。希望这会有所帮助。

using (var fs = new FileStream(HttpContext.Current.Server.MapPath(outputFilepath),FileMode.Create))
{
    using (var document = new Document())
    {
        using (var pdfCopy = new PdfCopy(document, fs))
        {
            document.Open();

            for (var i = 0; i < numberOfFilesToMerge; i++)
            {
                using (var pdfReader = new PdfReader(sourceFiles[i]))
                {
                    for (var page = 0; page < pdfReader.NumberOfPages;)
                    {
                        pdfCopy.AddPage(pdfCopy.GetImportedPage(pdfReader, ++page));
                    }
                }
            }
        }
     }
 }