0

I have a pdf file which has some content in it. It actually is a template. I have managed to read the contents from the pdf and made changes according to requirement and saved it in a string variable. Now I have to create a new pdf file which will have the changes and the initial template file won't be changed. For more transparency, I have attached the code sample. I am not being able to reflect the changes in the new pdf file. It's coming same as the initial template file with a different file name.

{
    string oldpath = @ "../../../JobAdminLib/pdfTemplate/sample_new.pdf";
    string newpath = @ "../../../JobAdminLib/pdfTemplate/sample_result_1.pdf";

    if (File.Exists(oldpath)) {
        PdfReader pdfReader = new PdfReader(oldpath);
        for (int page = 1; page <= pdfReader.NumberOfPages; page++) {
            ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
            string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);

            currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default,
            Encoding.UTF8, Encoding.Default.GetBytes(currentText)));

            string altertext = currentText.Replace("[_job]", "hahaha");
            string alterjobno = altertext.Replace("[DMxxxxxxx]", "DM12345");
            string text = alterjobno;
            text.Append(alterjobno);

        }
    }
}
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 3
    Er... PDF is not a Word processing format. You can't just replace one word with another, and expect the PDF to look nice. When you say "template", are you talking about an interactive PDF (a form) or a static PDF? If you're talking about a static PDF, you shouldn't alter the PDF. You should alter the *source that was used to create the PDF*, alter that source, and recreate the PDF. That how's it usually done. – Bruno Lowagie Jul 31 '17 at 12:28
  • Ok.. but in string text, on debbuging i saw it is replacing that particular word. only thing is that now i have to save it in a new pdf – kulbans1991 Jul 31 '17 at 12:32
  • Use (for example) LaTeX, source file is in text format, you can have simple template and modify it on the run. Then just run LaTeX compiler and enjoy your beautifully generated PDF file. Other ways are just overkill. – Pavel Pája Halbich Jul 31 '17 at 12:34
  • You could use a `PdfWriter` or write to the outputstream if it is webbased: https://stackoverflow.com/questions/31078442/how-to-save-pdf-on-server-map-path-using-itextsharp – JP Hellemons Jul 31 '17 at 12:36
  • I understand your question, but you don't understand my answer. Please download [chapter 6](https://manning-content.s3.amazonaws.com/download/d/3645210-8560-4e6d-9b03-3f9aca1921a5/samplechapter6.pdf) of my book, and read pages 159-160 carefully. **I am telling you that your requirement can't be met in PDF (in general).** Maybe your PDF is interactive (you didn't answer my question about the nature of your PDF); maybe it isn't. As long as you don't give us more info about your PDF, no one will be able to help you. – Bruno Lowagie Jul 31 '17 at 12:58
  • I have decided to vote to close this question as "too broad." Editing a PDF is extremely difficult, and the OP is presenting his problem in an extremely simplistic way *as if you can search and replace a string in PDF syntax.* The OP doesn't realize that iText's text extraction strategy executes the PDF syntax and that the result is a simple string *without* PDF syntax. Replacing one word by another in that result will change the string, but since all the original PDF syntax, **it makes no sense to put the altered string back into the PDF.** – Bruno Lowagie Jul 31 '17 at 13:05

0 Answers0