将页码添加到Pdf文档

时间:2012-10-10 10:17:54

标签: c# asp.net abcpdf

如果pdf的内容并非全部适合单个页面,我如何将pdf的内容扩展到下一页。目前我正在创建Pdf作为A4。

另外,我如何指定页数,例如第1页,共12页右下角。

3 个答案:

答案 0 :(得分:5)

要将文本添加到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());
 }

答案 1 :(得分: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;
    }

答案 2 :(得分:0)

abc pdf用于将HTML页面转换为pdf,将您的内容设置为两个html页面,它将以PDF格式生成两个页面。有关详细信息,您可以在左侧查看This这里有“内容”点击示例,然后选择“分页HTML示例”。