【问题标题】:Actually cropping a PDF with PDF Clown实际使用 PDF Clown 裁剪 PDF
【发布时间】:2016-06-06 09:07:41
【问题描述】:

我的目标实际上是使用 PdfClown 裁剪 PDF 文件。 有很多工具/库允许裁剪 PDF,更改 PDF 裁剪框。这允许将内容隐藏在矩形区域之外,但内容仍然存在,它可以通过 PDF 解析器访问,并且 PDF 大小不会改变。

相反,我需要的是创建一个只包含矩形区域内内容的新页面。

到目前为止,我已经尝试扫描内容并有选择地克隆它们。但我还没有成功。关于使用 PdfClown 有什么建议吗?

我看到有人正在尝试与 PdfBox Cropping a region from a PDF page with PDFBox 类似的操作,但尚未成功。

【问题讨论】:

  • 我编辑了我的问题以便更清楚。在我看来,我或多或少地问过同样的事情,但也许我错了。在某个时刻,作者说是的,我想我想要第二个。我想要一个仅包含边界框内的绘图、文本和图像说明的新 PDF
  • 哦,我明白了。我已经删除了我的评论。你确实在问几乎不可能的事情。
  • @Lorenza 你想要的是一个实际的编辑功能,这绝对不是微不足道的实现。您可能想查看 itext-xtra 包中的 iText 5 PdfCleanUp 功能以获得灵感。这种实现已经适用于相当多的 PDF,但仍有一些方法可以实现一般可用性。 (它已从 iText 7 中的开放功能中删除,现在作为封闭源插件继续开发。)
  • 投反对票的时候能给点动力吗?我是 stackoverflow 的新手,但我想从投票中学到一些东西。谢谢
  • 我尝试了 iText5 PdfCleanUp,它实际上删除了特定矩形区域内的文本内容。不确定它是否与其他内容相同,尤其是因为 pdf 大小没有太大变化。我会深入看看。

标签: pdf pdfclown


【解决方案1】:

有点晚了,但也许对某人有帮助; 我成功地完成了您的要求 - 但与其他图书馆一起。 所需库:iText 4 或 5 和 Ghostscript

使用伪代码的第 1 步

使用 iText,创建一个带有空白文档的 PDFWRITER 实例。打开要裁剪的原始文件的 PDFREADER 对象。导入页面,从源中获取 PDFTemplate 对象,将其 .boundingBox 属性设置为所需的裁剪框,将模板包装到 iText Image 对象中并将其粘贴到新页面的绝对位置。

Dim reader As New PdfReader(sourcefile)
Dim doc As New Document()
Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New System.IO.FileStream(outputfilename, System.IO.FileMode.Create))

//get the source page as an Imported Page
Dim page As PdfImportedPage = writer.GetImportedPage(reader, indexOfPageToGet) page

//create PDFTemplate Object at original size from source - see iText in Action book Page 91 for full details
Dim pdftemp As PdfTemplate = page.CreateTemplate(page.Width, page.Height) 
//paste the original page onto the template object, see iText documentation what those parameters do (scaling, mirroring)
pdftemp.AddTemplate(page, 1, 0, 0, 1, 0, 0)
//now the critical part - set .boundingBox property on the template. This makes all objects outside the rectangle invisible
pdftemp.boundingBox = {iText Rectangle Structure with new Cropbox}
//template not needed anymore
writer.ReleaseTemplate(pdftemp) 
//create an iText IMAGE object as wrapper to the template - with this img object absolute positionion on the final page is much easier
dim img as iTextSharp.Text.Image = Image.GetInstance(pdftemp)
// set img position
img.SetAbsolutePosition(x, y)
//set optional Rotation if needed
img.RotationDegrees = 0
//finally, this adds the actual content to the new document
doc.Add(img) 
//cleanup
doc.Close()
reader.Close()
writer.Close()

输出文件在视觉上看起来会被裁剪。但这些对象仍然存在于 PDF 流中。文件大小可能会保持很小的变化。

第 2 步:

使用 Ghostscript 和输出设备 pdfwrite,结合正确的命令行参数,您可以从步骤 1 重新处理 PDF。这将使您的 PDF 更小。有关参数https://www.ghostscript.com/doc/9.52/Use.htm,请参阅 Ghostscript 文档 此步骤实际上消除了边界框之外的对象 - 您在 OP 中要求的要求,至少对于我处理的文件而言。

可选步骤 3: 使用带有 -g 选项的 MUTOOL,您可以清理未使用的 XREF 对象。您的原始 PDF 可能有很多外部参照,这会增加文件大小。裁剪后可能不再需要其中的一些。 https://mupdf.com/docs/manual-mutool-clean.html

PDF 格式是一件棘手的事情,通常我会同意@Tilman Hausherr,我的建议可能不适用于所有文件并涵盖“几乎不可能”的情况,但它适用于我处理的所有情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 2011-09-05
    • 2015-07-07
    • 2020-02-05
    • 2013-06-09
    • 2016-07-26
    • 2013-08-10
    相关资源
    最近更新 更多