How to Merge PDFs From a ZipArchive in to one PDF in Memory

51 Views Asked by At

I have an application that needs to read Zip files that contain a full directory and multiple files.

enter image description here

I have it working to read each pdf and write it to disk then combine afterwards. However, the I/O is dumb. I want to read each pdf from the zip file and combine in to one in one method.

I could only find this example that uses iTextSharp which is deprecated.

Here is what I have so far:

public static void ProcessZipFileTest(string zipFile)
{
    List<string> pdfFiles = new List<string>();

    try
    {
        MemoryStream finalStream = new MemoryStream();

        using (ZipArchive archive = ZipFile.OpenRead(zipFile))
        {
            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                if (entry.FullName.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
                {
                    //// Write the file to disk for combining separately
                    //string destinationPath = Path.GetFullPath(Path.Combine(ProcessingFolder, Path.GetFileName(entry.FullName)));
                    //entry.ExtractToFile(destinationPath);
                    //pdfFiles.Add(destinationPath);

                    PdfCopyFields copy = new PdfCopyFields(finalStream);    // this is the iTextSharp way and outdated
                    string file1Path = "Sample1.pdf";
                    string file2Path = "Sample2.pdf";

                    var ms1 = new MemoryStream(File.ReadAllBytes(file1Path));
                    ms1.Position = 0;
                    copy.AddDocument(new PdfReader(ms1));
                    ms1.Dispose();
                }
            }
        }

        // write the final stream to disk
    }
    catch (Exception e)
    {
        // TODO LOG FAILURE
    }
1

There are 1 best solutions below

0
FatherOfDiwaffe On

Based on feedback, I'm going to write to disk and combine. The speed will be tested.