用PDFBox在PDF文件中绘制自动调整大小的图像
我的目标是用一个空白页面(DIN A4)在PDF文件上绘制一个我不知道其尺寸的上传图像。对于水平图像,我有一个包含一个水平空白页面的PDF文件,对于垂直图像,我有一个包含一个垂直页面的PDF文件。
这是我到目前为止的代码:
File image = convertMultipartFileToFile(file); //I get a MultipartFile from my RequestParam (Spring) - converting works fine
BufferedImage awtImage = ImageIO.read(image);
String path = "";
if (awtImage.getWidth() > awtImage.getHeight()) {
path = MyController.class.getResource("/pdf4ImageUploadHorizontal.pdf").getPath();
} else {
path = MyController.class.getResource("/pdf4ImageUploadVertical.pdf").getPath();
}
pdf = new File(path);
PDDocument doc = PDDocument.load(pdf);
PDPage page = doc.getPage(0);
int actualPDFWidth = 0;
int actualPDFHeight = 0;
if (awtImage.getWidth() > awtImage.getHeight()) {
actualPDFWidth = (int) PDRectangle.A4.getHeight();
actualPDFHeight = (int) PDRectangle.A4.getWidth();
} else {
actualPDFWidth = (int) PDRectangle.A4.getWidth();
actualPDFHeight = (int) PDRectangle.A4.getHeight();
}
// Add image to page
PDImageXObject pdImage = PDImageXObject.createFromFileByContent(image, doc);
Dimension scaledDim = getScaledDimension(new Dimension(pdImage.getWidth(), pdImage.getHeight()), new Dimension(actualPDFWidth, actualPDFHeight)); // I'm using this function: https://stackoverflow.com/questions/23223716/scaled-image-blurry-in-pdfbox
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
contentStream.drawImage(pdImage, 0, 0, scaledDim.width, scaledDim.height);
contentStream.close();
doc.save("c:\xyz\pdf.pdf");
对于垂直图像,一切正常(我希望图像在页面居中,但这是下一步)。
问题在于水平图像:我得到的不是我上传的水平图像填充整个水平pdf页面,而是一个水平pdf页面,其中左侧的图像向右旋转90度,并从上到下适合(缩放功能有效,但不是我希望的方式):我的愿望是在不旋转的情况下将上载的水平或垂直图片正确插入到目标PDF页面。
解决方案
我知道找到了一个解决方案...当然这不是最优雅的解决方案,但它很管用。我得到的结果是我的垂直或水平的图像,如果必要的话,缩小到A4格式,并在页面上居中。 我的代码:
File image = convertMultipartFileToFile(file);
BufferedImage awtImage = ImageIO.read(image);
// check if horizontal or vertical
Boolean isHorizontal = false;
if (awtImage.getWidth() > awtImage.getHeight()) {
isHorizontal = true;
}
String path = "";
// get actual height and width of pdf page 'cause pdfbox sees page always as vertical and only saves the rotation
// ....-------------------
// ...|...................|
// ...|.........A4........|...0.x
// ...|......PDF.page.....|..0y-|----------------------------
// ...|......vertical.....|.....|............A4..............|
// ...|...._________......|.....|.........PDF.page...........|
// ...|...(.........).....|.....|........horizontal..........|
// ...|...(..image..).....|.....|...._______________.........|
// ...|...(.........).....|.....|...(...............)........|
// ...|...(.........).....|.....|...(....image......)........|
// ...|...(.........).....|.....|...(_______________)........|
// ...|...(_________).....|.....|----------------------------
// 0x-|-------------------
// ..0y
int actualPDFWidth = 0;
int actualPDFHeight = 0;
if (isHorizontal) {
actualPDFWidth = (int) PDRectangle.A4.getHeight();
actualPDFHeight = (int) PDRectangle.A4.getWidth();
path = MyController.class.getResource("/pdf4ImageUploadHorizontal.pdf").getPath();
} else {
actualPDFWidth = (int) PDRectangle.A4.getWidth();
actualPDFHeight = (int) PDRectangle.A4.getHeight();
path = MyController.class.getResource("/pdf4ImageUploadVertical.pdf").getPath();
}
pdf = new File(path);
PDDocument doc = PDDocument.load(pdf);
PDPage page = doc.getPage(0);
PDImageXObject pdImage = PDImageXObject.createFromFileByContent(image, doc);
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
// scale image
Dimension scaledDim = getScaledDimension(new Dimension(pdImage.getWidth(), pdImage.getHeight()), new Dimension(actualPDFWidth, actualPDFHeight)); // I'm using this function: https://stackoverflow.com/questions/23223716/scaled-image-blurry-in-pdfbox
// if horizontal rotate 90°, calculate position and draw on page
if (isHorizontal) {
int x = (int) PDRectangle.A4.getWidth() - (((int) PDRectangle.A4.getWidth() - scaledDim.height) /2);
int y = ((int) PDRectangle.A4.getHeight() - scaledDim.width) / 2;
AffineTransform at = new AffineTransform(scaledDim.getHeight(), 0, 0, scaledDim.getWidth(), x, y);
at.rotate(Math.toRadians(90));
Matrix m = new Matrix(at);
contentStream.drawImage(pdImage, m);
} else {
int x = ((int) PDRectangle.A4.getWidth() - scaledDim.width) / 2;
int y = ((int) PDRectangle.A4.getHeight() - scaledDim.height) / 2;
contentStream.drawImage(pdImage, x, y, scaledDim.width, scaledDim.height);
}
contentStream.close();
doc.save("c:\xyz\pdf.pdf");
doc.close();
如果有什么问题,请纠正我。
相关文章