Java iText没有关联的PdfWriter用于制作间接指令



我有以下方法将图像缩放为A4

void scale(PdfDocument pdfDocument, Rectangle pageSize, Rectangle pageBodySize) {
int n = pdfDocument.getNumberOfPages();
for (int i = 1; i <= n; i++) {
PdfPage page = pdfDocument.getPage(i);
MarginFinder marginFinder = new MarginFinder();
PdfCanvasProcessor pdfCanvasProcessor = new PdfCanvasProcessor(marginFinder);
pdfCanvasProcessor.processPageContent(page);
Rectangle boundingBox = marginFinder.getBoundingBox();
if (boundingBox == null || boundingBox.getWidth() == 0 || boundingBox.getHeight() == 0) {
System.err.printf("Cannot scale page %d contents with bounding box %sn", i, boundingBox);
continue;
} else {
// Scale and move content into A4 with margin
double scale = 0, xDiff = 0, yDiff = 0;
double xScale = pageBodySize.getWidth() / boundingBox.getWidth();
double yScale = pageBodySize.getHeight() / boundingBox.getHeight();
if (xScale < yScale) {
yDiff = boundingBox.getHeight() * (yScale / xScale - 1) / 2;
scale = xScale;
} else {
xDiff = boundingBox.getWidth() * (xScale / yScale - 1) / 2;
scale = yScale;
}
AffineTransform transform = AffineTransform.getTranslateInstance(pageBodySize.getLeft() + xDiff, pageBodySize.getBottom() + yDiff);
transform.scale(scale, scale);
transform.translate(-boundingBox.getLeft(), -boundingBox.getBottom());
new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDocument)
.concatMatrix(transform);
}
page.setMediaBox(pageSize);
page.setCropBox(pageSize);
}
}

此方法在以下代码段中调用:

ByteArrayOutputStream mergedPdfStream = new ByteArrayOutputStream();
PdfDocument mergedPdf = new PdfDocument(new PdfWriter(mergedPdfStream));
for (PdfDocument doc : pdfDocuments) {
int n = doc.getNumberOfPages();

for (int i = 1; i <= n; i++) {

PdfPage page = doc.getPage(i).copyTo(mergedPdf);
mergedPdf.addPage(page);

}
Rectangle pageSize = PageSize.A4;
Rectangle pageBodySize = pageSize.clone().applyMargins(72, 72, 72, 72, false);
scale(doc, pageSize, pageBodySize);
}

mergedPdf.close();

然而,当它在结束时执行此代码时

new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDocument)
.concatMatrix(transform);

它抛出一个PDF异常没有关联的PdfWriter用于制作间接内容:

com.itextpdf.kernel.PdfException: There is no associate PdfWriter for making indirects.
at com.itextpdf.kernel.pdf.PdfObject.makeIndirect(PdfObject.java:228) ~[kernel-7.1.1.jar:?]
at com.itextpdf.kernel.pdf.PdfObject.makeIndirect(PdfObject.java:248) ~[kernel-7.1.1.jar:?]
at com.itextpdf.kernel.pdf.PdfPage.newContentStream(PdfPage.java:1186) ~[kernel-7.1.1.jar:?]
at com.itextpdf.kernel.pdf.PdfPage.newContentStreamBefore(PdfPage.java:244) ~[kernel-7.1.1.jar:?]

如何解决此问题?我做错什么了吗?

您调用

scale(doc, pageSize, pageBodySize);

对于CCD_ 2中的每个CCD_。因此在将页面复制到mergedPdf之后,您可以尝试缩放源文档中的页面。您无法缩放源PdfDocument实例中的页面,因为它们以只读方式打开(即仅使用PdfReader,不使用PdfWriter(,并且在复制后执行操作也没有意义,因为您肯定希望缩放结果mergedPdf中的内容,而不是源中的内容。

因此,移动块

Rectangle pageSize = PageSize.A4;
Rectangle pageBodySize = pageSize.clone().applyMargins(72, 72, 72, 72, false);
scale(doc, pageSize, pageBodySize);

从外部for回路主体中取出,在其之后,并用替换最后一行

scale(mergedPdf, pageSize, pageBodySize);

以缩放得到的合并PDF。

相关内容

  • 没有找到相关文章

最新更新