我的目的是绘制一个上传的图像,我不知道PDF文件的尺寸是否有一个空页面(DIN A4)。对于水平图像,我有一个带有一个水平空白页面的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页面。
答案 0 :(得分:1)
我知道找到了一个解决方案......肯定它不是最优雅的解决方案但它有效。我得到的结果是我的垂直或水平图像,如果需要缩小到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();
如果出现问题,请纠正我。