PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Jun 12, 2021 2:15 pm

All times are UTC

Forum rules

Please read this before posting on this forum: Forum Rules

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: How to edit PDF content
PostPosted: Tue Jun 09, 2020 6:34 am 

Joined: Tue Jun 09, 2020 6:22 am
Posts: 1

I have a PDF file. And it has some common text in each page.
I want to modify that common text, like I want to put current page number in that text suppose.

How can we do it using PDF sharp.

Reply with quote  
PostPosted: Fri Jan 22, 2021 10:41 am 

Joined: Sat Jan 09, 2021 7:09 am
Posts: 11
Here you go! Hope this helps.

    class Program
        static void Main(string[] args)
            string originalPdf = @"C:\origPdf.pdf";


            using (var doc = PdfReader.Open(originalPdf, PdfDocumentOpenMode.Modify))
                var page = doc.Pages[0];
                var contents = ContentReader.ReadContent(page);

                ReplaceText(contents, "Hello", "Hola");




        // Code from http://www.pdfsharp.net/wiki/HelloWorld-sample.ashx
        public static void CreatePdf(string filename)
            // Create a new PDF document
            PdfDocument document = new PdfDocument();
            document.Info.Title = "Created with PDFsharp";

            // Create an empty page
            PdfPage page = document.AddPage();

            // Get an XGraphics object for drawing
            XGraphics gfx = XGraphics.FromPdfPage(page);

            // Create a font
            XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic, new XPdfFontOptions(PdfFontEncoding.WinAnsi));

            // Draw the text
            gfx.DrawString("Hello, World!", font, XBrushes.Black,
              new XRect(0, 0, page.Width, page.Height),

            // Save the document...
            // ...and start a viewer.

        // Please refer to the pdf tech specs on what all entails in the content stream
        // https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf
        public static void ReplaceText(CSequence contents, string searchText, string replaceText)
            // Iterate thru each content items. Each item may or may not contain the entire
            // word if there are different stylings (ex: bold parts of the word) applied to a word.
            // So you may have to replace a character at a time.
            for (int i = 0; i < contents.Count; i++)
                if (contents[i] is COperator)
                    var cOp = contents[i] as COperator;
                    for (int j = 0; j < cOp.Operands.Count; j++)
                        if (cOp.OpCode.Name == OpCodeName.Tj.ToString() ||
                            cOp.OpCode.Name == OpCodeName.TJ.ToString())
                            if (cOp.Operands[j] is CString)
                                var cString = cOp.Operands[j] as CString;
                                if (cString.Value.Contains(searchText))
                                    cString.Value = cString.Value.Replace(searchText, replaceText);





Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC

Who is online

Users browsing this forum: Google [Bot] and 31 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group