简体   繁体   中英

iText rotate() won't orient the first page

Everything I have read regarding iText says you should be able to set the page size and then create a new page. But for some reason when I try this my first page isn't rotated. But my second is. Any ideas?

response.setContentType("application/pdf");
Document document = new Document();

try{
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    PdfWriter.getInstance(document, buffer); 
    document.open();
    //Start a new page
    document.setPageSize(PageSize.LETTER.rotate()); //  11" x 8.5"  new Rectangle(792f, 612f)

    document.newPage();
    Paragraph topText = new Paragraph();
    // add some content here...
    document.close(); 

    DataOutput dataOutput = new DataOutputStream(response.getOutputStream());
    byte[] bytes = buffer.toByteArray();
    response.setContentLength(bytes.length);

    for(int i = 0; i < bytes.length; i++) {
        dataOutput.writeByte(bytes[i]);
    }

} catch (DocumentException e) {
    e.printStackTrace();
}

document.newPage() really means "finish the current page and open a new one". This implies that after you open() a document, you already have a blank page (with whatever size the document had set before) ready.

You should set your page size before opening the document:

document.setPageSize(PageSize.LETTER.rotate());
document.open();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM