You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by "Clark, Raymond C" <Ra...@ehi.com> on 2017/04/03 15:39:57 UTC

Specifying an image once in a PDF

Hi,

I am using PDFBox in production and it is working great.  I have a question on how I am using it.  I have a method that puts an image in a PDF as you can see below.  Since I have the same image on multiple pages it seems to me that this code can/is drawing the same image on multiple pages.  Is there a way to drop an image into a PDF only once and then reference it on the various pages specifying the location it is to be printed at?  I hope that I made myself clear, I'm trying to avoid multiple copies of the same image in the PDF.

Here is my code:

public void drawImage(Column column, float xPosition, float yPosition) throws ProductRequestException {
        float scale = 1f; // alter this value to set the image size
        float imageYPosition = PDFGeneratorHelper.getImageYPosition(column, yPosition);
        float imageXPosition = PDFGeneratorHelper.getImageXPosition(column, xPosition);

        // Does NOT compress GIF and other image files
        PDImageXObject pdImage;
        try {
                pdImage = PDImageXObject.createFromFile(
                                new File(getClass().getClassLoader().getResource(column.getImagePath()).toURI())
                                        .getAbsolutePath(), document);
        } catch(IOException ioe) {
                throw new ProductRequestException("IOException 1 in drawImage", ioe);
        } catch(URISyntaxException use) {
                throw new ProductRequestException("URISyntaxException in drawImage", use);
        }
        pdImage.getCOSObject().setName("BDSImageName", column.getImagePath());
        try {
                if (column.getImageWidth() == null) {
                        contentStream.drawImage(pdImage, imageXPosition, imageYPosition, pdImage.getWidth() * scale,
                                        pdImage.getHeight() * scale);
                } else {
                        contentStream.drawImage(pdImage, imageXPosition, imageYPosition,
                                        Float.parseFloat(column.getImageWidth()),
                                        Float.parseFloat(column.getImageHeight()));
                }
        } catch(IOException ioe) {
                throw new ProductRequestException("IOException 2 in drawImage", ioe);
        }
}

Thank you,
Ray

________________________________

CONFIDENTIALITY NOTICE: This e-mail and any files transmitted with it are intended solely for the use of the individual or entity to whom they are addressed and may contain confidential and privileged information protected by law. If you received this e-mail in error, any review, use, dissemination, distribution, or copying of the e-mail is strictly prohibited. Please notify the sender immediately by return e-mail and delete all copies from your system.



Re: Specifying an image once in a PDF

Posted by Tilman Hausherr <TH...@t-online.de>.
Yes, not only this is possible, this is a good practice. Just use the 
PDImageXObject object as often as you want within that document. But the 
custom name you're using ("BDSImageName") will also be the same.

Tilman


Am 03.04.2017 um 17:39 schrieb Clark, Raymond C:
> Hi,
>
> I am using PDFBox in production and it is working great.  I have a question on how I am using it.  I have a method that puts an image in a PDF as you can see below.  Since I have the same image on multiple pages it seems to me that this code can/is drawing the same image on multiple pages.  Is there a way to drop an image into a PDF only once and then reference it on the various pages specifying the location it is to be printed at?  I hope that I made myself clear, I'm trying to avoid multiple copies of the same image in the PDF.
>
> Here is my code:
>
> public void drawImage(Column column, float xPosition, float yPosition) throws ProductRequestException {
>          float scale = 1f; // alter this value to set the image size
>          float imageYPosition = PDFGeneratorHelper.getImageYPosition(column, yPosition);
>          float imageXPosition = PDFGeneratorHelper.getImageXPosition(column, xPosition);
>
>          // Does NOT compress GIF and other image files
>          PDImageXObject pdImage;
>          try {
>                  pdImage = PDImageXObject.createFromFile(
>                                  new File(getClass().getClassLoader().getResource(column.getImagePath()).toURI())
>                                          .getAbsolutePath(), document);
>          } catch(IOException ioe) {
>                  throw new ProductRequestException("IOException 1 in drawImage", ioe);
>          } catch(URISyntaxException use) {
>                  throw new ProductRequestException("URISyntaxException in drawImage", use);
>          }
>          pdImage.getCOSObject().setName("BDSImageName", column.getImagePath());
>          try {
>                  if (column.getImageWidth() == null) {
>                          contentStream.drawImage(pdImage, imageXPosition, imageYPosition, pdImage.getWidth() * scale,
>                                          pdImage.getHeight() * scale);
>                  } else {
>                          contentStream.drawImage(pdImage, imageXPosition, imageYPosition,
>                                          Float.parseFloat(column.getImageWidth()),
>                                          Float.parseFloat(column.getImageHeight()));
>                  }
>          } catch(IOException ioe) {
>                  throw new ProductRequestException("IOException 2 in drawImage", ioe);
>          }
> }
>
> Thank you,
> Ray
>
> ________________________________
>
> CONFIDENTIALITY NOTICE: This e-mail and any files transmitted with it are intended solely for the use of the individual or entity to whom they are addressed and may contain confidential and privileged information protected by law. If you received this e-mail in error, any review, use, dissemination, distribution, or copying of the e-mail is strictly prohibited. Please notify the sender immediately by return e-mail and delete all copies from your system.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org