2

I'm currently trying to use iTextSharp to do some PDF field mapping, but the challenging part right now is just saving the modified file in a varbinary[max] column. Then I later need to read that blob and convert it into a pdf which I save to a file.

I've been all over looking at example code but I can't find exactly what I'm looking for, and can't seem to piece together the [read from file to iTextSharp object] -> [do my stuff] -> [convert to varbinary(max)] pipeline, nor the conversion of that blob back into a savable file.

If anyone has code snippet examples that would be extremely helpful. Thanks!

1 Answer 1

2

The need to deal with a pdf in multiple passes was not immediately clear when I first started working them, so maybe this is some help to you.

In the method below, we create a pdf, render it to a byte[], load it for post processing, render the pdf again and return the result.

The rest of your question deals with getting a byte[] into and out of a varbinary[max], saving a byte[] to file and reading it back out, which you can google easily enough.

public byte[] PdfGeneratorAndPostProcessor()
{
  byte[] newPdf;

  using (var pdf = new MemoryStream())
  using (var doc = new Document(iTextSharp.text.PageSize.A4))
  using (PdfWriter.GetInstance(doc, pdf))
  {
    doc.Open();

    // do stuff to the newly created doc...

    doc.Close();
    newPdf = pdf.GetBuffer();
  }      

  byte[] postProcessedPdf;
  var reader = new PdfReader(newPdf);

  using (var pdf = new MemoryStream())
  using (var stamper = new PdfStamper(reader, pdf))
  {
    var pageCount = reader.NumberOfPages;
    for (var i = 1; i <= pageCount; i++)
    {
      // do something on each page of the existing pdf
    }

    stamper.Close();
    postProcessedPdf = pdf.GetBuffer();
  }

  reader.Close();
  return postProcessedPdf;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.