PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Mar 04, 2024 8:41 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



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

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

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.


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

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

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

            CreatePdf(originalPdf);

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

                ReplaceText(contents, "Hello", "Hola");
                page.Contents.ReplaceContent(contents);

                doc.Pages.Remove(page);
                doc.AddPage().Contents.ReplaceContent(contents);
               
                doc.Save(originalPdf);
            }

            Process.Start(originalPdf);

        }

        // 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),
              XStringFormats.Center);

            // Save the document...
            document.Save(filename);
            // ...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);
                                }

                            }
                        }
                    }


                }
            }


        }
    }




Best,
Reas


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 07, 2023 3:44 pm 
Offline

Joined: Tue Dec 05, 2023 7:47 am
Posts: 2
I get the error `System.InvalidOperationException: "'CStringType' must not be null here."` in line
`page.Contents.ReplaceContent(contents);`


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 14, 2023 2:36 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3092
Location: Cologne, Germany
Wano wrote:
I get the error `System.InvalidOperationException: "'CStringType' must not be null here."` in line
`page.Contents.ReplaceContent(contents);`
Which version of PDFsharp are you using?

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 10, 2024 2:49 pm 
Offline

Joined: Wed Jan 10, 2024 2:46 pm
Posts: 2
Thomas Hoevel wrote:
Wano wrote:
I get the error `System.InvalidOperationException: "'CStringType' must not be null here."` in line
`page.Contents.ReplaceContent(contents);`
Which version of PDFsharp are you using?


Hello! Same error here... I am using v6.0.0. ┬┐Any clue?. Thank you in advance!


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 10, 2024 2:56 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 901
Location: CCAA
VicEstampida wrote:
Hello! Same error here... I am using v6.0.0.
If the problem persists with v6.1.0-preview-1, then please use the IssueSubmissionTemplate to let us replicate the issue.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 10, 2024 3:39 pm 
Offline

Joined: Wed Jan 10, 2024 2:46 pm
Posts: 2
TH-Soft wrote:
VicEstampida wrote:
Hello! Same error here... I am using v6.0.0.
If the problem persists with v6.1.0-preview-1, then please use the IssueSubmissionTemplate to let us replicate the issue.


Thank you, but we're not allowed to use pre-release packages in this project, so I downgraded PDFSharp to version 1.50.5147 and it's working fine. Thank you again Thomas!


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 11, 2024 9:41 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 901
Location: CCAA
VicEstampida wrote:
Thank you, but we're not allowed to use pre-release packages in this project, so I downgraded PDFSharp to version 1.50.5147 and it's working fine.
When nobody evaluates the preview of 6.1.0, then the stable release of 6.1.0 will have the same bugs.
Feel free to stick to 1.50 with hundreds of bugs that were fixed in the meantime.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 16, 2024 8:45 pm 
Offline

Joined: Tue Sep 30, 2014 12:29 pm
Posts: 36
I can confirm the code works with v6.1.0-preview-1.
(when using a proper FontResolver)


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 16, 2024 9:35 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 901
Location: CCAA
(void) wrote:
I can confirm the code works with v6.1.0-preview-1.
Thanks for the feedback. Sounded like a bug that was already resolved.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


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

All times are UTC


Who is online

Users browsing this forum: Baidu [Spider] and 78 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