PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 9:13 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed May 05, 2021 3:51 pm 
Offline

Joined: Wed May 05, 2021 3:47 pm
Posts: 3
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?


Top
 Profile  
Reply with quote  
PostPosted: Wed May 05, 2021 4:11 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Try "new XRect(0, 0, pp.Width, pp.Height)".
Do you see text now?

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


Top
 Profile  
Reply with quote  
PostPosted: Wed May 05, 2021 4:23 pm 
Offline

Joined: Wed May 05, 2021 3:47 pm
Posts: 3
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?


Top
 Profile  
Reply with quote  
PostPosted: Wed May 05, 2021 4:24 pm 
Offline

Joined: Wed May 05, 2021 3:47 pm
Posts: 3
I just found the way. Thank you so much!!


Top
 Profile  
Reply with quote  
PostPosted: Wed May 05, 2021 6:08 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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.

_________________
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  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 123 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:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group