2012-06-25 88 views

回答

8

在谷歌第一擊:

/* 
* This class is part of the book "iText in Action - 2nd Edition" 
* written by Bruno Lowagie (ISBN: 9781935182610) 
* For more info, go to: http://itextpdf.com/examples/ 
* This example only works with the AGPL version of iText. 
*/ 

package part1.chapter05; 

import java.io.FileOutputStream; 
import java.io.IOException; 

import com.itextpdf.text.Document; 
import com.itextpdf.text.DocumentException; 
import com.itextpdf.text.Paragraph; 
import com.itextpdf.text.pdf.PdfWriter; 

public class NewPage { 

/** Path to the resulting PDF file. */ 
public static final String RESULT 
    = "results/part1/chapter05/new_page.pdf"; 

/** 
* Main method creating the PDF. 
* @param args no arguments needed 
* @throws IOException 
* @throws DocumentException 
*/ 
public static void main(String[] args) throws IOException, DocumentException { 
    // step 1 
    Document document = new Document(); 
    // step 2 
    PdfWriter writer 
     = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); 
    // step 3 
    document.open(); 
    // step 4 
    document.add(new Paragraph("This page will NOT be followed by a blank page!")); 
    document.newPage(); 
    // we don't add anything to this page: newPage() will be ignored 
    document.newPage(); 
    document.add(new Paragraph("This page will be followed by a blank page!")); 
    document.newPage(); 
    writer.setPageEmpty(false); 
    document.newPage(); 
    document.add(new Paragraph("The previous page was a blank page!")); 
    // step 5 
    document.close(); 

    } 
} 
+0

+1你找到了正確的。 –

5

使用後,document.newPage();,它也會在不添加任何內容忽略。因此,如果您需要空白頁面,請在致電newPage()後立即添加writer.setPageEmpty(false);

+0

我喜歡這個答案最好,因爲它很短,也很重要。 –

相關問題