我正在使用“pdfbox-app-2.0.0-RC2.jar”
我创建了一个缩小pdf内容的类,并在指定的坐标下使用此内容创建新的pdf。
我的问题是如果修改了pdf文件并添加了新内容,那么缩小文件中的内容就会丢失。
以下是我的方法:
public static String scaleInputPdf(String sourceFileName, float xpos,
float ypos, float imgCtrlWidth, float imgCtrlHeight,
PDRectangle pageSize) throws IOException {
String workdir = "/tmp";
File tmpDir = new File(workdir);
if(! tmpDir.exists()) {
tmpDir.mkdir();
}
String destPath = workdir + "/Result.pdf";
PDDocument sourceDoc = null;
try {
// load the source PDF
sourceDoc = PDDocument.load(new File(sourceFileName));
// create a new PDF and add a blank page
PDDocument doc = new PDDocument();
for (int sourcePageNo = 0; sourcePageNo < sourceDoc
.getNumberOfPages(); sourcePageNo++) {
PDPage page = new PDPage();
page.setMediaBox(pageSize);
doc.addPage(page);
PDPage currentSourcePage = sourceDoc.getPage(sourcePageNo);
PDPageContentStream contents = new PDPageContentStream(doc,
page, true, true, true);
float scaleFactor = getScalingFactor(currentSourcePage,
imgCtrlWidth, imgCtrlHeight, pageSize);
PDFormXObject form = null;
// Create a Form XObject from the source document using
// LayerUtility
LayerUtility layerUtility = new LayerUtility(doc);
form = layerUtility.importPageAsForm(sourceDoc,
sourcePageNo);
// draw a scaled form
contents.saveGraphicsState();
Matrix scalingMatrix = Matrix.getScaleInstance(scaleFactor,
scaleFactor);
contents.transform(scalingMatrix);
Matrix translatingMatrix = Matrix.getTranslateInstance(
xpos,
getYPosition(currentSourcePage, pageSize, ypos,
scaleFactor));
contents.transform(translatingMatrix);
contents.drawForm(form);
contents.restoreGraphicsState();
contents.close();
}
doc.save(destPath);
doc.close();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (sourceDoc != null) {
sourceDoc.close();
}
}
System.err.println("Done! /n file is at following location=" +destPath);
return destPath;
}
private static float getScalingFactor(PDPage sourcePage,
float imgCtrlWidth, float imgCtrlHeight, PDRectangle pageSize) {
PDRectangle imageSize = sourcePage.getMediaBox();
if (imageSize.getHeight() > 792) {
if (pageSize.equals(PDRectangle.LETTER)) {
imgCtrlWidth = 838.8f; // 932 * 0.90
imgCtrlHeight = 1018f; // 1132 *0.90
} else if (pageSize.equals(PDRectangle.A4)) {
imgCtrlWidth = 819f; // 910 * 0.90
imgCtrlHeight = 972f; // 1080 * 0.90
}
}
float scaleX = imgCtrlWidth / imageSize.getWidth();
float scaleY = imgCtrlHeight / imageSize.getHeight();
float scaleFactor = ((scaleX < scaleY) ? scaleX : scaleY);
if (imageSize.getHeight() > 792 && scaleFactor > 0.88) {
scaleFactor = 0.88f;
}
return scaleFactor;
}
private static float getYPosition(PDPage sourcePage,
PDRectangle finalDocPageSize, float yMarginPos, float scaleFactor) {
float yPos = yMarginPos;
float imgHeight = sourcePage.getMediaBox().getHeight();
PDRectangle cropBox = sourcePage.getCropBox();
if (imgHeight > 792) {
if (cropBox.getLowerLeftY() != 0) {
yPos = cropBox.getLowerLeftY() + 40;
}
} else {
yPos = (finalDocPageSize.getHeight() - (imgHeight * scaleFactor + yMarginPos));
}
return yPos;
}
}
答案 0 :(得分:0)
添加以下代码,以便在具有调整大小值的新页面上显示注释:
列出anotn = currentSourcePage.getAnnotations();
for(PDAnnotation an :anotn) {
PDRectangle rec = an.getRectangle();
PDRectangle currentSourcePageMBox = currentSourcePage.getMediaBox();
boolean isLandscape = currentSourcePageMBox.getWidth() > currentSourcePageMBox.getHeight();
if(isLandscape) {
rec.setLowerLeftY(((rec.getLowerLeftY()) * scaleFactor) + 180 + 2 * ypos);
rec.setUpperRightY(((rec.getUpperRightY()) * scaleFactor) + 180 + 2*ypos);
}
else {
rec.setLowerLeftY(((rec.getLowerLeftY()) * scaleFactor) + ypos);
rec.setUpperRightY(((rec.getUpperRightY()) * scaleFactor) + ypos);
}
rec.setLowerLeftX((rec.getLowerLeftX() + xpos) * scaleFactor);
rec.setUpperRightX((rec.getUpperRightX() + xpos ) * scaleFactor);
an.setRectangle(rec);
}
// get all annotations and set on the new page.
page.setAnnotations(currentSourcePage.getAnnotations());