Quantcast
Channel: Aspose.Pdf Product Family
Viewing all articles
Browse latest Browse all 3131

PDF - ImagePlacementAbsorber order not same as getResources().getImages()

$
0
0
  Hi,

Does ImagePlacementAbsorber has images in same order as that of page.getResources().getImages() ?

We are using below code to delete image from a rectangle, but it looks sometimes the image indexes (ps and ps2) differ.

Images not getting correctly delete based on index from ImagePlacementAbsorber. Also the image objects differes in both collections.

Can you please let us know how can we get the Rectangle of XImage using getImages()  ?

or to delete image using ImagePlacementAbsorber? ip.getImage().delete(); --this is not working.


Code Using ImagePlacementAbsorber :

  ImagePlacementAbsorber ipa = new ImagePlacementAbsorber();
            pdfDoc.getPages().accept(ipa);
            ImagePlacementCollection ipc = ipa.getImagePlacements();
            int ips = ipc.size();
            LOGGER.info("ips count:"+ ips);
            HashMap<Integer,XImage>  imgMap = new ArrayList<Integer,XImage>();
           
            int imgInd=0;
            int prevPageInd=0;
            int thisPageInd=0;
            int imgInPageInd=0;
           
            for(int ps=1;ps<=ips;ps++)
            {
                ImagePlacement ip = ipc.get_Item(ps);               
                Rectangle r = ip.getRectangle();
              XImage img= ip.getImage();
              String name = img.getName();
                imgMap.put(ps,ip.getImage());
                thisPageInd = ip.getPage().getNumber()  ;
                if(   thisPageInd != prevPageInd )          
                {
                imgInPageInd=0;               
                }
                imgInPageInd= imgInPageInd+1;
                if(deleteThisImage)               
                {
                    imgInd = imgInPageInd;
                }              
            }
           
 Code Using getImages() :
                int ps2=0;
                 HashMap<Integer,XImage>  imgMap2 = new ArrayList<Integer,XImage>();
                for (int p = 1; p <= numPages; p++)
                    {
                       
                        ArrayList<Integer> imgIndxs = new ArrayList<Integer>();
                        int noOfImages = pdfDoc.getPages().get_Item(p).getResources().getImages().size();
                        String [] str = pdfDoc.getPages().get_Item(p).getResources().getImages().getNames();     
       
                        for (int k = 1; k <= noOfImages; k++)
                        {
                            XImage img2 = pdfDoc.getPages().get_Item(p).getResources().getImages().get_Item(k);
                             String name2 = img.getName();
                               imgMap2.put(ps2,ip.getImage());
                               ps2++;                           
                        }                           
                }
               
Delete image:
pdfDoc.getPages().get_Item(1).getResources().getImages().delete(imgInd); //say delete one image from page 1.

Thanks.
-Sonali

Viewing all articles
Browse latest Browse all 3131

Trending Articles