C# 将页码添加到Pdf文档
如果pdf的内容不能全部放在一个页面上,如何将pdf的内容扩展到下一页。目前我正在创建A4格式的PdfC# 将页码添加到Pdf文档,c#,asp.net,abcpdf,C#,Asp.net,Abcpdf,如果pdf的内容不能全部放在一个页面上,如何将pdf的内容扩展到下一页。目前我正在创建A4格式的Pdf 另外,我如何指定页数,例如右下角的第1页,共12页 abc pdf用于将HTML页面转换为pdf,将您的内容设置为两个HTML页面,它将生成两个pdf页面。有关更多详细信息,您可以在左侧的“内容”中查看,单击示例,然后选择“页面HTML示例” 要将文本添加到PDF文档中,并让其在文本不适合的情况下创建新页面,可以使用以下代码 theID = theDoc.AddHtml(theText) Wh
另外,我如何指定页数,例如右下角的第1页,共12页 abc pdf用于将HTML页面转换为pdf,将您的内容设置为两个HTML页面,它将生成两个pdf页面。有关更多详细信息,您可以在左侧的“内容”中查看,单击示例,然后选择“页面HTML示例” 要将文本添加到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());
}
Doc Doc=new Doc();
doc.Page=doc.AddPage();
int id=doc.AddImageUrl(“http://www.google.com/“,对,700,对);
while(true)
{
如果(!文档可链接(id))
打破
doc.Page=doc.AddPage();
id=文件AddImageToChain(id);
}
doc.Font=doc.AddFont(“Arial”);
doc.FontSize=9;
对于(int i=1;i您需要做的是首先确保您有一个自动扩展到所需大小的文档,下面的c#示例将采用URL并构建一个最多50页的文档,如果需要,请进行扩展。(下面的示例在文档中为页眉和页脚添加空间)
问题标记为C#Valid point@Askolein。我将更新我的答案。只晚了2年:(
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;
}