【问题标题】:Merge PDF based on size using Aspose使用 Aspose 根据大小合并 PDF
【发布时间】:2018-05-25 02:30:54
【问题描述】:

由于我是 Aspose 新手,因此在以下情况下我需要帮助。
我想使用 Aspose 将多个 PDF 合并为 1 个 PDF,我可以轻松完成,但问题是,我想将 PDF 大小限制为 200MB。
这意味着,如果我合并的 PDF 大小大于 200MB,那么我需要将 PDF 拆分为多个 PDF。 例如,如果我合并的 PDF 为 300MB,那么第一个 PDF 应为 200MB,并且第二个 PDF 应该是 100MB。

主要问题是,我无法在下面的代码中找到文档的大小。我正在使用下面的代码。

Document destinationPdfDocument = new Document();
                Document sourcePdfDocument = new Document();

            //Merge PDF one by one
            for (int i = 0; i < filesFromDirectory.Count(); i++)
            {
                if (i == 0)
                {
                    destinationPdfDocument = new Document(filesFromDirectory[i].FullName);
                }
                else
                {
                    // Open second document
                    sourcePdfDocument = new Document(filesFromDirectory[i].FullName);

                    // Add pages of second document to the first
                    destinationPdfDocument.Pages.Add(sourcePdfDocument.Pages);


                    //** I need to check size of destinationPdfDocument over here to limit the size of resultant PDF**

                }
            }

            // Encrypt PDF
            destinationPdfDocument.Encrypt("userP", "ownerP", 0, CryptoAlgorithm.AESx128);

            string finalPdfPath = Path.Combine(destinationSourceDirectory, destinatedPdfPath);

            // Save concatenated output file
            destinationPdfDocument.Save(finalPdfPath);

还可以根据大小合并 PDF 的其他方式。
提前致谢

【问题讨论】:

    标签: c# pdf merge aspose aspose.pdf


    【解决方案1】:

    恐怕在物理保存之前没有直接的方法来确定 PDF 文件的大小。因此,我们已经在问题跟踪系统中将功能请求记录为 PDFNET-43073,并且产品团队一直在调查此功能的可行性。一旦我们有关于该功能可用性的一些重要更新,我们一定会通知您。请给我们一点时间。

    但是,作为一种解决方法,您可以将文档保存到内存流中并检查该内存流的大小,无论它是否超出了您所需的 PDF 大小。请检查以下代码 sn-p,我们已经使用上述方法生成了所需大小为 200MB 的 PDF。

    //Instantiate document objects
    Document destinationPdfDocument = new Document();
    Document sourcePdfDocument = new Document();
    
    //Load source files which are to be merged
    var filesFromDirectory = Directory.GetFiles(dataDir, "*.pdf");
    
    for (int i = 0; i < filesFromDirectory.Count(); i++)
    {    
     if (i == 0)
     {
     destinationPdfDocument = new Document(filesFromDirectory[i]);
     }
     else
     {
     // Open second document
     sourcePdfDocument = new Document(filesFromDirectory[i]);
     // Add pages of second document to the first
     destinationPdfDocument.Pages.Add(sourcePdfDocument.Pages);
     //** I need to check size of destinationPdfDocument over here to limit the size of resultant PDF**
     MemoryStream ms = new MemoryStream();
     destinationPdfDocument.Save(ms);
     long filesize = ms.Length;
     ms.Flush();
     // Compare the filesize in MBs
     if (i == filesFromDirectory.Count() - 1)
     {
      destinationPdfDocument.Save(dataDir + "PDFOutput_" + i + ".pdf");
     }
     else if ((filesize / (1024 * 1024)) < 200)
     continue;
     else
     {
      destinationPdfDocument.Save(dataDir + "PDFOutput_" + i.ToString() + ".pdf");
      destinationPdfDocument = new Document();
     }
     }
     }
    

    我希望这会有所帮助。如果您需要任何进一步的帮助,请告诉我们。

    我与 Aspose 合作,担任开发人员宣传员。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多