Oscar
Oscar

Reputation: 11

itext how to rescale A3 size pdf with content to A4 size

as titled, I m trying to rescale A3 size pdf to A4 size, which I can do that. But after that, the content seems still in A3 scale, but only the pager size changed. What I mean is that the content is overflow outside the A4 size pager, and it's not shrinked.

This is my codes:

Document document = new Document(PageSize.A4, 0,0,0,0);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("output file.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfReader reader = new PdfReader("input file.pdf");
for (int i = 1; i <= reader.getNumberOfPages(); i++){
     PdfImportedPage page = writer.getImportedPage(reader, i);            
     document.newPage();
     cb.addTemplate(page,0,0);
}
reader.close();
document.close();

Can anyone advise to solve this problem, THANK YOU.

Upvotes: 1

Views: 4326

Answers (2)

e.ayoubi
e.ayoubi

Reputation: 1

you should use an this Code, which allows you to scale down the page

            PdfDocument resultantDocument = new PdfDocument(writer)); 
            resultantDocument.SetDefaultPageSize(PageSize.A4);

            pdfDocument = new PdfDocument(new PdfReader(TempMailAsPDFA3));
            for (int i = 1; i <= pdfDocument.GetNumberOfPages(); i++)
            {
                PdfPage page = pdfDocument.GetPage(i);
                PdfFormXObject formXObject = page.CopyAsFormXObject(resultantDocument);

                PdfCanvas pdfCanvas = new PdfCanvas(resultantDocument.AddNewPage(PageSize.A4));
                // 3a and 3b
                pdfCanvas.AddXObjectWithTransformationMatrix(formXObject, 0.7f, 0, 0, 0.7f, 0, 0);
            }
            resultantDocument.Close();
            pdfDocument.Close();

Upvotes: 0

mkl
mkl

Reputation: 95918

It's not shrinked because you don't tell it to shrink.

You add the A3 page to the new PDF using

cb.addTemplate(page,0,0);

This method adds the page as is at coordinates 0, 0.

Instead you should use an overload of that method which allows you to scale down the page in the new PDF, e.g.

cb.addTemplate(page, 0.5, 0, 0, 0.5, 0, 0);

or

cb.addTemplate(page, AffineTransform.getScaleInstance(0.5, 0.5));

PS: 0.5 above most likely is incorrect, it scales down from A3 to A5. Most likely it should be the square root of 0.5 to scale down from A3 to A4.

Upvotes: 3

Related Questions