Document.NewPage() not adding new page

时光怂恿深爱的人放手 提交于 2019-12-31 04:28:26

问题


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

回答1:


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

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


来源:https://stackoverflow.com/questions/11430019/document-newpage-not-adding-new-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!