0

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()
user1505521
  • 231
  • 1
  • 2
  • 12
  • And from which website did you get this code? You did not mention that in your question. – Amedee Van Gasse Dec 08 '16 at 18:42
  • @AmedeeVanGasse: This would be the exact link http://developers.itextpdf.com/examples/stamping-content-existing-pdfs-itext5/replacing-pdf-objects – user1505521 Dec 08 '16 at 19:03
  • `stream.SetData(Encoding.ASCII.GetBytes(data.ToString().Replace("{$Resource.Months}", "Replaced!")));` this is so wrong... – mkl Dec 08 '16 at 21:03
  • You overlooked this: **"This is only a partial answer. It's a quick and dirty method showing how to change a stream inside a PDF. Obviously, you'll have to detect words that are stored in Form XObjects too, and you can seriously screw up the layout when you manipulate the content stream as is done in this example."** You shouldn't copy/paste code you don't understand! This example is out of your league. Only big boys may use it. It's not for newbies. – Bruno Lowagie Dec 08 '16 at 21:26
  • @BrunoLowagie The example in question should really be removed from the iText site because no one will read the stack overflow answer it refers to, everyone will get the impression that PDF editing is trivial. (Admittedly, the OP made things even worse in his code...) – mkl Dec 08 '16 at 21:31
  • @mkl I'm answering from my phone and I can't edit the web site right now, but the least we should do if we don't remove the example is add a warning "whoever copies this code is not worthy of being a developer." ;-) – Bruno Lowagie Dec 08 '16 at 21:35
  • @BrunoLowagie: haha I got you man! thanks for the insult anyway.. bt i never came across document processing until today in my career and in these couple of days I just understood what really PDFs are.. – user1505521 Dec 09 '16 at 14:08

0 Answers0