[英]pdfbox and itext extracting image with incorrect dpi
當我使用pdfbox提取圖像時,我得到一些PDF的圖像dpi不正確。 當我使用Photoshop或Acrobat Reader Pro提取圖像時,我可以看到使用Windows照片查看器的圖像的dpi為200,但是當我使用pdfbox提取圖像時,dpi為72。
為了提取圖像我使用以下代碼: 無法從PDFA1-格式文檔中提取圖像
當我查看日志時,我看到一個不尋常的條目:2015-01-23-main - DEBUG-org.apache.pdfbox.util.TIFFUtil:
<?xml version="1.0" encoding="UTF-8"?><javax_imageio_jpeg_image_1.0> <JPEGvariety> <app0JFIF majorVersion="1" minorVersion="2" resUnits="0" Xdensity="1" Ydensity="1" thumbWidth="0" thumbHeight="0"/> </JPEGvariety> <markerSequence> <dqt> <dqtable elementPrecision="0" qtableId="0"/> <dqtable elementPrecision="0" qtableId="1"/> </dqt> <dht> <dhtable class="0" htableId="0"/> <dhtable class="0" htableId="1"/> <dhtable class="1" htableId="0"/> <dhtable class="1" htableId="1"/> </dht> <sof process="0" samplePrecision="8" numLines="0" samplesPerLine="0" numFrameComponents="3"> <componentSpec componentId="1" HsamplingFactor="2" VsamplingFactor="2" QtableSelector="0"/> <componentSpec componentId="2" HsamplingFactor="1" VsamplingFactor="1" QtableSelector="1"/> <componentSpec componentId="3" HsamplingFactor="1" VsamplingFactor="1" QtableSelector="1"/> </sof> <sos numScanComponents="3" startSpectralSelection="0" endSpectralSelection="63" approxHigh="0" approxLow="0"> <scanComponentSpec componentSelector="1" dcHuffTable="0" acHuffTable="0"/> <scanComponentSpec componentSelector="2" dcHuffTable="1" acHuffTable="1"/> <scanComponentSpec componentSelector="3" dcHuffTable="1" acHuffTable="1"/> </sos> </markerSequence> </javax_imageio_jpeg_image_1.0>
我試圖谷歌,但我可以看到通過此日志找出pdfbox的含義。 這是什么意思?
您可以從以下鏈接下載帶有此問題的示例pdf: http : //myslams.com/test/1.pdf
我甚至嘗試過itext,但是用96 dpi提取圖像。
難道我做錯了什么? 或pdfbox和itext有這個限制?
經過一番挖掘,我找到了你的1.pdf。 從而,...
在對最近的答案 @Tilman的評論中,您正在討論這個較舊的答案 ,其中@Tilman指向PrintImageLocations PDFBox示例。 我為你的文件運行它並獲得:
Processing page: 0
*******************************************************************
Found image [Im0]
position = 0.0, 0.0
size = 1704px, 888px
size = 613.44, 319.68
size = 8.52in, 4.44in
size = 216.408mm, 112.776mm
Processing page: 1
*******************************************************************
Found image [Im0]
position = 0.0, 0.0
size = 1704px, 2800px
size = 613.44, 1008.0
size = 8.52in, 14.0in
size = 216.408mm, 355.6mm
Processing page: 2
*******************************************************************
Found image [Im0]
position = 0.0, 0.0
size = 1704px, 2800px
size = 613.44, 1008.0
size = 8.52in, 14.0in
size = 216.408mm, 355.6mm
Processing page: 3
*******************************************************************
Found image [Im0]
position = 0.0, 0.0
size = 1704px, 1464px
size = 613.44, 527.04
size = 8.52in, 7.3199997in
size = 216.408mm, 185.928mm
在所有頁面上,這在x和y方向上均為200dpi(1704px / 8.52in = 888px / 4.44in = 2800px / 14.0in = 1464px / 7.32in = 200dpi)。
因此,PDFBox為您提供了您所追求的dpi值。
(@Tilman:該示例的當前2.0.0-SNAPSHOT版本返回完全無稽之談;您可能想要解決此問題。)
該PDFBox示例的簡化iText版本將是:
public void printImageLocations(InputStream stream) throws IOException
{
PdfReader reader = new PdfReader(stream);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
ImageRenderListener listener = new ImageRenderListener();
for (int page = 1; page <= reader.getNumberOfPages(); page++)
{
System.out.printf("\nPage %s:\n", page);
parser.processContent(page, listener);
}
}
static class ImageRenderListener implements RenderListener
{
public void beginTextBlock() { }
public void renderText(TextRenderInfo renderInfo) { }
public void endTextBlock() { }
public void renderImage(ImageRenderInfo renderInfo)
{
try
{
PdfDictionary imageDict = renderInfo.getImage().getDictionary();
float widthPx = imageDict.getAsNumber(PdfName.WIDTH).floatValue();
float heightPx = imageDict.getAsNumber(PdfName.HEIGHT).floatValue();
float widthUu = renderInfo.getImageCTM().get(Matrix.I11);
float heigthUu = renderInfo.getImageCTM().get(Matrix.I22);
System.out.printf("Image %.0fpx*%.0fpx, %.0fuu*%.0fuu, %.2fin*%.2fin\n", widthPx, heightPx, widthUu, heigthUu, widthUu/72, heigthUu/72);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
(注意:我假設沒有旋轉和未圖像的圖像。)
您的文件的結果:
Page 1:
Image 1704px*888px, 613uu*320uu, 8,52in*4,44in
Page 2:
Image 1704px*2800px, 613uu*1008uu, 8,52in*14,00in
Page 3:
Image 1704px*2800px, 613uu*1008uu, 8,52in*14,00in
Page 4:
Image 1704px*1464px, 613uu*527uu, 8,52in*7,32in
因此,一直也是200dpi。 所以iText也為你提供了你所追求的dpi值。
顯然, 您引用的代碼沒有機會在PDF的上下文中報告合理的dpi值,因為它只提取資源中找到的圖像,但忽略了在頁面上使用相應圖像資源的方式 。
圖像資源可以被拉伸,旋轉,傾斜,...當他在頁面內容中使用它時,作者喜歡的任何方式。
順便說一下,如果作者沒有傾斜並且僅旋轉90°的倍數,則dpi值才有意義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.