ITextSharp为合并的pdf添加页码

时间:2016-04-27 18:00:58

标签: c# asp.net sql-server pdf itextsharp

我正在尝试将页码添加到使用FileStream构建的合并pdf,这些pdf是从上传到我的服务器的各个pdf构建的。我从MSSQL服务器读取了pdf文件名列表,然后合并页面。

我知道我最有可能使用pdfstamper,但大多数示例都与我的特定代码无关。

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

提前致谢!

2 个答案:

答案 0 :(得分:3)

因为看起来您已经获得了将文件合并在一起的代码。如果你想在pdf中添加文本,你可以使用PdfStamper以下代码是我用来向pdf添加文本的代码(你需要在x和y点将它带到你需要的位置)

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

答案 1 :(得分:0)

我在这里Add Header and Footer for PDF using iTextsharp找到了解决方案(几乎显示了页码而不是总页数)。