2012-07-11 156 views
1

我想添加一個新的頁面到PDF文檔,但由於某種原因,這沒有發生。也許我的其他問題https://stackoverflow.com/questions/11428878/itextsharp-splitlate-not-working與此有關,因爲這個問題中的表沒有中斷,也沒有創建新的頁面。 這是我對新的頁面添加代碼:Document.NewPage()不添加新頁面

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

回答

3

只需調用newPage()絕不添加任何空白頁。
您需要讓作者知道該頁面是空的。

示例:參照NewPage Example使用Java。希望同樣的方法也適用於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 
+0

非常感謝!它現在的作品! – user1517150 2012-07-14 11:19:00