【问题标题】:Resize page with ABCPdf before rendering (huge images in the pdf)渲染前使用 ABCPdf 调整页面大小(pdf 中的巨大图像)
【发布时间】:2016-04-07 13:50:03
【问题描述】:

当我尝试将 pdf 文件转换为单独的图像文件作为旧浏览器的后备时,我遇到了 ABCPdf 问题。

我有一些工作代码可以完美地呈现页面并将呈现的大小调整为所需的大小。现在当pdf页面很大w7681px x h10978px时出现我的问题。它几乎杀死了我的开发机器,部署机器甚至无法咀嚼文件。

我通常只是将页面 1 对 1 渲染为 pdf 页面,然后使用其他算法来调整此图像的大小。这效率不高,因为 ABCPdf 需要消耗大量能量来输出此图像。

我有以下代码:

    private byte[] GeneratePng(Doc pdfDoc, int dpi)
    {
        var useDpi = dpi;
        pdfDoc.Rendering.DotsPerInch = useDpi;
        pdfDoc.Rendering.SaveQuality = 100;
        pdfDoc.Rect.String = pdfDoc.CropBox.String;
        pdfDoc.Rendering.ResizeImages = true;

        int attemptCount = 0;

        for (;;)
        {
            try
            {
                return pdfDoc.Rendering.GetData("defineFileTypeDummyString.png");
            }
            catch
            {
                if (++attemptCount == 3) throw;
            }
        }
    }

我尝试了以下解决方案:

调整页面大小

pdfDoc.SetInfo(pdfDoc.Page, "/MediaBox:Rect", "0 0 200 300");

调整页面大小并输出。这似乎根本没有任何改变。

在渲染之前调整图像大小:

foreach (IndirectObject io in pdfDoc.ObjectSoup) {
  if (io is PixMap) {
    PixMap pm = (PixMap)io;
    pm.Realize(); // eliminate indexed color images
    pm.Resize(pm.Width / 4, pm.Height / 4);
  }
}

什么也没做,仍然导致加载时间过长。

在渲染前运行缩小尺寸操作:

  using (ReduceSizeOperation op = new ReduceSizeOperation(pdfDoc))
    op.Compact(true);

什么也没做。刚刚直接去渲染,花了很长时间。

有人可以帮我吗?也许指向我一些 ABCPdf 调整大小算法之类的。

【问题讨论】:

    标签: c# pdf image-resizing abcpdf


    【解决方案1】:

    好的,所以我与 ABCPdf 的客户支持人员进行了交谈,他们给了我以下信息。

    doc1.Read(originalPDF);
    
    // Specify size of output page.  (This example scales the page, maintaining the aspect ratio,
    // but you could set the MediaBox Height and Width to any desired value.)
    doc2.MediaBox.Height = doc1.MediaBox.Height / 8;
    doc2.MediaBox.Width = doc1.MediaBox.Width / 8;
    doc2.Rect.SetRect(doc2.MediaBox);
    doc2.Page = doc2.AddPage();
    
    // Create the output image
    doc2.AddImageDoc(doc1, 1, null);
    doc2.Rendering.Save(savePath);
    

    这应该与单页 PDF 一起使用,所以如果你有一个充满大图片的 pdf,那么你应该把它切碎。您可以按照我的其他 Q/A 进行操作:Chop PDFs into single pages

    他们在上述代码中使用的渲染算法是由 ABCPdf 自动检测的,您无法自己控制它(他们告诉我我不想这样做)。所以我相信他们的代码。至少我做了一个测试,质量看起来与InterpolationMode.HighQualityBicubic 非常相似,只是在缩放时有所不同。所以我也不会太在意。

    最后,与渲染和调整大小相比,上述代码的速度提高了大约 10 倍。所以如果你经常做这个操作真的很值得。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多