java IText:如何在 pdf 中添加空白页?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11185172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:07:09  来源:igfitidea点击:

IText: How to add blank page inside pdf?

javaitext

提问by marioosh

I have pdf document, for example 25 pages. How to add one blank page beetwen page 10 and 11 ?

我有 pdf 文档,例如 25 页。如何在第 10 页和第 11 页之间添加一个空白页?

回答by Viezevingertjes

First hit on 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();

    }
}

回答by Chandra

After you use, document.newPage();, it will be ignored if you don't add any content. So, if you need blank page, then add writer.setPageEmpty(false);right after calling newPage().

使用后,document.newPage();如果不添加任何内容,将被忽略。因此,如果您需要空白页,请writer.setPageEmpty(false);在调用newPage().

回答by megglos

Just have a look at the following method of PdfWriter:

看看PdfWriter的以下方法:

http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfWriter.html#setPageEmpty(boolean)

http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfWriter.html#setPageEmpty(boolean)

Should work like so:

应该像这样工作:

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

You tell the writer that the page is not empty, even though it is, so a new page will be created.

您告诉作者该页面不是空的,即使它是空的,因此将创建一个新页面。