簡體   English   中英

合並多個 Pdf 文件時出現 Pdfsharp 內存不足異常

[英]Pdfsharp Out of Memory Exception when Combine Multi Pdf File

為此,我必須將大量(但未定義)的 pdf 轉換為單個 pdf,我在這里使用代碼 PDFsharp。

    // Get some file names
    string[] files = filesToPrint.ToArray();

    // Open the output document
    PdfDocument outputDocument = new PdfDocument();

    PdfPage newPage; 

    int nProcessedFile = 0;
    int nMemoryFile = 5;
    int nStepConverted = 0;
    String sNameLastCombineFile = ""; 


    // Iterate files
    foreach (string file in files)
    {
        // Open the document to import pages from it.
        PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);

        // Iterate pages
        int count = inputDocument.PageCount;
        for (int idx = 0; idx < count; idx++)
        {
            // Get the page from the external document...
            PdfPage page = inputDocument.Pages[idx];
            // ...and add it to the output document.
            outputDocument.AddPage(page);                                
        }

        nProcessedFile++;
        if (nProcessedFile >= nMemoryFile)
        {
            //nProcessedFile = 0;
            //nStepConverted++;
            //sNameLastCombineFile = "ConcatenatedDocument" + nStepConverted.ToString() + " _tempfile.pdf";

            //outputDocument.Save(sNameLastCombineFile);
            //outputDocument.Close();                 
        }
    }
    // Save the document...
    const string filename = "ConcatenatedDocument1_tempfile.pdf";
    outputDocument.Save(filename);
    // ...and start a viewer.
   Process.Start(filename);

對於少量文件,代碼可以工作,但在某些時候會產生內存不足的異常

有解決辦法嗎?

ps 我正在考慮逐步保存文件,然后將剩余的 aggiungingere 保存在內存中,但我找不到方法。

更新1:

if (nProcessedFile >= nMemoryFile)
{
nProcessedFile = 0;
//nStepConverted++;
sNameLastCombineFile = "ConcatenatedDocument" + nStepConverted.ToString() + " _tempfile.pdf";

outputDocument.Save(sNameLastCombineFile);
outputDocument.Close();

outputDocument = PdfReader.Open(sNameLastCombineFile,PdfDocumentOpenMode.Modify);
}

UPDATE 2 versione 1.32 完整示例在線錯誤: PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);

文本錯誤:無法處理 iref 流。 PDFsharp 的當前實現無法處理 Acrobat 6 引入的此 PDF 功能。

using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<String> filesToPrint = new List<string>();

            filesToPrint = Directory.GetFiles(@"D:\Downloads\RACCOLTA\FILE PDF", "*.pdf").ToList();

            // Get some file names
            string[] files = filesToPrint.ToArray();

            // Open the output document
            PdfDocument outputDocument = new PdfDocument();

            PdfPage newPage;

            int nProcessedFile = 0;
            int nMemoryFile = 5;
            int nStepConverted = 0;
            String sNameLastCombineFile = "";

            try
            {
                // Iterate files
                foreach (string file in files)
                {
                    // Open the document to import pages from it.
                    PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);

                    // Iterate pages
                    int count = inputDocument.PageCount;
                    for (int idx = 0; idx < count; idx++)
                    {
                        // Get the page from the external document...
                        PdfPage page = inputDocument.Pages[idx];
                        // ...and add it to the output document.
                        outputDocument.AddPage(page);
                    }

                    nProcessedFile++;
                    if (nProcessedFile >= nMemoryFile)
                    {
                        nProcessedFile = 0;
                        //nStepConverted++;
                        sNameLastCombineFile = "ConcatenatedDocument" + nStepConverted.ToString() + " _tempfile.pdf";

                        outputDocument.Save(sNameLastCombineFile);
                        outputDocument.Close();

                        inputDocument = PdfReader.Open(sNameLastCombineFile , PdfDocumentOpenMode.Modify);
                    }
                }
                // Save the document...
                const string filename = "ConcatenatedDocument1_tempfile.pdf";
                outputDocument.Save(filename);
                // ...and start a viewer.
                Process.Start(filename);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();

            }
        }
    }
}

UPDATE3 產生內存不足異常的代碼

            int count = inputDocument.PageCount;
            for (int idx = 0; idx < count; idx++)
            {
                // Get the page from the external document...
                newPage = inputDocument.Pages[idx];
                // ...and add it to the output document.
                outputDocument.AddPage(newPage);

                newPage.Close();
            }

我不能確切地是哪一行一般異常

我有一個類似的問題,保存、關閉和重新打開 PdfDocument 並沒有真正幫助。

我將所有(100+)大(高達 5Mb)圖像(tiff、jpg 等)添加到 pdf 文檔中,其中每個圖像都有自己的頁面。 它在圖像#50 附近墜毀。 在保存關閉重新打開之后,它確實完成了整個文檔,但仍然接近最大內存,大約 3Gb。 再多一些圖像,它仍然會崩潰。

經過更多的改進,我為 XGraphics 對象實現了一個 using,它再次好一點但不多。

向前邁出的一大步是在循環中處理 XImage! 在那之后,應用程序從未使用超過 100-200Kb,我刪除了 PdfDocument 的 save-close-reopen 並且沒有問題。

保存並關閉后outputDocument (代碼程式碼中注釋掉),你必須打開outputDocument再次,使用PdfDocumentOpenMode.Modify

它可以幫助為inputDocument添加using(...)

如果您的代碼作為 32 位進程運行,那么切換到 64 位將允許您的進程使用超過 2 GB 的 RAM(假設您的計算機具有超過 2 GB 的 RAM)。

更新:消息“無法處理 iref 流”意味着您必須使用 NuGet 上提供的 PDFsharp 1.50 Prerelease。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM