您好,pdf的拆分合并问题

您好,小弟使用Aspose.Pdf,遇到了问题,我有3个pdf文件,001_1,001_2,001_3。001_1pdf文件里面有若干的页数,如何计算pdf每一页的大小。

小弟要实现,一张pdf文件大小是1MB。目前将所有pdf合并成一张pdf,以下下是实现代码,小弟想拆分pdf,获得每页的大小,合并pdf,计算出如果大一1MB就形成新的pdf。

感谢您的支持,我将提供测试文件。测试文件.zip (1.5 MB)

       // 创建 pdffileeditor 对象
        PdfFileEditor pdfEditor = new PdfFileEditor();
        // 创建 pdf 文件路径数组
        string[] filesArray = new string[hbfiles.Count()];

        for (int i = 0; i < hbfiles.Count(); i++)
        {

            filesArray[i] = hbfiles[i];

        }

        //filesArray[0] = "file1.pdf";
        //filesArray[1] = "file2.pdf";
        //filesArray[2] = "file3.pdf"; 

        //获得程序根目录
        String pdf = HB;
        // 合并文件
        pdfEditor.Concatenate(filesArray, pdf);

@fhn123456

为了计算文档生成期间的文件大小,您可以将其保存到MemoryStream并检查流大小。例如,请检查以下代码段,其中文档正在合并,并且文件大小也相应地被检查。

Document destinationPdfDocument = new Document();
Document sourcePdfDocument = new Document();
// 这是所有pdf文件所在的目录
var filesFromDirectory = Directory.GetFiles(dataDir, "*.pdf");

for (int i = 0; i < filesFromDirectory.Count(); i++)
{
 if (i == 0)
 {
  destinationPdfDocument = new Document(filesFromDirectory[i]);
 }
 else
 {
  // 打开第二个文件
  sourcePdfDocument = new Document(filesFromDirectory[i]);
  // 将第二个文档的页面添加到第一个文档
  destinationPdfDocument.Pages.Add(sourcePdfDocument.Pages);
  // 我需要在此处检查destinationPdfDocument的大小以限制生成的PDF的大小
  MemoryStream ms = new MemoryStream();
  destinationPdfDocument.Save(ms);
  long filesize = ms.Length;
  ms.Flush();
  // 比较文件大小与MB
  if (i == filesFromDirectory.Count() - 1)
  {
   destinationPdfDocument.Save(dataDir + "PDFOutput_" + i + ".pdf");
  }
  else if ((filesize / (1024 * 1024)) < 1) // Check if PDF is less than 1MB
   continue;
  else
  {
   destinationPdfDocument.Save(dataDir + "PDFOutput_" + i.ToString() + ".pdf");
   destinationPdfDocument = new Document();
  }
 }
}
// 保存串联的输出文件
destinationPdfDocument.Save(dataDir + "CombinedPDF.pdf");

如果您仍然需要进一步的帮助,请告知我们。