PDFsharp & MigraDoc Foundation https://forum.pdfsharp.net/ |
|
How to edit PDF content https://forum.pdfsharp.net/viewtopic.php?f=2&t=4143 |
Page 1 of 1 |
Author: | ytbadgujar [ Tue Jun 09, 2020 6:34 am ] |
Post subject: | How to edit PDF content |
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. |
Author: | rsoeung [ Fri Jan 22, 2021 10:41 am ] |
Post subject: | Re: How to edit PDF content |
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 |
Author: | Wano [ Thu Dec 07, 2023 3:44 pm ] |
Post subject: | Re: How to edit PDF content |
I get the error `System.InvalidOperationException: "'CStringType' must not be null here."` in line `page.Contents.ReplaceContent(contents);` |
Author: | Thomas Hoevel [ Thu Dec 14, 2023 2:36 pm ] |
Post subject: | Re: How to edit PDF content |
Wano wrote: I get the error `System.InvalidOperationException: "'CStringType' must not be null here."` in line Which version of PDFsharp are you using?
`page.Contents.ReplaceContent(contents);` |
Author: | VicEstampida [ Wed Jan 10, 2024 2:49 pm ] |
Post subject: | Re: How to edit PDF content |
Thomas Hoevel wrote: Wano wrote: I get the error `System.InvalidOperationException: "'CStringType' must not be null here."` in line Which version of PDFsharp are you using?`page.Contents.ReplaceContent(contents);` Hello! Same error here... I am using v6.0.0. ¿Any clue?. Thank you in advance! |
Author: | TH-Soft [ Wed Jan 10, 2024 2:56 pm ] |
Post subject: | Re: How to edit PDF content |
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.
|
Author: | VicEstampida [ Wed Jan 10, 2024 3:39 pm ] |
Post subject: | Re: How to edit PDF content |
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! |
Author: | TH-Soft [ Thu Jan 11, 2024 9:41 am ] |
Post subject: | Re: How to edit PDF content |
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. |
Author: | (void) [ Tue Jan 16, 2024 8:45 pm ] |
Post subject: | Re: How to edit PDF content |
I can confirm the code works with v6.1.0-preview-1. (when using a proper FontResolver) |
Author: | TH-Soft [ Tue Jan 16, 2024 9:35 pm ] |
Post subject: | Re: How to edit PDF content |
(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.
|
Author: | Malindor [ Wed Aug 14, 2024 11:30 pm ] |
Post subject: | Re: How to edit PDF content |
Greetings, I know I'm jumping on an older post but I'm trying this with PDFSharp 6.1.1. and when I try the above code, it works (I can see the replacement of the text with the debugger) but all that is saved is a blank page. Has there been an update that has changed how the replacement text method should work? Thanks! |
Author: | TH-Soft [ Thu Aug 15, 2024 6:29 am ] |
Post subject: | Re: How to edit PDF content |
Malindor wrote: Has there been an update that has changed how the replacement text method should work? No.If you think there's a bug in PDFsharp, then see above (IssueSubmissionTemplate). Maybe the code above works for some pages, but invalidates other pages (e.g. with your PDF). |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |