1

I am merging a series of Word documents by converting each of them into PDF using PdfSharp.PDF. Each Word document has paging defined in it (for ex: "Page 1 of 2" etc). But when I merge the PDF, I want to overwrite the paging to indicate the page # across all pages present in the merged document (i.e. "Page 1 of 265" etc).

I found a few options online but using other PDF techniques but can this be achieved using PdfSharp.PDF?

Heres the code -

 PdfDocument outputDocument = new PdfDocument();
    PdfDocument currPdf = PdfReader.Open((string)fileName, PdfDocumentOpenMode.Import);
    foreach (PdfPage p in currPdf.Pages)
    {
        outputDocument.AddPage(p);
    }
    outputDocument.Save(outputName);

1 Answers1

2

Before outputDocument.Save you can insert a loop that goes through all pages and adds the page numbers.

I didn't downvote the question, but this is so trivial that I understand someone else gave a downvote.

See also:
http://www.pdfsharp.net/wiki/ConcatenateDocuments-sample.ashx
http://www.pdfsharp.net/wiki/CombineDocuments-sample.ashx

  • Thank you for the response. While the links are helpful in adding graphics to set the page number, what I was looking for is to replace current page number setup with a new one. i.e. find the page number range present in each page (which will be aligned differently in the footer depending on whether they is other text defined in it in different pages) and replace it with the new range. I will dig into the links further and see if this can be achieved. – Kiran Lakshmikantha Sep 05 '14 at 18:11
  • So the problem is not as trivial as I thought it was. PDFsharp was not designed to retrieve text from PDF pages - and retrieving text can be very complicated due to nesting and transforming. A simple hack would be drawing a white rectangle over the old page number and then drawing the new page number (obviously this will work only if you know the documents and know where the page numbers are). Or maybe cover an area (e.g. 2 cm) at the bottom with a semi-transparent rectangle and add a small opaque rectangle with the new page number. Text covered by the semi-transparent rect can still be read. – I liked the old Stack Overflow Sep 08 '14 at 08:24