PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Wed Nov 05, 2025 9:49 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: PDFsharp edit a pdf file
PostPosted: Mon Jul 15, 2013 7:31 am 
Offline

Joined: Mon Jul 15, 2013 7:07 am
Posts: 3
Environment - PDFsharp Library, Visual Studio 2012 and C# as the language.

I am trying to:
    1. read Test1.pdf (Width = 17 inches, Height – 11 inches) with 1 page
    2. add some text to it
    3. save it as another file (Test2.pdf)

I am able to do all the following. But when I open the file Test2.pdf the size of the page is getting reduced to Width = 11 inches, Height – 11 inches. These PDF files that I am using are Product Specification Sheets that I have downloaded from the internet. I believe this is happening on only certain types of file and I am not sure how to differentiate these files.

Code given below:
//File dimentions - Width = 17 inches, Height - 11 inches (Tabloid Format)
PdfDocument pdfDocument = PdfReader.Open(@"D:\Test1.pdf", PdfDocumentOpenMode.Modify);

PdfPage page = pdfDocument.Pages[0];
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);

//When the file is saved dimentions change to - Width = 11 inches, Height - 11 inches
pdfDocument.Save(@"D:\Test2.pdf");


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 15, 2013 7:56 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3136
Location: Cologne, Germany
PDF files come in many different "flavors".
Maybe the workaround described here will work for you:
viewtopic.php?p=2637#p2637

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 15, 2013 9:06 am 
Offline

Joined: Mon Jul 15, 2013 7:07 am
Posts: 3
Thomas, thank you for the reply. I restructured the code as suggested by you to the following:

Code:
PdfDocument PDFDoc = PdfReader.Open(@"D:\Test1.pdf", PdfDocumentOpenMode.Import);
PdfDocument PDFNewDoc = new PdfDocument();
for (int Pg = 0; Pg < PDFDoc.Pages.Count; Pg++)
{
PDFNewDoc.AddPage(PDFDoc.Pages[Pg]);
XGraphics gfx = XGraphics.FromPdfPage(PDFDoc.Pages[Pg]);
XFont font = new XFont("Arial", 10, XFontStyle.Regular);
gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, PDFDoc.Pages[Pg].Width, PDFDoc.Pages[Pg].Height), XStringFormats.BottomCenter);
}
PDFNewDoc.Save(@"D:\Test2.pdf");

When I run this code I get the exception - Cannot create XGraphics for a page of a document that cannot be modified. Use PdfDocumentOpenMode.Modify.

And when I change the code from
PdfDocument PDFDoc = PdfReader.Open(@"D:\Test1.pdf", PdfDocumentOpenMode.Import);
to
PdfDocument PDFDoc = PdfReader.Open(@"D:\Test1.pdf", PdfDocumentOpenMode.Modify);

I get the exception - A PDF document must be opened with PdfDocumentOpenMode.Import to import pages from it.

How exactly should I achieve this ?


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 15, 2013 9:15 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3136
Location: Cologne, Germany
Use the page returned by AddPage or the page from PDFNewDoc.Pages, not the page from PDFDoc.Pages.

See also:
http://stackoverflow.com/a/17649357/162529

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 15, 2013 9:29 am 
Offline

Joined: Mon Jul 15, 2013 7:07 am
Posts: 3
Thomas, thanks a lot !!!
This solved my problem.


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 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