After resize document, new content have as same size. How save new size in ByteArrayOutputStream?

mflorczak :

I have problem with size document. I pass vertical orientation(595.0x842.0) pdf and I change orientation pdf to horizontal(842.0x595.0) but When I return ByteArrayOutputStream as ByteArray and I try read this ByteArray I get vertical orientation(595.0x842.0) instead horizontal(842.0x595.0)

To write new content I use PdfWriter and I send Document with new size and ByteArrayOutputStream as second parametr.

PdfReader reader = new PdfReader(pdf.getFile());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Document document = new Document(PageSize.A4.rotate(), 0, 0, 0, 0);
PdfWriter writer = PdfWriter.getInstance(document, outputStream);

I expect the output horizontal(842.0x595.0) instead (595.0x842.0)

I need to this dimension in this code:

PdfReader reader = new PdfReader(pdf.getFile());
            int n = reader.getNumberOfPages();
            PdfStamper stamper = new PdfStamper(reader,outputStream);
            PdfContentByte pageContent;
            for (int i = 0; i < n;) {
                pageContent = stamper.getOverContent(++i);
                System.out.println(reader.getPageSize(i));
                ColumnText.showTextAligned(pageContent, Element.ALIGN_RIGHT,
                        new Phrase(String.format("page %s of %s", i, n)), 
                            reader.getPageSize(i).getWidth()- 20, 20, 0);
            }

This variable pdf.getFile() is a ByteArray(ByteArrayOutputStream ) from previous code. I expect page number in right down corner(x point 842) but actual is 595. For better understand I send screenshot.

enter image description here

mkl :

You use the PdfReader method getPageSize but you should use getPageSizeWithRotation, i.e. you should replace

System.out.println(reader.getPageSize(i));
ColumnText.showTextAligned(pageContent, Element.ALIGN_RIGHT,
        new Phrase(String.format("page %s of %s", i, n)), 
            reader.getPageSize(i).getWidth()- 20, 20, 0);

by

System.out.println(reader.getPageSizeWithRotation(i));
ColumnText.showTextAligned(pageContent, Element.ALIGN_RIGHT,
        new Phrase(String.format("page %s of %s", i, n)), 
            reader.getPageSizeWithRotation(i).getWidth()- 20, 20, 0);

The Backgrounds

There are two properties of a page responsible for the final displayed page dimension: MediaBox and Rotate. (Let's for the moment ignore the crop box and all the other boxes which also exist.)

A landscape A4 page, therefore, can be created in two conceptually different ways, either as a 842x595 media box with 0° (or 180°) rotation or as a 595x842 media box with 90° (or 270°) rotation.

If you instantiate a Document with PageSize.A4.rotate(), iText uses the latter way. (If you had instantiated it with new RectangleReadOnly(842,595), iText would have used the former way.)

PdfReader.getPageSize only inspects the media box. Thus, it returns a rectangle with width 595 for your PDF page with 595x842 media box and 90° rotation.

PdfReader.getPageSizeWithRotation also inspects the page rotation. Thus, it returns a rectangle with width 842 for your PDF page with 595x842 media box and 90° rotation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=123085&siteId=1