本文介紹了用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();
如果有什么問題,請糾正我。
這篇關于用PDFBox在PDF文件中繪制自動調整大小的圖像的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,