javapdfbox

When adding an image to a pdf file using pdfbox the image is added without color, a part of the image should be red but it is black


I am using pdfbox to add a png image to a pdf file in java. The image is added to the pdf and is displayed but with no color only black. I changed a part of the image to Red but it still gets added to the pdf as black, I was expecting if I changed the image color it would also be the same color in the pdf after I added it with pdf box but clearly I am missing something.

PDImageXObject pdImageStamp Is the image that I changed to have one part Red but if added it display as Black.

Please note I am using pdfbox version 2.0.8 and Java 8.

Here is my code used to add the image.

private  String addStampSignatureImageToPage(PDDocument pdDoc, PDPage pdPage,String stampPath ,String signaturePath, String locationOnPdf) throws Exception{

StandardFunctions stdClass = new StandardFunctions();
//Decode file settings
String [] fileSettings = locationOnPdf.split(",");

PDImageXObject pdImageStamp = null;
PDImageXObject pdImageSignature = null;
PDPageContentStream contentStream = null;
Matrix matrix = null;

if(fileSettings==null||fileSettings.length!=9)
{
    //No Settings found do not add stamp
    return "No file settings for location found, no stamp added";
}

int stampWidth = Integer.valueOf(fileSettings[0]);
int stampHeight = Integer.valueOf(fileSettings[1]);
int stampX = Integer.valueOf(fileSettings[2]);
int stampY = Integer.valueOf(fileSettings[3]);
int signatureWidth = Integer.valueOf(fileSettings[4]);
int signatureHeight = Integer.valueOf(fileSettings[5]);
int signatureX = Integer.valueOf(fileSettings[6]);
int signatureY = Integer.valueOf(fileSettings[7]);
int rotateDegree = Integer.valueOf(fileSettings[8]);

try{
    //Define the stamp image
    BufferedImage awtImage = ImageIO.read(new File(stampPath));
    System.out.println("3");
    //pdImageStamp = PDImageXObject.createFromFile(stampPath, pdDoc);
    pdImageStamp = LosslessFactory.createFromImage(pdDoc, awtImage);
    
    pdImageStamp.setWidth(stampWidth);
    pdImageStamp.setHeight(stampHeight);
    
    
    //Define the signature image
    pdImageSignature = PDImageXObject.createFromFile(signaturePath, pdDoc);
    pdImageSignature.setWidth(signatureWidth);
    pdImageSignature.setHeight(signatureHeight);
    
    //Page stream that we want to append our image to.
    contentStream = new PDPageContentStream(pdDoc, pdPage,PDPageContentStream.AppendMode.APPEND,true);
    
   
    matrix = new Matrix();
    //If we want to rotate the image we apply it to our matrix to transform the page.
    if(rotateDegree > 0)
    {
        matrix.rotate(Math.toRadians(rotateDegree));
        contentStream.transform(matrix);
    }
    
    //When lanscape mode the y needs to be negative and atleast the size of the image width. the x should be positive at least the size of the image.
    //                                  x    y    w    h
    //contentStream.drawImage(pdImage, 100,-100, 100, 100);
    
    //Add the stamp to the pdf
    contentStream.drawImage(pdImageStamp, stampX, stampY, stampWidth, stampHeight);
    //Add the signature to the pdf
    contentStream.drawImage(pdImageSignature, signatureX, signatureY, signatureWidth, signatureHeight);
    
    //Close stream
    contentStream.close();
}
catch(Exception e)
{
    stdClass.writeLog("Something crashed...Signature stamp image" + e.getMessage());
    
    
}
finally{
    stdClass = null;
    fileSettings = null;
    pdImageStamp = null;
    pdImageSignature = null;
    contentStream = null;
    matrix = null;
}


return "DONE";

}

An example of the red block I want to add to the pdf. enter image description here

The resulting pdf I get after I use pdfbox.

enter image description here

Edit To help someone else struggling with the same issue. When I further investigated the problem I discovered that after creating the image you should not set the width and height using the following code.

    pdImageStamp.setWidth(stampWidth);
    pdImageStamp.setHeight(stampHeight);

When you use this for some reason the color gets lost it only gest added as black with no color. Rather use the drawImage function to set the width and height like this.

contentStream.drawImage(pdImageStamp, stampX, stampY, stampWidth, stampHeight);

Even if you are using the old pdf box version this works for me for .png files.


Solution

  • Whatever the bug was in 2.0.8, it no longer occurs in 2.0.31 which is the current version at the time of writing this, and can be downloaded through maven or at https://pdfbox.apache.org/download.html

    Also

    After creating the image you should not set the width and height using the following code.

    pdImageStamp.setWidth(stampWidth);
    pdImageStamp.setHeight(stampHeight);
    

    PDImageXObject methods setWidth and setHeight don't change the width and height of the displayed image, they change the width and height metadata of the bitmap image resource, As you don't change the bitmap image itself at the same time, this change will usually result in a mismatch of the bitmap and its metadata. This can result in arbitrary issues.

    Rather use the drawImage function to set the width and height like this.

    contentStream.drawImage(pdImageStamp, stampX, stampY, stampWidth, stampHeight);
    

    Even if you are using the old pdf box version this works for .png files.