繁体   English   中英

使用没有图像的PDFBox将PDF转换为GREYSCALE?

[英]Converting PDF to GRAYSCALE using PDFBox without images?

我正在使用Apache PDFBox,

我想不使用图像方法将RGB PDF文件转换为另一个GREYSCALE文件,因为它使文件很大-_-!

所以这是我的步骤:

  1. 从Adobe InDesign导出(A4)First.pdf,其中包含图像,文本,矢量对象。

  2. 我阅读了First.pdf文件。 完成!

  3. 使用LayerUtility,从First.pdf复制页面,然后旋转页面并将其放入新的PDF文件(A4)Second.pdf。 完成!

    • 首选此方法,因为我需要矢量对象来减小尺寸。
  4. 然后,我想将其另存为GREY-SCALE PDF文件(Second-grayscale.pdf)

这是我的代码(不是全部):

PDDocument documentFirst = PDDocument.load("First.pdf"));

// Second.pdf its empty always
PDDocument documentSecond = PDDocument.load("Second.pdf"));

for (int page = 0; page < documentSecond.getNumberOfPages(); page++) {
    // get current page from documentSecond
    PDPage tempPage = documentSecond.getPage(page);

    // create content contentStream
    PDPageContentStream contentStream = new PDPageContentStream(documentSecond, tempPage);

    // create layerUtility
    LayerUtility layerUtility = new LayerUtility(documentSecond);

    // importPageAsForm from documentFirst
    PDFormXObject form = layerUtility.importPageAsForm(documentFirst, page);

    // saveGraphicsState
    contentStream.saveGraphicsState();

    // rotate the page
    Matrix matrix;
    matrix.rotate(Math.toRadians(90));
    contentStream.transform(matrix);

    // draw the rotated page from documentFirst to documentSecond
    contentStream.drawForm(form);

    contentStream.close();
}

// save the new document
documentSecond.save("Second.pdf");

documentSecond.close();
documentFirst.close();

// now convert it to GRAYSCALE or do it in the Loop above!

好吧,我本周刚刚开始使用Apache Box,我已经遵循了一些示例,但是大多数都已经过时并且无法正常工作,直到现在我已经完成了我所需要的,只需要灰度:)!

java中是否有其他使用开放源代码库或免费工具的解决方案。 (我发现使用Ghost脚本和Python)

我读了这个例子,但我不明白,有一个不推荐使用的功能!:

https://github.com/lencinhaus/pervads/blob/master/libs/pdfbox/src/java/org/apache/pdfbox/ConvertColorspace.java

关于PDF规范,以及更改色彩空间...

据我所知,您提到您会对基于Ghostscript的解决方案感兴趣。 如果您能够从命令行调用GS,则可以使用此命令行进行颜色到灰度的转换

gs -sDEVICE=pdfwrite -sProcessColorModel=DeviceGray -sColorConversionStrategy=Gray -dOverrideICC -o out.pdf -f input.pdf

我的答案来自如何从命令行将PDF转换为灰度以避免被光栅化?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM