如何将 ZipArchive 中的 PDF 合并到内存中的一个 PDF

问题描述 投票:0回答:1

我有一个应用程序需要读取包含完整目录和多个文件的 Zip 文件。

我可以读取每个 pdf 并将其写入磁盘,然后进行合并。然而,I/O 是愚蠢的。我想阅读 zip 文件中的每个 pdf 并合并为一合一的方法。

我只能找到这个使用 iTextSharp 的示例,该示例已被弃用。

这是我到目前为止所拥有的:

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
    }
c# .net-core itext itext7
1个回答
0
投票

根据反馈,我将写入磁盘并合并。将测试速度。

© www.soinside.com 2019 - 2024. All rights reserved.