Document.NewPage()不添加新页面 [英] Document.NewPage() not adding new page

查看:2182
本文介绍了Document.NewPage()不添加新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to add a new page to a pdf document, however for some reason this is not happening. Maybe my other question https://stackoverflow.com/questions/11428878/itextsharp-splitlate-not-working has something to do with this since the table in this question does not break and no new pages are created. This is the code I have for the adding of new pages:

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


推荐答案

只需调用 newPage( )不会添加任何空白页。

你需要让作者知道页面是空的。

Just calling a newPage() will not add any blank page.
You need to let the writer know that the page is empty.

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

Example: Refer to NewPage Example using Java. Hope the same method works for C# too.

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

这篇关于Document.NewPage()不添加新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆