itext pdf PageSize.LEGAL_LANDSCAPE.rotate()无法打印

时间:2015-03-17 06:47:59

标签: itext

我创建了一个程序来编写有两页的pdf,第一页是肖像,第二页是横向。它会创建pdf,但是当我打印该文件时,它不会打印第二页,即横向页面。

以下是我的代码

/******************/  

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class TestPDF {
    public static void main(String args[]) throws DocumentException, FileNotFoundException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("/home/devang/test.pdf"));
        document.setMargins(10.0f, 10.0f, 20.0f, 2.0f);
        document.open();
        //PAGE1
        addFirstPage(document);
        //PAGE2
        addSecondPage(document);
        document.close();
    }
    public static Document addFirstPage(Document document) throws DocumentException {
        document.addTitle("Test PDF");
        Paragraph paragraph = new Paragraph();
        paragraph.setAlignment(Element.ALIGN_CENTER);
        paragraph.add("Page 1");
        paragraph.add("\nPage 1");
        paragraph.add("\nPage 1");
        paragraph.add("\nPage 1");
        paragraph.add("\nPage 1");
        document.add(paragraph);
        return document;
    }
    public static Document addSecondPage(Document document) throws DocumentException {
        document.setPageSize(PageSize.LEGAL_LANDSCAPE.rotate());
        document.newPage();
        document.addTitle("Test PDF");
        Paragraph paragraph = new Paragraph();
        paragraph.setAlignment(Element.ALIGN_CENTER);
        paragraph.add("Page 2");
        paragraph.add("\nPage 2");
        paragraph.add("\nPage 2");
        paragraph.add("\nPage 2");
        paragraph.add("\nPage 2");
        document.add(paragraph);
        return document;
    }
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

用以下代码替换您的代码:

Rectangle a4 = PageSize.A4;
Rectangle a4Landscape = a4.rotate();
document.setPageSize(a4Landscape);