3

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.

4

2 回答 2

13

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();
}
于 2012-05-07T17:09:09.200 回答
1

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();
于 2012-05-07T17:11:22.680 回答