How to replace text in PDF with C #?

I have seen many solutions here, but none of them is a clear or good answer.

Here is my simple question, hoping with a direct answer.

I have a PDF file (template) that is created with text like this:

{FIRSTNAME} {LASTNAME} {ADDRESS} {PHONENUMBER}

Is it possible to have C # code that replaces these templates with text of my choice?

No fields, no other complex material.

Is there any open source library that helps me achieve this?

+5
source share
4 answers

, . DocX DocX, , DocX PDF ( PDF Creator ).

pdf sharp/migradoc .

+2

, , . , , :).

, PdfSharp , ( , , ).

, , , pdf , PdfContent PdfReference .

+3

"" PDF , . . , PDF . , .

, , . , , , .

, OpenSource, . PDFKit.NET. (, , ..). . page.CreateShapes . , PDF.

: http://www.tallcomponents.com/pdfkit3.aspx

: TallComponents,

+2
source

For plain text, replace the iTextSharp library . Below is the code that replaces one line with another. Please note that this will replace only plain text and may not work in all cases.

    //using iTextSharp.text.pdf;
    void VerySimpleReplaceText(string OrigFile, string ResultFile, string origText, string replaceText)
    {
        using (PdfReader reader = new PdfReader(OrigFile))
        {
            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                byte[] contentBytes = reader.GetPageContent(i);
                string contentString = PdfEncodings.ConvertToString(contentBytes, PdfObject.TEXT_PDFDOCENCODING);
                contentString = contentString.Replace(origText, replaceText);
                reader.SetPageContent(i, PdfEncodings.ConvertToBytes(contentString, PdfObject.TEXT_PDFDOCENCODING));
            }
            new PdfStamper(reader, new FileStream(ResultFile, FileMode.Create, FileAccess.Write)).Close();
        }
    }
+2
source

All Articles