How can I get Images coordinates in pdf into JSONfile?

限于喜欢 提交于 2019-12-08 02:23:39

问题


I have coded creating html page included images extracting a page in pdf document.

I had tried to extract images from pdf and then I succeeded to extract images from pdf and to apply the images to html page using PDFBox lib. but I did not extract image coordinates in html page.

So searched how to extract image coordinates in pdf, I tried to extract image coordinates in pdf using PDFBox Library.

Below code :

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 );
            }
        }
    }
}

Outputs printing X,Y Positions in images is All 0.0, 0.0.

I think because getGraphicsState() is method to return the graphicsState.

But I want to get specific images coordinates applied to height,width of a PDF page in order to create html page.

I think maybe it is solution to extract JSON from images coordinates in PDF.

Please introduce image coordinates in PDF to JSON tool or suggest PDF Library.

(Already I used pdf2json tool in FlexPaper. this tool extracts JSONfile including not images data but just texts data(content, coordinates, font..) from PDF page.)


回答1:


I was able to find images with searching for cm operator. I overrided PDFTextStripper the following way: Note: it doesn't take into account rotation and mirroring!

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);
    }
}

Of course, one can use PDFStreamEngine instead of PDFTextStripper, if one is not interested in finding text together with images.



来源:https://stackoverflow.com/questions/25541699/how-can-i-get-images-coordinates-in-pdf-into-jsonfile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!