Hi, 

 

hope you can help me fixing the following unreasonable behavior:

 

situation: 

 

my code uses a pdf file with embedded text, images and forms as a template

everything works the code searches the pdf for forms on different pages and the 
forms are filled as defined

even pages with a form and image painting work, the forms prepared to 
overwritten by an image show the new image 

 

the problem: 

 

                when I paint an image on a pdfpage where a image already exists 
in the template pdf the image from the template get’s invisible 

                I checked the coordinates of the the painting – ok the new 
image is on the right position

                

 

the relevant code:

 

       for (String imagename : this.templateimages.keySet()) {

                                  PDField field2 = (PDTextField) 
acroForm.getField(imagename);

                                  if (field2 != null) {

                                        field2.setValue(" ");

                                        field2.setReadOnly(true);

 

                                        COSDictionary fieldDict = 
field2.getCOSObject();

                                        COSArray fieldAreaArray = (COSArray) 
fieldDict.getDictionaryObject(COSName.RECT);

                                        PDRectangle result = new 
PDRectangle(fieldAreaArray);

 

                                        int fieldpagenumber = 
getFormPagenr(field2);

                                        if (fieldpagenumber >= 0 && result != 
null) {

 

                                               PDPage page = 
doc.getPage(fieldpagenumber);

                                               FileObject file = 
this.templateimages.get(imagename);

                                               if (file != null && 
file.exists() == true) 

                                               {

                                                      InputStream is = 
file.getContent().getInputStream();

                                                      BufferedImage awtImage = 
ImageIO.read( is );

                                                      

                                                      PDImageXObject pdImage = 
LosslessFactory.createFromImage(doc, awtImage);

                                                      PDPageContentStream 
contents = new PDPageContentStream(doc, page);

                                                      

                                                      log.info("drawimage x={} 
y={} w={} h={}",result.getLowerLeftX(), result.getLowerLeftY(),

                                                                   
result.getWidth(), result.getHeight());

                                                      
contents.drawImage(pdImage, result.getLowerLeftX(), result.getLowerLeftY(),

                                                                   
result.getWidth(), result.getHeight());

             

                                                      contents.close();

 

                                               } else {

                                                      log.error("problem at pdf 
generate pdimage file=null or file does not exist {}"

                                                                   + ((file != 
null) ? file.getPublicURIString() : ""));

                                                      throw new Exception("file 
not found "+file);

                                               }

                                        } else {

                                               log.error("probelm at pdf 
generate pagenumber<0 or result=null");

                                               throw new Exception("template 
has no pages");

                                        }

                                  } else {

                                        throw new Exception("field for image " 
+ imagename + " not found");     

                                  }

                           }

 

 

Version: (in 2.0.1 it was the same)

 

<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->

             <dependency>

                    <groupId>org.apache.pdfbox</groupId>

                    <artifactId>pdfbox</artifactId>

                    <version>2.0.16</version>

             </dependency>

 

 

Please Help!

 

Martin

 

 

Reply via email to