PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Sep 26, 2024 10:03 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Thu Sep 12, 2024 12:06 pm 
Offline

Joined: Thu Sep 12, 2024 11:53 am
Posts: 1
I am working on merging multiple PDFs using PDFSharp and need to impose a size limit on the output files. The code works correctly when implemented in a Windows service, but I'm encountering the below error when attempting to run the same code in an AWS Lambda function.
Error: The document was already saved and cannot be modified anymore. Saving a document converts its in memory representation into a PDF file or stream. This can only be done once. After that process the in memory representation is outdated and protected against further modification.
Code:
Code:
public void MergePDFsUsingPDFSharpWithSizeLimitNew(List<string> pdfFiles, string outputDirectory)
{
    const int maxFileSizeKB = 6000; // Maximum file size in KB
    var outputFileIndex = 1;
    var outputFileName = Path.Combine(outputDirectory, $"mergedUsingPdfSharp_{outputFileIndex}.pdf");

    _log.Info($"{nameof(PdfMergerService)}: MergePDFsUsingPDFSharpWithSizeLimitNew: Starting PDF merge...");

    var stopwatch = Stopwatch.StartNew();

    var outputDocument = new PdfDocument();

    foreach (var pdfFile in pdfFiles)
    {
        _log.Info($"{nameof(PdfMergerService)}: Processing file {pdfFile}");
        try
        {
            if (!File.Exists(pdfFile))
                throw new FileNotFoundException($"File not found: {pdfFile}");

            using var inputDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);
            for (int i = 0; i < inputDocument.PageCount; i++)
            {
                outputDocument.AddPage(inputDocument.Pages[i]);

                // Save to a temporary stream and check file size
                using var tempStream = new MemoryStream();
                outputDocument.Save(tempStream);
                var fileSizeKB = tempStream.Length / 1024; // Get size in KB

                // If the file size exceeds the limit, save the current PDF and start a new one
                if (fileSizeKB >= maxFileSizeKB)
                {
                    SaveCurrentOutput(outputDocument, outputDirectory, ref outputFileIndex);

                    // Start a new document
                    outputDocument = new PdfDocument();
                }
            }
        }
        catch (Exception ex)
        {
            _log.Error($"{nameof(PdfMergerService)}: Error processing {pdfFile}: {ex.Message}");
        }
    }

    // Save any remaining pages in the output document
    if (outputDocument.PageCount > 0)
    {
        SaveCurrentOutput(outputDocument, outputDirectory, ref outputFileIndex);
    }

    stopwatch.Stop();
    _log.Info($"{nameof(PdfMergerService)}: Merge completed in {stopwatch.ElapsedMilliseconds} ms.");
}

private void SaveCurrentOutput(PdfDocument outputDocument, string outputDirectory, ref int outputFileIndex)
{
    var outputFileName = Path.Combine(outputDirectory, $"mergedUsingPdfSharp_{outputFileIndex}.pdf");

    try
    {
        outputDocument.Save(outputFileName);
        _log.Info($"{nameof(PdfMergerService)}: Output saved to {outputFileName}");
    }
    catch (Exception ex)
    {
        _log.Error($"{nameof(PdfMergerService)}: Error saving merged PDF: {ex.Message}");
    }

    // Increment file index for the next output
    outputFileIndex++;
}


Top
 Profile  
Reply with quote  
PostPosted: Thu Sep 12, 2024 12:53 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 984
Location: CCAA
Bhagyashree Teli wrote:
The code works correctly when implemented in a Windows service, but I'm encountering the below error when attempting to run the same code in an AWS Lambda function.
AIUI you get the same behaviour under Windows and under AWS.
It's a feature that you can only call "Save()" once.
Start with a new PdfDocument after calling "Save()".

Or call "Save()", then call "Open()" to try and add more pages.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 73 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group