简体   繁体   English

使用pdfbox Java在中间位置绘制图像

[英]Draw image at mid position using pdfbox Java

I'm trying to add an image to the center of pdf using pdfbox. 我正在尝试使用pdfbox将图像添加到pdf的中心。 Below is my code but I'm unable to get the correct position of image in pdf. 以下是我的代码,但无法获取pdf中图像的正确位置。 I followed the following link In PDFBox, how to change the origin (0,0) point of a PDRectangle object? 我跟随以下链接在PDFBox中,如何更改PDRectangle对象的原点(0,0)点? to get the correct position but still image is off from the midpoint position? 获取正确的位置,但静止图像偏离中点位置?

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.util.Matrix;

public class imageAppend {
     public static void main (String[] args){

            File file = new File("...pdf file location");
            PDDocument doc = null;
            try 
            {
                doc = PDDocument.load(file);
                PDImageXObject pdImage = PDImageXObject.createFromFile("image file location", doc);

                PDPage page = doc.getPage(0);
                PDPageContentStream contentStream = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true);

               float x_pos = page.getCropBox().getWidth();
               float y_pos = page.getCropBox().getHeight();

                float x_adjusted = ( x_pos - w ) / 2;
                float y_adjusted = ( y_pos - h ) / 2;

                Matrix mt = new Matrix(1f, 0f, 0f, -1f, page.getCropBox().getLowerLeftX(), page.getCropBox().getUpperRightY());
            contentStream.transform(mt);
            contentStream.drawImage(pdImage, x_adjusted, y_adjusted, w, h);

                doc.save("new pdf file location");
                doc.close();

            } catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
}

I reproduced your problem, with my test data (unfortunately you did not share yours) I get 我用测试数据重现了您的问题(不幸的是您没有分享您的问题),我得到了

截图

The fix is simple, I removed the two lines 修复很简单,我删除了两行

Matrix mt = new Matrix(1f, 0f, 0f, -1f, page.getCropBox().getLowerLeftX(), page.getCropBox().getUpperRightY());
contentStream.transform(mt);

and now get 现在得到

截图

For the general case you should also add the coordinates of the lower left corner of the crop box to your x_adjusted and y_adjusted 对于一般情况,还应将裁剪框左下角的坐标添加到x_adjustedy_adjusted

float x_adjusted = ( x_pos - w ) / 2 + page.getCropBox().getLowerLeftX();
float y_adjusted = ( y_pos - h ) / 2 + page.getCropBox().getLowerLeftY();

( AddImage test method testImageAppendNoMirror ) AddImage测试方法testImageAppendNoMirror

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM