简体   繁体   English

iText rotate()不会定位第一页

[英]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. 我读过的有关iText的所有内容都表示您应该能够设置页面大小然后创建新页面。 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". document.newPage()实际上意味着“完成当前页面并打开一个新页面”。 This implies that after you open() a document, you already have a blank page (with whatever size the document had set before) ready. 这意味着在open()文档之后,您已经准备好了一个空白页面(文档之前设置的大小)。

You should set your page size before opening the document: 您应该在打开文档之前设置页面大小:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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