Dwhitz
Dwhitz

Reputation: 1270

I can't rotate my page from existing PDF

I've just made a simple Java Console Project, which writes content to an existent PDF, but I can't rotate the page. I've already tried to rotate the page trying many examples found here on stackoverflow, but nothing works for me.

My Class

public class PDfRotate{

    private static String workingDir = System.getProperty("user.dir");
    private static String FILERIN = workingDir + "/Rin.pdf";
    private static String FILERIN_INPUT = workingDir + "/Test.pdf";
    private static String FILERIN_OUTPUT = workingDir + "/RinOutput.pdf";
    private static Document document;

    public static void main(String[] args) throws IOException, DocumentException {

        Document document = new Document();
        rotatePdf(document);
        document.close();               
    }

    public static void rotatePdf(Document document) throws DocumentException, IOException{

        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILERIN_OUTPUT));

        document.open();
        Paragraph par = new Paragraph("hello");
        document.add(par);

        document.add(PageSize.A4.rotate());

        PdfReader reader = new PdfReader(FILERIN);
        PdfImportedPage page = writer.getImportedPage(reader,1);
        Image instance = Image.getInstance(page);
        document.add(instance);
    }

Upvotes: 0

Views: 1752

Answers (2)

Dwhitz
Dwhitz

Reputation: 1270

Solved by myself.

I should set the rotation before opening the document.

document.setPageSize(PageSize.A4.rotate());
document.open();
....
document.close();

Upvotes: 1

mkl
mkl

Reputation: 95898

Rotating the pages by 90°

At first I understood the question and comments to be about rotating the document pages (including their content) by 90°.

This is demonstrated in the iText sample RotatePages.java from chapter 13 of iText in Action - Second Edition. The focal code:

PdfReader reader = new PdfReader(SOURCE);
int n = reader.getNumberOfPages();
int rot;
PdfDictionary pageDict;
for (int i = 1; i <= n; i++) {
    rot = reader.getPageRotation(i);
    pageDict = reader.getPageN(i);
    pageDict.put(PdfName.ROTATE, new PdfNumber(rot + 90));
}
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
stamper.close();
reader.close();

To this the OP commented that he didn't want rotate the content, only the page. Thus:

Switching portrait to landscape and vice versa

To switch between landscape and portrait one can do something like the following:

PdfReader reader = new PdfReader(SOURCE);
int n = reader.getNumberOfPages();
PdfDictionary pageDict;
for (int i = 1; i <= n; i++) {
    Rectangle rect = reader.getPageSize(i);
    Rectangle crop = reader.getCropBox(i);
    pageDict = reader.getPageN(i);
    pageDict.put(PdfName.MEDIABOX, new PdfArray(new float[] {rect.getBottom(), rect.getLeft(), rect.getTop(), rect.getRight()}));
    pageDict.put(PdfName.CROPBOX, new PdfArray(new float[] {crop.getBottom(), crop.getLeft(), crop.getTop(), crop.getRight()}));
}
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
stamper.close();
reader.close();

(SwitchPageCanvas)

This obviously will cut away some content.

Upvotes: 3

Related Questions