1

我正在尝试向 pdf 文档添加一个新页面,但是由于某种原因这没有发生。也许我的另一个问题https://stackoverflow.com/questions/11428878/itextsharp-splitlate-not-working与此有关,因为此问题中的表格没有中断,也没有创建新页面。这是我用于添加新页面的代码:

Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4.Rotate(),20,20,20,40);
string rep1Name;                 // variable to hold the file name of the first part of the report
rep1Name = Guid.NewGuid().ToString() + ".pdf";

FileStream output = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/ReportGeneratedFiles/reports/" + rep1Name), FileMode.Create);
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, output);

doc.Open();
doc.NewPage();
doc.NewPage();
doc.Close();
4

1 回答 1

4

只是调用 anewPage()不会添加任何空白页。
您需要让作者知道该页面是空的。

示例:请参阅使用 Java的NewPage 示例。希望同样的方法也适用于 C#。

public class PdfNewPageExample
{
    // throws DocumentException, FileNotFoundException
    public static void main( String ... a ) throws Exception
    {
        String fileHome = System.getProperty( "user.home" ) + "/Desktop/";
        String pdfFileName = "Pdf-NewPage-Example.pdf";

        // step 1
        Document document = new Document();
        // step 2
        FileOutputStream fos = new FileOutputStream( fileHome + pdfFileName );
        PdfWriter writer = PdfWriter.getInstance( document, FileOutputStream );
        // 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();

        System.out.println( "Done ..." );
    } // psvm( .. )
} // class PdfNewPageExample
于 2012-07-12T17:43:56.407 回答