जावा iText स्केल दस्तावेज़ 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) में परिवर्तित हो रहे हैं लेकिन BUT सामग्री को अंदर (परिमार्जित) नहीं किया जा रहा है, सामग्री कटी हुई दिखाई देती है क्योंकि मूल पृष्ठ 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)

ए 4 परिणाम पृष्ठ आकार के लिए प्रत्येक तरफ मार्जिन के एक इंच के साथ आप इसे इस तरह से कॉल कर सकते हैं PdfDocument pdfDocument:

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

( ScaleToA4 परीक्षण से अंश testFdaRequiresUseOfEctdFormatAndStandardizedStudyDataInFutureRegulatorySubmissionsSept)