0

I am able to copy the contents and edit , but i am not getting the same template as the old one, the template is getting changed, and i have a image on my old file and that image is also not getting copied into my new file , rest of the other contents are getting copied,c an someone help me to make my new pdf file template as the old one, here is my code below.

    public static void Main(string[] args)
    {
        var editedText = ExtractTextFromPdf(@"C:\backup_temp\Template.pdf");
        string outputfile =@"C:\backup_temp\Result.pdf";
        using (var fileStream = new FileStream(outputfile, FileMode.Create, 
        FileAccess.Write)) 
        {
            Document document = new Document(PageSize.A4, 25, 25, 30, 30);
            PdfWriter writer = PdfWriter.GetInstance(document, fileStream);
            document.Open();
            document.Open();
            document.Add(new Paragraph(editedText));
            document.Close();
            writer.Close();
            fileStream.Close();
        }
    }

    public static string ExtractTextFromPdf(string path)
    {
        using (PdfReader reader = new PdfReader(path))
        {
            StringBuilder text = new StringBuilder();
            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
            text.Append(PdfTextExtractor.GetTextFromPage(reader, i));
            text.Replace("[DMxxxxxxx]", "[DM123456]");
            }
            return text.ToString();
        }
    }
mbelsky
  • 6,093
  • 2
  • 26
  • 34
Shakti S
  • 21
  • 10
  • Please read my comments in answer to the question [creating new pdf by copying data from another pdf](https://stackoverflow.com/questions/45415907/creating-new-pdf-by-copying-data-from-another-pdf). You are trying to achieve something that is impossible. If you are the same person as [kulbans1991](https://stackoverflow.com/users/5964772/kulbans1991), please be aware that taking a new identity to post the same question once more will not change the answer. – Bruno Lowagie Aug 01 '17 at 10:43
  • then is it possible to meet my requirement in anyother way, which means i should edit the content from an old pdf and save it as a new pdf document, i tried using so many things and methods but it is not working out. – Shakti S Aug 01 '17 at 11:06
  • No, it's not possible to meet your requirement in any other way if you only have access to the PDF. If you have access to the original format (e.g. a Word file, the original data and the code that was used to produce your PDF,...), you should alter that original format and recreate the PDF. – Bruno Lowagie Aug 01 '17 at 11:22
  • @ShaktiS Actually it probably *is* possible. It depends on the very PDF. So, if you are in a position to formulate requirements for the PDFs and data your program shall receive as input, you might create an implementation. Unfortunately, though, there are less and less PDFs nowadays fulfilling the requirements in question... – mkl Aug 01 '17 at 11:41

1 Answers1

0

As Bruno says, if your "template" is another pdf document, you can not achieve this functionality in a trivial way. Pdf documents do not automatically reflow their content. And to the best of my knowledge, there is no pdf library that will allow you to insert/replace/edit content and still produce a nice-looking document.

The best solution in your case would be:

  • store the template document as an easy to edit format
  • generate the pdf document based on this easy template

Example use-case:

  1. I have some HTML document that contains the precise layout and images and text, and some placeholders for things I want to fill in.
  2. I use JSoup (or some other library) to edit the DOM structure of my template, this is very easy since I can give elements IDs and simply change the content by ID. I don't need regular expressions.
  3. I use pdfHTML (iText add-on) to convert my html document to pdf
Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54