使用pdfbox将图像添加到pdf文件时,添加的图像没有颜色,图像的一部分应该是红色的,但它是黑色的

问题描述 投票:0回答:1

我正在使用 pdfbox 将 png 图像添加到 java 中的 pdf 文件中。 图像被添加到 pdf 中并显示,但没有颜色,只有黑色。 我将图像的一部分更改为红色,但它仍然以黑色添加到 pdf 中,我期望如果我更改图像颜色,在使用 pdf 框添加它后,它在 pdf 中也会是相同的颜色,但显然我是缺少一些东西。

PDImageXObject pdImageStamp 是我更改为有一部分红色的图像,但如果添加它会显示为黑色。

请注意,我使用的是 pdfbox 版本 2.0.8 和 Java 8。

这是我用来添加图像的代码。

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";

}

我想添加到 pdf 中的红色块的示例。

使用 pdfbox 后得到的 pdf 文件。

java pdfbox
1个回答
0
投票

无论 2.0.8 中存在什么错误,它在撰写本文时的当前版本 2.0.31 中不再出现,可以通过 maven 或在 https://pdfbox.apache.org/download 下载.html

© www.soinside.com 2019 - 2024. All rights reserved.