PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri May 03, 2024 10:33 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Tue Nov 25, 2008 1:38 pm 
Offline

Joined: Wed Nov 19, 2008 2:27 pm
Posts: 1
I'm getting this error where is says
gfx = XGraphics.FromPdfPage(page);

can someone explain how I need to fix this?

Cannot create XGraphics for a page of a document that cannot be modified. Use PdfDocumentOpenMode.Modify.

Code:
if (checkBox1.Checked == true)
                {
                    MessageBox.Show("Printing Page Numbers.");
                    PdfDocument outputDocument = new PdfDocument();

                    XFont font = new XFont("Verdana", 10, XFontStyle.Bold);
                    XStringFormat format = new XStringFormat();
                    format.Alignment = XStringAlignment.Center;
                    format.LineAlignment = XLineAlignment.Far;
                    XGraphics gfx;
                    XRect box;

                    foreach (string file in pdfList.Items)
                    {

                        PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);
                        int count = inputDocument.PageCount;

                        for (int idx = 0; idx < count; idx++)
                        {
                            PdfPage page = inputDocument.PageCount > idx ?
                            inputDocument.Pages[idx] : new PdfPage();
                            gfx = XGraphics.FromPdfPage(page);
                            box = page.MediaBox.ToXRect();
                            box.Inflate(0, -10);
                            gfx.DrawString(String.Format("{0} • {1}", file, idx + 1),
                            font, XBrushes.Red, box, format);

                            outputDocument.AddPage(page);


                        }
                    }




Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Nov 26, 2008 8:46 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3097
Location: Cologne, Germany
The page was opened in Import mode (for import only, i. e. read-only).

It's in the error message: use PdfDocumentOpenMode.Modify instead of PdfDocumentOpenMode.Import if you really want to modify the PDF file you opened.
Or create a new PDF file, copy the contents of the old file and add page numbers to the new file.

With PdfDocumentOpenMode.Modify the source file will not change if you don't call Save.

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 85 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