java itext将文档缩放为a4

qrjkbowd  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(859)

我使用以下方法将文档的所有页面“调整大小”为a4页面尺寸:

for (PdfDocument doc : pdfDocuments) {
        int n = doc.getNumberOfPages();

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

            PdfPage page = doc.getPage(i);

            Rectangle media = page.getCropBox();
            if (media == null) {
                media = page.getMediaBox();
            }

            Rectangle crop = new Rectangle(0, 0, 210, 297);
            page.setMediaBox(crop);
            page.setCropBox(crop);

            // The content, placed on a content stream before, will be rendered before the other content
            // and, therefore, could be understood as a background (bottom "layer")
            new PdfCanvas(page.newContentStreamBefore(),
                    page.getResources(), doc).writeLiteral("\nq 0.5 0 0 0.5 0 0 cm\nq\n");

            // The content, placed on a content stream after, will be rendered after the other content
            // and, therefore, could be understood as a foreground (top "layer")
            new PdfCanvas(page.newContentStreamAfter(),
                    page.getResources(), doc).writeLiteral("\nQ\nQ\n");
        }
    }

但是,这并没有像预期的那样工作,页面正在转换为a4(297x210),但是内容没有安装在内部(缩放),因为原始页面大于297x210,所以内容看起来被剪切了。我怎样才能解决这个问题?

brccelvz

brccelvz1#

在你澄清的评论中
我希望前一个内容的边界框被缩放,并在目标中添加一个边距
因此,我们首先要确定原始页面内容的边界框。可以使用 MarginFinder 从这个答案开始上课。注意:该类决定了所有内容的边界框,即使它只是一个白色的矩形,视觉上与没有内容或以前在裁剪框之外的东西没有区别。。。如果您的用例需要它,您可能也必须扩展该类来考虑这些情况。
确定了内容边界框后,剩下的就是一点计算。
下面的方法使用上面的类确定边界框,相应地变换内容,并更改结果裁剪框。

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 %s\n", 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);
    }
}

(比例法) scale )
对于一个a4的结果页大小,每边有一英寸的边距,你可以这样称呼一个页面 PdfDocument pdfDocument :

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

(摘自scaletoa4测试) testFdaRequiresUseOfEctdFormatAndStandardizedStudyDataInFutureRegulatorySubmissionsSept )

相关问题