問題描述
我有一個 pdf 文件(附件).我的目標是使用 pdfbox 原樣將 pdf 轉換為圖像(與在 Windows 中使用截圖工具相同).pdf有各種形狀和文字.
i have a pdf file(attached). My objective is to convert a pdf to an image using pdfbox AS IT IS,(same as using snipping tool in windows). The pdf has all kinds of shapes and text .
我正在使用以下代碼:
PDDocument doc = PDDocument.load("Hello World.pdf");
PDPage firstPage = (PDPage) doc.getDocumentCatalog().getAllPages().get(67);
BufferedImage bufferedImage = firstPage.convertToImage(imageType,screenResolution);
ImageIO.write(bufferedImage, "png",new File("out.png"));
當我使用代碼時,圖像文件給出完全錯誤的輸出(附加了 out.png)
when i use the code, the image file gives totally wrong outputs(out.png attached)
如何讓 pdfbox 拍攝類似直接快照圖像的內容?
how do i make pdfbox take something like a direct snapshot image?
另外,我注意到png的圖像質量不太好,有什么辦法可以提高生成圖像的分辨率?
also, i noticed that the image quality of the png is not so good, is there any way to increase the resolution of the generated image?
這是pdf(見第68頁)https://drive.google.com/file/d/0B0ZiP71EQHz2NVZUcElvbFNreEU/編輯?usp=共享
here is the pdf(see page number 68) https://drive.google.com/file/d/0B0ZiP71EQHz2NVZUcElvbFNreEU/edit?usp=sharing
編輯 2:似乎所有的文字都消失了.我也嘗試使用 PDFImageWriter 類
EDIT 2: it seems that all the text isvanishing. i also tried using the PDFImageWriter class
test.writeImage(doc, "png", null, 68, 69, "final.png",TYPE_USHORT_GRAY,200 );
同樣的結果
推薦答案
事實證明 jpedal(lgpl) 完美地完成了轉換(就像快照一樣).
it turns out that jpedal(lgpl) does the converting perfectly(just like a snapshot).
這是我用過的:
PdfDecoder decode_pdf = new PdfDecoder(true);
FontMappings.setFontReplacements();
decode_pdf.openPdfFile("Hello World.pdf");
decode_pdf.setExtractionMode(0,800,3);
try {
for(int i=0;i<40;i++)
{
BufferedImage img=decode_pdf.getPageAsImage(2+i);
ImageIO.write(img, "png",new File(String.valueOf(i)+"out.png"));
}
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
decode_pdf.closePdfFile();
} catch (PdfException e) {
e.printStackTrace();
}
它工作正常.
這篇關于將 PDF 轉換為圖像(格式正確)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!