2012-10-10 119 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; 
    }