2

I'm trying to add page numbers to a merged pdf built using FileStream from individual pdf's uploaded to my server. I read the list of pdf file names from MSSQL server then merge the pages.

I know most likely I should use pdfstamper but most examples don't relate to my particular code.

try
{
    rdr = cmd2.ExecuteReader();

    DataTable dt = new DataTable();
    dt.Load(rdr);
    List<PdfReader> readerList = new List<PdfReader>();
    foreach (DataRow row in dt.Rows)
    {
        PdfReader pdfReader = new PdfReader("http://site.azurewebsites.net/uploads/reports/" +
          Convert.ToString(row[0]));
        readerList.Add(pdfReader);
    }
    con2.Close();
    System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
    contentType.MediaType = System.Net.Mime.MediaTypeNames.Application.Pdf;
    Document document = new Document(PageSize.A4, 0, 0, 0, 0);
    //Get instance response output stream to write output file.
    PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
    document.Open();
    foreach (PdfReader reader in readerList)
    {
        for (int i = 1; i <= reader.NumberOfPages; i++)
        {
            PdfImportedPage page = writer.GetImportedPage(reader, i);
            document.Add(iTextSharp.text.Image.GetInstance(page));
        }
    }
    document.Close();
    Response.AppendHeader("content-disposition", "inline; filename=" + Request.QueryString["id"] + "-Final");
    Response.ContentType = "application/pdf";
    }
    }
    catch
    {
    Response.Redirect("~/login.aspx", false);
    }
}

private void MergePDFs(string outPutFilePath, params string[] filesPath)
{
    List<PdfReader> readerList = new List<PdfReader>();
    foreach (string filePath in filesPath)
    {
        PdfReader pdfReader = new PdfReader(filePath);
        readerList.Add(pdfReader);
    }
    //Define a new output document and its size, type
    Document document = new Document(PageSize.A4, 0, 0, 0, 0);
    //Create blank output pdf file and get the stream to write on it.
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outPutFilePath, FileMode.Create));
    document.Open();
    foreach (PdfReader reader in readerList)
    {
        for (int i = 1; i <= reader.NumberOfPages; i++)
        {

            PdfImportedPage page = writer.GetImportedPage(reader, i);
            document.Add(iTextSharp.text.Image.GetInstance(page));

        }
    }
    document.Close();
}

Thanks in advance!

MethodMan
  • 18,625
  • 6
  • 34
  • 52
Kyle G
  • 149
  • 1
  • 3
  • 10
  • What is your question or problem? There is no clear question here. – Mikanikal Apr 27 '16 at 18:23
  • I am trying to add pdf page numbers to each merged pdf file as in "Page 1 of 5". I have found examples like this http://developers.itextpdf.com/examples/columntext-examples/adding-page-numbers-existing-pdf but am struggling to incorporate pdfstamper into my code. – Kyle G Apr 27 '16 at 18:51

2 Answers2

3

since it looks like you have got the code for merging the files together. if you want to add text to the pdf you would use the PdfStamper the following code is what i use to add text to a pdf (you will need at x and y points to get it to the location that you need).

     using (var newPDF = new FileStream(outPutFile, FileMode.Create, FileAccess.ReadWrite))
     {
          PdfReader pdfReader = new PdfReader(reader);
          PdfStamper pdfStamper = new PdfStamper(pdfReader, newPDF);

          for (int page = 1; page <= pdfReader.NumberOfPages; page++)
          {
               PdfContentByte pdfContent = pdfStamper.GetOverContent(page);
               Rectangle mediabox = pdfReader.GetPageSize(page);  

               pdfContent.BeginText();
               pdfContent.ShowTextAligned(0, "someText", xLocation, mediabox.Height - yLocation , 0);
               pdfContent.EndText();


           }

           pdfStamper.Close();
      } 
Eric
  • 122
  • 8
0

I found the solution (almost it shows page number but not total pages) here Add Header and Footer for PDF using iTextsharp .

Community
  • 1
  • 1
Kyle G
  • 149
  • 1
  • 3
  • 10