【问题标题】:Why aren't 2 pdf files getting merged?为什么不合并 2 个 pdf 文件?
【发布时间】:2019-09-17 13:08:40
【问题描述】:

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

【问题讨论】:

  • 您尝试写入同时尝试读取的文件。使用不同的结果文件名。

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 2013-06-10
    相关资源
    最近更新 更多