PDFsharp:有没有办法在页面标题中生成"Y页面X"?

mka*_*tzm 27 c# pdfsharp migradoc

这似乎很简单,但我在API中找不到类似getPageCount()的东西.我可以让它返回当前页面,但不是总页数.也许我想念它?

我想以某种方式能够在每页的顶部打印"第1页,共9页",其中"1"当然是当前的页码.

Kid*_*ick 31

确保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);
Run Code Online (Sandbox Code Playgroud)

  • 这对我来说完美无缺. (3认同)
  • 这个“MigraDoc”是什么,我从哪里得到它?我正在使用`PdfSharp`,就像问题所述。我没有`Document` 只有`PdfDocument` (2认同)

Je *_*not 28

使用PDFsharp,这取决于您.

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

使用AddPageField的示例

示例中的代码段:

// 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());
Run Code Online (Sandbox Code Playgroud)

设置制表位的代码片段(假设DIN A 4的主体为16 cm):

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

这两个片段都来自链接网站.示例代码也可供下载.

  • @Marek Bar:AddNumPagesField添加文档中的页数(并且在页面之间不改变),AddPageField添加当前页码并在页面之间进行更改。 (2认同)

小智 14

我知道这个问题已经过时并且有一个可接受的答案,但是在搜索PDFsharp解决方案时,问题出现在第一个问题中.

为了记录,在PDFsharp中实现这一点很容易.PdfDocumentPdfSharp.Pdf命名空间下找到的类包含pages(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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

XGraphics gfx1 = XGraphics.FromPage(page);
XGraphics gfx2 = XGraphics.FromPage(page);
Run Code Online (Sandbox Code Playgroud)