4

たとえば、25ページのpdfドキュメントがあります。10 ページと 11 ページの間に空白ページを 1 ページ追加するにはどうすればよいですか?

4

3 に答える 3

10

Google での最初のヒット:

/*
 * 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();

    }
}
于 2012-06-25T07:40:12.967 に答える
8

を使用した後document.newPage();、コンテンツを追加しないと無視されます。したがって、空白のページが必要な場合は、 をwriter.setPageEmpty(false);呼び出した直後に追加しnewPage()ます。

于 2012-06-25T07:42:34.167 に答える
3

次の PdfWriter のメソッドを見てください。

http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfWriter.html#setPageEmpty(ブール値)

次のように動作するはずです:

Document doc = new Document();
PdfWriter pdfWriter
        = PdfWriter.getInstance(document, new FileOutputStream("file.pdf"));
pdfWriter.setPageEmpty(false);
doc.newPage();
doc.close();

ページが空であっても空ではないことをライターに伝えると、新しいページが作成されます。

于 2012-06-25T07:46:01.403 に答える