JavaiTextスケールドキュメントをA4に

Aug 20 2020

ドキュメントのすべてのページを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より大きいため、コンテンツは切り取られて表示されます。どうすればこれを修正できますか?

回答

1 mkl Aug 24 2020 at 13:47

コメントであなたは明確にしました

以前のコンテンツのバウンディングボックスを拡大縮小し、ターゲットにマージンを追加したい

したがって、最初に元のページコンテンツのバウンディングボックスを決定する必要があります。これは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);
    }
}

(ScaleToA4メソッドscale

両側に1インチの余白があるA4結果ページサイズの場合、PdfDocument pdfDocument次のように呼び出すことができます。

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

(ScaleToA4テストからの抜粋testFdaRequiresUseOfEctdFormatAndStandardizedStudyDataInFutureRegulatorySubmissionsSept