PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Writing text on existing pdf
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4250
Page 1 of 1

Author:  souocare [ Wed May 05, 2021 3:51 pm ]
Post subject:  Writing text on existing pdf

Hi.
I need some help.
I was searching for how to write on an existing PDF file.
I have this code here:
Code:
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
            PdfDocument PDFDoc = PdfReader.Open(@"C:\Users\Userofthispc\Documents\Test.pdf", PdfDocumentOpenMode.Import);
            PdfDocument PDFNewDoc = new PdfDocument();

            for (int Pg = 0; Pg < PDFDoc.Pages.Count; Pg++)
            {
                PdfPage pp = PDFNewDoc.AddPage(PDFDoc.Pages[Pg]);

                XGraphics gfx = XGraphics.FromPdfPage(pp);
                XFont font = new XFont("Arial", 30, XFontStyle.Italic);
                gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(pp.Width / 2, pp.Height / 2 + 85, pp.Width, pp.Height), XStringFormats.Center);
            }

            PDFNewDoc.Save(@"C:\Users\Userofthispc\Documents\Testv2.pdf");


The code should work (I think), but when I run it, it doesn't write anything.
Can you help me?

Author:  TH-Soft [ Wed May 05, 2021 4:11 pm ]
Post subject:  Re: Writing text on existing pdf

Try "new XRect(0, 0, pp.Width, pp.Height)".
Do you see text now?

Author:  souocare [ Wed May 05, 2021 4:23 pm ]
Post subject:  Re: Writing text on existing pdf

Strange, I had that before and it was not working. And now it is!

Thank you!
How can I move it up a little bit?

Author:  souocare [ Wed May 05, 2021 4:24 pm ]
Post subject:  Re: Writing text on existing pdf

I just found the way. Thank you so much!!

Author:  TH-Soft [ Wed May 05, 2021 6:08 pm ]
Post subject:  Re: Writing text on existing pdf

souocare wrote:
Strange, I had that before and it was not working. And now it is!
The rectangle you specify in the code in the first post is mostly outside of the visible page region. The text is centred and also out of sight.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/