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