【问题标题】:How can I get Images coordinates in pdf into JSONfile?如何将 pdf 中的图像坐标转换为 JSONfile?
【发布时间】:2014-10-21 21:31:13
【问题描述】:

我已经编写了创建 html 页面的代码,其中包含在 pdf 文档中提取页面的图像。

我曾尝试从 pdf 中提取图像,然后我成功地从 pdf 中提取图像并使用 PDFBox lib 将图像应用到 html 页面。但我没有在html页面中提取图像坐标。

所以搜索了如何提取pdf中的图像坐标,我尝试使用PDFBox Library提取pdf中的图像坐标。

以下代码:

public static void main(String[] args) throws Exception
{
    try
    {
        PDDocument document = PDDocument.load(
            "/Users/tmdtjq/Downloads/PDFTest/test.pdf" );

        PrintImageLocations printer = new PrintImageLocations();
        List allPages = document.getDocumentCatalog().getAllPages();
        for( int i=0; i<allPages.size(); i++ )
        {
            PDPage page = (PDPage)allPages.get( i );
            int pageNum = i+1;
            System.out.println( "Processing page: " + pageNum );
            printer.processStream( page, page.findResources(),
                page.getContents().getStream() );
        }
    }
    finally
    {
    }
}

protected void processOperator( PDFOperator operator, List arguments ) throws IOException
{
    String operation = operator.getOperation();
    if( operation.equals( "Do" ) )
    {
        COSName objectName = (COSName)arguments.get( 0 );
        Map xobjects = getResources().getXObjects();
        PDXObject xobject = xobjects.get( objectName.getName() );
        if( xobject instanceof PDXObjectImage )
        {
            try
            {
                PDXObjectImage image = (PDXObjectImage)xobject;
                PDPage page = getCurrentPage();
                Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();
                double rotationInRadians =(page.findRotation() * Math.PI)/180;

                AffineTransform rotation = new AffineTransform();
                rotation.setToRotation( rotationInRadians );
                AffineTransform rotationInverse = rotation.createInverse();
                Matrix rotationInverseMatrix = new Matrix();
                rotationInverseMatrix.setFromAffineTransform( rotationInverse );
                Matrix rotationMatrix = new Matrix();
                rotationMatrix.setFromAffineTransform( rotation );

                Matrix unrotatedCTM = ctm.multiply( rotationInverseMatrix );
                float xScale = unrotatedCTM.getXScale();
                float yScale = unrotatedCTM.getYScale();
                float xPosition = unrotatedCTM.getXPosition();
                float yPosition = unrotatedCTM.getYPosition();

                System.out.println( "Found image[" + objectName.getName() + "] " +
                    "at " + xPosition + "," + yPosition +
                    " size=" + (xScale/100f*image.getWidth()) + "," + (yScale/100f*image.getHeight() ));
            }
            catch( NoninvertibleTransformException e )
            {
                throw new WrappedIOException( e );
            }
        }
    }
}

在图像中打印 X、Y 位置的输出为 All 0.0, 0.0。

我认为是因为 getGraphicsState() 是返回 graphicsState 的方法。

但我想将特定的图像坐标应用于 PDF 页面的高度、宽度,以便创建 html 页面。

我认为这可能是从 PDF 中的图像坐标中提取 JSON 的解决方案。

请介绍PDF中的图像坐标到JSON工具或建议PDF库。

(我已经在 FlexPaper 中使用了 pdf2json 工具。这个工具从 PDF 页面中提取 JSON 文件,不包括图像数据,而只是文本数据(内容、坐标、字体..)。)

【问题讨论】:

标签: java image pdf pdfbox


【解决方案1】:

我能够通过搜索 cm 运算符找到图像。 我通过以下方式覆盖了PDFTextStripper: 注意:不考虑旋转和镜像!

public static class TextFinder extends PDFTextStripper {

    public TextFinder() throws IOException {
        super();
    }

    @Override
    protected void startPage(PDPage page) throws IOException {
        // process start of the page
        super.startPage(page);
    }

    @Override
    public void process(PDFOperator operator, List<COSBase> arguments)
            throws IOException {

        if ("cm".equals(operator.getOperation())) {
            float width = ((COSNumber)arguments.get(0)).floatValue();
            float height = ((COSNumber)arguments.get(3)).floatValue();
            float x = ((COSNumber)arguments.get(4)).floatValue();
            float y = ((COSNumber)arguments.get(5)).floatValue();
            // process image coordinates
        }
        super.processOperator(operator, arguments);
    }

    @Override
    protected void writeString(String text,
            List<TextPosition> textPositions) throws IOException {
        for (TextPosition position : textPositions) {
            // process text coordinates
        }
        super.writeString(text, textPositions);
    }
}

当然,如果您对查找文本和图像不感兴趣,可以使用PDFStreamEngine 而不是PDFTextStripper

【讨论】:

    猜你喜欢
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 2013-09-22
    相关资源
    最近更新 更多