簡體   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