Is it possible to return binary stream (byte[ ]) from pdfstamper ?
Basically the objective is to edit PDF doc and replace particular text.
Input already in binary stream (byte[ ])
I worked on C# environment & iText for the PDF editing lib.
Here's my piece of code :
PdfReader reader = new PdfReader(Mydoc.FileStream);
PdfDictionary dict = reader.GetPageN(1);
PdfObject pdfObject = dict.GetDirectObject(PdfName.CONTENTS);
if (pdfObject.IsStream())
{
PRStream stream = (PRStream)pdfObject;
byte[] data = PdfReader.GetStreamBytes(stream);
stream.SetData(System.Text.Encoding.ASCII.GetBytes(System.Text.Encoding.ASCII. GetString(data).Replace("[TextReplacement]", "Hello world")));
}
FileStream outStream = new FileStream(dest, FileMode.Create);
PdfStamper stamper = new PdfStamper(reader, outStream);
reader.Close();
return newPDFinStream // this result should be in stream byte[]
Understand that FileStream need to have output filepath like C:\location\new.pdf
is it possible to not temporary save it ? and directly return the binary?
Sure, just save it to a MemoryStream
instead:
using (MemoryStream ms = new MemoryStream())
{
// Odd to have a constructor but not use the newly-created object.
// Smacks of the constructor doing too much.
var ignored = new PdfStamper(reader, ms);
return ms.ToArray();
}
See more on this question at Stackoverflow