如果 pdf 的内容不能全部放在一个页面上,我如何将 pdf 的内容扩展到下一页。目前我正在将 Pdf 创建为 A4。
另外,我如何指定页数,例如右下角的第 1 页,共 12 页。
要将文本添加到 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());
}
您需要做的是首先确保您有一个会自动扩展为所需大小的文档,下面的 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;
}
abc pdf 用于将 HTML 页面转换为 pdf,将您的内容设置为两个 html 页面,它将生成两个 PDF 页面。有关更多详细信息,您可以在左侧查看“ 内容”,单击“示例”,然后选择“分页 HTML 示例”。