I am trying to replace some keyword in my pdf file using Itext Sharp and I am using exactly the same code from the sites documenation but I get an empty file with no text. Here is the code. and the strange thing is the file size is same.
public static void EditPdfDoc()
{
string fileNameExisting = PathManager.GetPdfTemplatePath("MD_ContSum_F_082415.pdf");
Encoding en = GetEncoding(fileNameExisting);
string fileNameNew =PathManager.GetPdfTemplatePath("")+"new.pdf";
FileStream fstream = File.Create(fileNameNew);
// Open existing PDF
var pdfReader = new PdfReader(fileNameExisting);
PdfDictionary dict = pdfReader.GetPageN(1);
PdfObject object1 = dict.GetDirectObject(PdfName.CONTENTS);
if(object1.GetType()==typeof(PRStream))
{
PRStream stream = (PRStream)object1;
byte[] data = PdfReader.GetStreamBytes(stream);
stream.SetData(Encoding.ASCII.GetBytes(data.ToString().Replace("{$Resource.Months}", "Replaced!")));
}
// PdfStamper, which will create
var stamper = new PdfStamper(pdfReader, fstream);
stamper.Close();
pdfReader.Close()