PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat May 04, 2024 4:38 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Wed Mar 10, 2010 10:44 am 
Offline

Joined: Wed Mar 10, 2010 10:09 am
Posts: 4
Hey!

First thanks for offer PDFsharp. It works very well and it's definitively the best open source project respective modifying PDF with .NET/C#!

I have to put an PDF with an image (=background_filename) beneath another PDF with Text (=input_filename). I tried both options (draw XImage and XPdfForm) and always get a NullReferenceException (saying the object is null).

When I open 'background_filename' in Adobe Acrobat 9 and use the option
Document -> Background -> Add/Replace -> Source :: File :: 'background_filename'
and save it, the code work exactly as I want (XImage and XPdfForm).

So my two questions are: Is there a possibility to use an PDF file as Background which itself has not a Background (and use instead of that everything else 'above' the Background like images, text etc....) or is there a way to automate with C# the steps I made in Adobe Acrobat 9???

Thanks for your help!

My Code:
Code:
public void bg (string input_filename, string background_filename, string output_filename){       
            File.Copy(input_filename, output_filename, true);
            PdfDocument pdf_doc_output = PdfReader.Open(output_filename);
            //XPdfForm bg = XPdfForm.FromFile(background_filename);
            XImage bg = XImage.FromFile(background_filename);
           
            if (pdf_doc_output.Version < 14)
                pdf_doc_output.Version = 14;

            for (int idx = 0; idx < pdf_doc_output.Pages.Count; idx++)
            {
                PdfPage page = pdf_doc_output.Pages[idx];
                XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);
                //XPdfForm img = XPdfForm.FromFile(background_filename);
                XImage img = XImage.FromFile(background_filename);
                gfx.DrawImage(img, 0,0);
            }
            pdf_doc_output.Save(output_filename);
            Process.Start(output_filename);
        }


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 10, 2010 10:58 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3097
Location: Cologne, Germany
Hi!

Your code is conceptually correct.

I think XPdfForm is the class to use. Use "bg" (i. e. load background once outside the loop and use the same bg for every page).

Where do you get the NULL RefException?
If you set Debug => Exceptions to Break on "Thrown" you'll know where it comes from (if you included the PDFsharp source code in your solution).

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 10, 2010 11:42 am 
Offline

Joined: Wed Mar 10, 2010 10:09 am
Posts: 4
It crashes when I want to Draw the Image:
Code:
gfx.DrawImage(img, 0,0);


I remove the XImage part as well as the bg part inside the loop. Maybe you can reproduce the error by using the (new) code:
Code:
public void background (string input_filename, string background_filename, string output_filename){       
            File.Copy(input_filename, output_filename, true);
            PdfDocument pdf_doc_output = PdfReader.Open(output_filename);
            XPdfForm bg = XPdfForm.FromFile(background_filename);
           
            if (pdf_doc_output.Version < 14)
                pdf_doc_output.Version = 14;

            for (int idx = 0; idx < pdf_doc_output.Pages.Count; idx++)
            {
                PdfPage page = pdf_doc_output.Pages[idx];
                XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);
                gfx.DrawImage(bg, 0,0);
            }
            pdf_doc_output.Save(output_filename);
            Process.Start(output_filename);
        }


Any idea???


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 10, 2010 12:26 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3097
Location: Cologne, Germany
alegria wrote:
Maybe you can reproduce the error by using the (new) code:

I could try - but I won't coz that's not what I get paid for.
The problem could be related to your PDF files - and it'll work with my PDF files.

You can look at the exception details you get.
Maybe post them here if you are clueless.

Here's our sample code:
Code:
formGfx.DrawImage(XPdfForm.FromFile("../../../../../PDFs/SomeLayout.pdf"), new XRect(140, 80, 50, 50 * Math.Sqrt(2)));

http://www.pdfsharp.net/wiki/XForms-sample.ashx

Your code seems to do the same.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 10, 2010 1:32 pm 
Offline

Joined: Wed Mar 10, 2010 10:09 am
Posts: 4
Quote:
The problem could be related to your PDF files - and it'll work with my PDF files.

Maybe you're right. But I like to understand it or at least know how to affect this issue...

Quote:
You can look at the exception details you get.

Well I'm not a perfect programmer but I manage the task to include the PDFsharp source code in my solution (it's easy when you know how to do this, but for a beginner it can be difficult...).

In the "PdfObject.cs" File/Class I get in Line 297 the error "NullReferenceException". The Error occurs in the line:
Code:
PdfObject[] elements = externalObject.Owner.Internals.GetClosure(externalObject);

Any Idea???

Quote:
I could try - but I won't coz that's not what I get paid for.

So if you're interested to point out the error in a more effective way (sorry - but i can't pay millions of euro for your help...), I can send you the whole project including PdfSharp Source Code and my PDF example files???

Thanks for your reply...

P.S. I use the version v2.0.50727


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 10, 2010 1:50 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3097
Location: Cologne, Germany
alegria wrote:
In the "PdfObject.cs" File/Class I get in Line 297 the error "NullReferenceException".

So we're one step further: the exception occurs because there is something unusual about your PDF file (maybe there's an error in PDFsharp, maybe in the PDF file).

I don't know what
"Document -> Background -> Add/Replace -> Source :: File :: 'background_filename'"
does.

This code
Code:
formGfx.DrawImage(XPdfForm.FromFile("../../../../../PDFs/SomeLayout.pdf"), new XRect(140, 80, 50, 50 * Math.Sqrt(2)));

simply draws page 1 of an existing PDF file onto the current page of the new PDF file.
This only works if your file "background_filename" contains at least one page.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 10, 2010 4:34 pm 
Offline

Joined: Wed Mar 10, 2010 10:09 am
Posts: 4
As always - the solution is easy:

Code:
PdfDocument test = PdfReader.Open(background_filename);
test.Version = 15;
test.Save(background_filename);


Sorry for the confusion but I don't thought about the PDFversion...


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

All times are UTC


Who is online

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