PDFBox not returning the correct size of an image -


i new pdfbox , stuck @ finding height of image in inches. after couple of searches, piece of code working with:

pdresources resources = apdpage.findresources();         graphicsstate = new pdgraphicsstate(apdpage.findcropbox());         pagewidth = apdpage.findcropbox().getwidth() / 72;         pageheight = apdpage.findcropbox().getheight() / 72;         @suppresswarnings("deprecation")         map<string, pdxobjectimage> imageobjects = resources.getimages();         if (null == imageobjects || imageobjects.isempty())             return;         (map.entry<string, pdxobjectimage> entryxobjects : imageobjects.entryset()) {              pdxobjectimage image = entryxobjects.getvalue();         //  system.out.println("bits per component: " + image.getbitspercomponent());             matrix ctmnew = graphicsstate.getcurrenttransformationmatrix();             float imagexscale = ctmnew.getxscale();             float imageyscale = ctmnew.getyscale();             system.out.println("position = " + ctmnew.getxposition() + ", " + ctmnew.getyposition());             // size in pixel             system.out.println("size = " + image.getwidth() + "px, " + image.getheight() + "px");             // size in page units             system.out.println("size = " + imagexscale + "pu, " + imageyscale + "pu");             // size in inches              imagexscale /= 72;             imageyscale /= 72;             system.out.println("size = " + imagexscale + "in, " + imageyscale + "in");             // size in millimeter             imagexscale *= 25.4;             imageyscale *= 25.4;             system.out.println("size = " + imagexscale + "mm, " + imageyscale + "mm");              system.out.printf("dpi  = %.0f dpi (x), %.0f dpi (y) %n", image.getwidth() * 72 / ctmnew.getxscale(), image.getheight() * 72 / ctmnew.getyscale());          } 

but value not coming correctly in inches. imagexscale value in pu coming 0.1 always.

any appreciated.

first of need know how bitmap images used in pdfs:

in pdf page object has collection of called resources, among them bitmap image resources, font resources, ...

you can inspect these resources do:

pdresources resources = apdpage.findresources(); @suppresswarnings("deprecation") map<string, pdxobjectimage> imageobjects = resources.getimages(); if (null == imageobjects || imageobjects.isempty())     return; (map.entry<string, pdxobjectimage> entryxobjects : imageobjects.entryset()) {     pdxobjectimage image = entryxobjects.getvalue();     system.out.println("size = " + image.getwidth() + "px, " + image.getheight() + "px"); } 

but gives pixel dimension of images available in page resources.

when such resource painted onto page, operation doing first scales down 1x1 unit square , paints scaled down version.

the reason why on screen , on paper have images of reasonable size, way painting operators work in pdfs influenced called current graphics state. graphics state contains information current fill color, line widths, etc... in particular contains called current transformation matrix defines how operation draws shall stretched, rotated, skewed, translated, ... transformed.

the usual sequence of operations when drawing bitmap image looks this:

  • ...
  • store temporary copy of current graphics state,
  • change current transformation matrix scaling transformation multiplies x coordinate desired widths , y coordinate desired height of image draw,
  • draw image referenced in resources, and
  • restore current graphics state temporarily stored values,
  • ...

thus, know dimensions of image on page, have know current transformation matrix as when image drawing operation executed.

your code, on other hand, uses current transformation matrix freshly instantiated graphics state all values @ defaults. thus, code prints false information on how image scaled on page.

to correct information, have parse sequence of operations executed creating document page.

this pdfbox printimagelocations example does: processes page content stream (which contains operations), updating copy of values of current graphics state, , when sees operation drawing bitmap image, uses value of current transformation matrix @ moment:

protected void processoperator( pdfoperator operator, list arguments ) throws ioexception {     string operation = operator.getoperation();     if( invoke_operator.equals(operation) )     {         cosname objectname = (cosname)arguments.get( 0 );         map<string, pdxobject> xobjects = getresources().getxobjects();         pdxobject xobject = (pdxobject)xobjects.get( objectname.getname() );         if( xobject instanceof pdxobjectimage )         {             pdxobjectimage image = (pdxobjectimage)xobject;             pdpage page = getcurrentpage();             int imagewidth = image.getwidth();             int imageheight = image.getheight();             double pageheight = page.getmediabox().getheight();             system.out.println("*******************************************************************");             system.out.println("found image [" + objectname.getname() + "]");              matrix ctmnew = getgraphicsstate().getcurrenttransformationmatrix();             ...             [calculate dimensions , rotation of image on page]             ...  

thus, task pdfbox example should starting point.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -