【问题标题】:PDFsharp: Is there a way to generate "Page X of Y" in the header of the page?PDFsharp:有没有办法在页面标题中生成“Y 页 X”?
【发布时间】:2013-10-21 15:36:02
【问题描述】:

看起来很简单,但我在 API 中找不到类似 getPageCount() 的东西。我可以让它返回当前页面,但不能返回总页数。也许我错过了?

我希望能够以某种方式在每一页的顶部打印“第 1 页,共 9 页”,其中“1”当然是当前页码。

【问题讨论】:

  • 你能显示一些代码吗?

标签: c# pdfsharp migradoc


【解决方案1】:

确保在您的课程中包含using MigraDoc.DocumentObjectModel; 语句。

Document document = new Document();
Section section = document.AddSection();

Paragraph paragraph = new Paragraph();
paragraph.AddText("Page ");
paragraph.AddPageField();
paragraph.AddText(" of ");
paragraph.AddNumPagesField();

section.Headers.Primary.Add(paragraph);

【讨论】:

  • 这是什么MigraDoc,我在哪里可以得到它?我正在使用PdfSharp,就像问题所述。我没有Document 只有PdfDocument
  • @sLw MigraDoc 是 PDFsharp 之上的一个层,它提供了一种面向对象的方法来创建 PDF。简化 PDF 设计 IMO。有关更多信息,请参阅 pdfsharp.net。
【解决方案2】:

使用 PDFsharp 由您决定。

我假设您正在使用 MigraDoc:使用 MigraDoc,您可以添加页眉。为当前页码添加paragraph.AddPageField(),为总页数添加paragraph.AddNumPagesField()

Sample that uses AddPageField

示例中的代码 sn-p:

// Create a paragraph with centered page number. See definition of style "Footer".
Paragraph paragraph = new Paragraph();
paragraph.AddTab();
paragraph.AddPageField();

// Add paragraph to footer for odd pages.
section.Footers.Primary.Add(paragraph);
// Add clone of paragraph to footer for odd pages. Cloning is necessary because an object must
// not belong to more than one other object. If you forget cloning an exception is thrown.
section.Footers.EvenPage.Add(paragraph.Clone());

设置制表位的代码 sn-p(假设 DIN A 4,主体为 16 厘米):

style = document.Styles[StyleNames.Footer]; 
style.ParagraphFormat.AddTabStop("8cm", TabAlignment.Center); 

两个 sn-ps 均取自链接站点。示例代码也可供下载。

【讨论】:

  • 此页面上显示的示例效果不佳。我的意思是页码不会改变。
  • @Marek Bar: AddNumPagesField 添加文档中的页数(并且不会在页面之间更改),AddPageField 添加当前页码并逐页更改。
  • @PDFsharpTeam...如果我只想在 PDF 超过一页的情况下显示页码...我应该怎么做?现在,即使我总共只有一页,我的 PDF 也会显示“Pag: 1”。
  • @Romias:在第一次运行中,为最可能的情况创建文档(例如,如果单页比多页更有可能,则没有页脚)。如果 PDF 不符合预期(例如超过一页),则将其丢弃并创建一个带页脚的新文档。或者总是使用页脚创建它,如果它是单页,则使用 PDFsharp 在页脚上绘制一个白色矩形(这是一个 hack)。或者总是在没有页脚的情况下创建它,并在需要时使用 PDFsharp 来绘制页脚。
  • 完美运行:)
【解决方案3】:

我知道这个问题很老,并且有一个公认的答案,但是在搜索 PDFsharp 解决方案时,这个问题出现在第一个问题中。

为了记录,在 PDFsharp 中实现这一点很容易。在 PdfSharp.Pdf 命名空间下的 PdfDocument 类包含页面集合 (PdfDocument.Pages)。您所要做的就是遍历集合并在每个页面的某处添加页面计数器,使用XGraphics 对象,您可以使用XGraphics.FromPdfPage(PdfPage) 对其进行实例化。

using PdfSharp.Pdf; // PdfDocument, PdfPage
using PdfSharp.Drawing; // XGraphics, XFont, XBrush, XRect
                        // XStringFormats

// Create a new PdfDocument.
PdfDocument document = new PdfDocument();
// Add five pages to the document.
for(int i = 0; i < 5; ++i)
    document.AddPage();

// Make a font and a brush to draw the page counter.
XFont font = new XFont("Verdana", 8);
XBrush brush = XBrushes.Black;

// Add the page counter.
string noPages = document.Pages.Count.ToString();
for(int i = 0; i < document.Pages.Count; ++i)
{
    PdfPage page = document.Pages[i];

    // Make a layout rectangle.
    XRect layoutRectangle = new XRect(0/*X*/, page.Height-font.Height/*Y*/, page.Width/*Width*/, font.Height/*Height*/);

    using (XGraphics gfx = XGraphics.FromPdfPage(page))
    {
        gfx.DrawString(
            "Page " + (i+1).ToString() + " of " + noPages,
            font,
            brush,
            layoutRectangle,
            XStringFormats.Center);
    }
}

值得注意的是,如果给定页面已经存在 XGraphics 对象,则在创建新页面之前,需要释放旧页面。这会失败:

PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();

XGraphics gfx1 = XGraphics.FromPage(page);
XGraphics gfx2 = XGraphics.FromPage(page);

【讨论】:

  • 你拯救了我的一天,干杯人!
【解决方案4】:

值得注意的是AddSectionPagesField()也存在。这样,'Y' 将是该部分的页数,而不是整个文档的页数。

当您为一张印刷品生成许多不同的文档并且您想要单独计算页数时,它会发现它的用处。我希望它是可以理解的。

那么你也可以使用:

            Paragraph paragraph = new Paragraph();
            paragraph.AddText("Page");
            paragraph.AddPageField();
            paragraph.AddText(" of ");
            paragraph.AddSectionPagesField();

            // Add paragraph to header for odd pages.
            section.Headers.Primary.Add(paragraph);
            // Add clone of paragraph to header for odd pages. Cloning is necessary because an object must
            // not belong to more than one other object. If you forget cloning an exception is thrown.
            section.Headers.EvenPage.Add(paragraph.Clone());

同样仅用于页脚:

            section.Footers.Primary.Add(paragraph);
            section.Footers.EvenPage.Add(paragraph.Clone());

【讨论】:

    【解决方案5】:

    这里是你可以解决它的方法

            Paragraph foot = sec.Footers.Primary.AddParagraph();
            foot.AddText("Page ");
            foot.AddPageField();
            foot.AddText(" of ");
            foot.AddNumPagesField();
    

    【讨论】:

      猜你喜欢
      • 2020-03-18
      • 2019-02-26
      • 2020-11-03
      • 2010-09-12
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      相关资源
      最近更新 更多