creating new pdf by copying data from another pdf

2019-03-04 19:44发布

问题:

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);

        }
    }
}
标签: c# pdf itext