简体   繁体   中英

add page number to pdf document (itextsharp)

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

You'll have to open the PDF with iTextSharp and 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.

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();
}

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM