C# 将页码添加到 pdf 文档 (itextsharp)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10485728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:01:42  来源:igfitidea点击:

add page number to pdf document (itextsharp)

c#asp.netitextsharp

提问by slayer35

I want to add page numbers to the footer of the itextsharp pdf file.Im generating pdf from html (asp.net repeater).And Im using XMLWorkerHelper to parse the html content.I searched a lot but cant find anything useful to achive this.

我想将页码添加到 itextsharp pdf 文件的页脚。我从 html 生成 pdf(asp.net 转发器)。我使用 XMLWorkerHelper 来解析 html 内容。我搜索了很多,但找不到任何有用的东西来实现这一点。

采纳答案by shriek

You'll have to open the PDF with iTextSharpand add the page numbers yourself. I did something like this a while back, here's my function that might give you a start. The function adds the current page to the lower left, so you might have to place it somewhere else that fits your needs.

您必须自己打开 PDFiTextSharp并添加页码。不久前我做了这样的事情,这是我的功能,可能会让你开始。该函数将当前页面添加到左下方,因此您可能需要将其放置在适合您需要的其他位置。

public static byte[] AddPageNumbers(byte[] pdf)
{
MemoryStream ms = new MemoryStream();
// we create a reader for a certain document
PdfReader reader = new PdfReader(pdf);
// we retrieve the total number of pages
int n = reader.NumberOfPages;
// we retrieve the size of the first page
Rectangle psize = reader.GetPageSize(1);

// step 1: creation of a document-object
Document document = new Document(psize, 50, 50, 50, 50);
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance(document, ms);
// step 3: we open the document

document.Open();
// step 4: we add content
PdfContentByte cb = writer.DirectContent;

int p = 0;
Console.WriteLine("There are " + n + " pages in the document.");
for (int page = 1; page <= reader.NumberOfPages; page++)
{
    document.NewPage();
    p++;

    PdfImportedPage importedPage = writer.GetImportedPage(reader, page);
    cb.AddTemplate(importedPage, 0, 0);

    BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    cb.BeginText();
    cb.SetFontAndSize(bf, 10);
    cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, +p + "/" + n, 7, 44, 0);
    cb.EndText();
}
// step 5: we close the document
document.Close();
return ms.ToArray();
}

回答by James Johnson

Something like this should work:

这样的事情应该工作:

var sourceFileList = new List<string>();

//add files to merge

int sourceIndex = 0;
PdfReader reader = new PdfReader(sourceFileList[sourceIndex]);
int sourceFilePageCount = reader.NumberOfPages;

Document doc = new Document(reader.GetPageSizeWithRotation(1));
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(destinationFileName, FileMode.Create));
doc.Open();

PdfImportedPage page;
PdfContentByte contentByte = writer.DirectContent;                

int rotation;
while (sourceIndex < sourceFileList.Count)
{
    int pageIndex = 0;
    while (pageIndex < sourceFilePageCount)
    {
        pageIndex++;

        doc.SetPageSize(reader.GetPageSizeWithRotation(pageIndex));
        doc.NewPage();

        page = writer.GetImportedPage(reader, pageIndex);
        rotation = reader.GetPageRotation(pageIndex);

        if (rotation.Equals(90 | 270))
            contentByte.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pageIndex).Height);
        else
            contentByte.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
    }

    sourceIndex++;
    if (sourceIndex < sourceFileList.Count)
    {
        reader = new PdfReader(sourceFileList[sourceIndex]);
        sourceFilePageCount = reader.NumberOfPages;
    }
}

doc.Close();