2012-10-10 156 views
2

如果pdf的內容不能全部適合單頁,我如何將pdf的內容擴展到下一頁。目前我正在創建PDF格式爲A4。將頁碼添加到PDF文檔中

另外我怎樣才能指定頁面的數量,例如右下角第1頁,共12頁。

回答

0

abc pdf用於將HTML頁面轉換爲pdf,將您的內容設置爲兩個html頁面,它將在PDF中生成兩個頁面。有關更多詳細信息,您可以在左側查看This,在此處有「內容」點擊示例然後選擇「分頁的HTML示例」。

4

要添加文本到PDF文檔,並讓它創建新的頁面,如果文本不適合您可以使用下面的代碼。

theID = theDoc.AddHtml(theText) 
While theDoc.Chainable(theID) 
    theDoc.Page = theDoc.AddPage() 
    theDoc.FrameRect 
    theID = theDoc.AddHtml("", theID) 
Wend 

要添加您的頁碼和頁數到每個頁面使用此。

theDoc.Rect = "100 50 500 150" 'position of page number 
For i = 1 To theDoc.PageCount 
    theDoc.PageNumber = i 
    theDoc.AddText i & "/" & theDoc.PageCount 
Next 

編輯:C#版本

Doc doc = new Doc(); 
doc.Page = doc.AddPage(); 
int id = doc.AddImageUrl("http://www.google.com/", true, 700, true); 
while (true) 
{ 
    if (!doc.Chainable(id)) 
     break; 
    doc.Page = doc.AddPage(); 
    id = doc.AddImageToChain(id); 
} 

doc.Font = doc.AddFont("Arial"); 
doc.FontSize = 9; 
for (int i = 1; i <= doc.PageCount; i++) 
{ 
    doc.PageNumber = i; 
    doc.Rect.String = "470 55 570 65"; 
    doc.HPos = 1; 
    doc.AddText("Page " + i.ToString() + " of " + doc.PageCount.ToString()); 
} 
+0

問題標記爲C# – Askolein

+1

有效點@Askolein。我會更新我的答案。只有2年太晚:( – johna

2

你需要做的就是首先要確保你有一個將會被自動擴展到所需的大小的文件是什麼,在C#下面的例子將採取URL並建立一個多達50頁的文件,如果需要擴大。 (下面的示例將空間中的文檔中頁眉和頁腳)

private static Doc CreateNewDoument(string currentURL) 
     { 
      var theDoc = new Doc(); 

      theDoc.MediaBox.String = "A4"; 

      theDoc.HtmlOptions.PageCacheEnabled = false; 
      theDoc.HtmlOptions.ImageQuality = 101; 
      theDoc.Rect.Width = 719; 
      theDoc.Rect.Height = 590; 
      theDoc.Rect.Position(2, 70); 
      theDoc.HtmlOptions.Engine = EngineType.Gecko; 

      // Add url to document.);); 
      try 
      { 
       //Make sure we dont have a cached page.. 
       string pdfUrl = currentURL+ "&discache=" + DateTime.Now.Ticks.ToString(); 

       int theID = theDoc.AddImageUrl(pdfUrl); 
       //Add up to 50 pages 
       for (int i = 1; i <= 50; i++) 
       { 
        if (!theDoc.Chainable(theID)) 
         break; 
        theDoc.Page = theDoc.AddPage(); 
        theID = theDoc.AddImageToChain(theID); 
       } 
       theDoc.PageNumber = 1; 
      } 
      catch (Exception ex) 
      { 
       //HttpContext.Current.Response.Redirect(pdCurrentURL); 

       throw new ApplicationException("Error generating pdf..." + "Exception: " + ex + "<br/>URL for render: " + pdfUrl+ "<br/>Base URL: " + currentURL); 
      } 

      return theDoc; 
     } 

然後到頁腳添加到每個頁面只是使用下面的方法。下面的方法在裏面添加一個藍色框和文本。

private static Doc AddFooter(Doc theDoc) 
    { 
     int theCount = theDoc.PageCount; 
     int i = 0; 
     for (i = 1; i <= theCount; i++) 
     { 
      theDoc.Rect.String = "20 15 590 50"; 
      theDoc.Rect.Position(13, 30); 
      System.Drawing.Color c = System.Drawing.ColorTranslator.FromHtml("#468DCB"); 
      theDoc.Color.Color = c; 
      theDoc.PageNumber = i; 
      theDoc.FillRect(); 

     } 
     i = 0; 
     for (i = 1; i <= theCount; i++) 
     { 
      theDoc.Rect.String = "20 15 260 50"; 
      theDoc.Rect.Position(190, 20); 
      System.Drawing.Color cText = System.Drawing.ColorTranslator.FromHtml("#ffffff"); 
      theDoc.Color.Color = cText; 
      string theFont = "Century Gothic"; 
      theDoc.Font = theDoc.AddFont(theFont); 
      theDoc.FontSize = 17; 
      theDoc.PageNumber = i; 
      theDoc.AddText("Page " + i +" of " +theCount); //Setting page number 
      //theDoc.FrameRect(); 
     } 
     return theDoc; 
    } 

然後,只需調用一大堆..像

 private static bool BuildPDF(string pdfPath) 
    { 
     bool pdfBuilt = false; 

     try 
     { 
      var theDoc = new Doc(); 

      string pdGeneral = "http://ww.myurl.com"; 
      theDoc = CreateNewDoument(pdGeneral); 

      theDoc = AddFooter(theDoc); 

      theDoc.Save(pdfPath); 
      theDoc.ClearCachedDecompressedStreams(); 
      theDoc.Clear(); 
      theDoc.Dispose(); 

      pdfBuilt = true; 
     } 
     catch (Exception) 
     { 
      //PDF normaly in use dont worry.. 
     } 

     return pdfBuilt; 
    }