PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Apr 28, 2024 6:29 am

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 Feb 26, 2014 4:10 pm 
Offline

Joined: Wed Feb 26, 2014 3:48 pm
Posts: 8
In VS2010 using MigraDoc if I run my .NET4.0 Windows application with a blank text field I get an exception;

ArgumentNullException was unhandled

This is pointing at ParagraphElements.cs > (beginning on Line 81)

Code:

public Text AddText(string text)
    {
      if (text == null)
        throw new ArgumentNullException("text");

............ more code




I was unable to locate exception handling generally and specifically on this error. It maybe a newb mistake and my apologies for bothering you.

Ultimately I stepped outside of the code where the MigraDoc begins and placed some simple code

Code:


            if (Pnumber == null)
            {

                MessageBox.Show("Please enter a Project Number.");
                return;

            }

            if (Pname == null)
            {

                MessageBox.Show("Please enter a Project Name.");
                return;
            }


            if (Prev == null)
            {

                MessageBox.Show("Please enter a Project Revision level.");
                return;
            }



This did not work within the basic creation of the MigraDoc pdf (which I basically copied and pasted together from the sample documentation).

Code:

Document document = CreateDocument();

            document.UseCmykColor = true;

            //document.DefaultPageSetup.PageHeight = DPS.PageHeight;
            //document.DefaultPageSetup.PageWidth = DPS.PageWidth;

            // string ddl = MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToString(document);

            ////#if true_
            ////      RtfDocumentRenderer renderer = new RtfDocumentRenderer();
            ////      renderer.Render(document, "HelloWorld.rtf", null);
            ////#endif

            // ----- Unicode encoding and font program embedding in MigraDoc is demonstrated here -----

            // A flag indicating whether to create a Unicode PDF or a WinAnsi PDF file.
            // This setting applies to all fonts used in the PDF document.
            // This setting has no effect on the RTF renderer.
            const bool unicode = false;


<<More samples and comments>>

            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(unicode, embedding);

            // Associate the MigraDoc document with a renderer
            pdfRenderer.Document = document;

            // Layout and render document to PDF
            pdfRenderer.RenderDocument();

            // Save the document...

            string filename = Pnumber + "-SCG.pdf";
            ////const string filename = "HelloWorld.pdf";
            pdfRenderer.PdfDocument.Save(filename);
            // ...and start a viewer.
            Process.Start(filename);
        }

        /// <summary>
        /// Creates an absolutely minimalistic document.
        /// </summary>
        static Document CreateDocument()
        {

<< the other part I copied and added to>>

return document;



My questions are:

1. Is this the appropriate way to handle exceptions when using MigraDoc that will throw user error exceptions, and
2. Is there any documentation explaining better exception handling of user errors when the MigraDoc statements have already been called?

Thank you very much for your assistance. I'm looking forward to working with MigraDoc and PDFsharp. It looks fascinating and isn't terribly difficult to use. I hope it performs as I've been told :)

Have a Great Day

Frank Pytel


Top
 Profile  
Reply with quote  
PostPosted: Wed Feb 26, 2014 4:25 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
Hi, Frank,

I'm afraid I do not fully understand your questions.

Calling AddText("") will work fine without exception.
Calling AddText(null) will lead to an exception.

When using a TextBox in the GUI, you normally do not get null if the box is left empty. So normally everything works fine.

In your case you could use code like AddText(Pname ?? "") to make the program work even with empty fields.
Or something like this:
Code:
if (Pname != null)
    AddText(Pname)


AddText throws ArgumentNullException to tell your application that something went wrong. This exception should be handled in your code.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Feb 26, 2014 4:45 pm 
Offline

Joined: Wed Feb 26, 2014 3:48 pm
Posts: 8
And again, I messed up. :shock:

The way the program works is that I am not getting the information directly from a textBox. The textBox passes the information to an XML element first. The XML element is made accessible in real time to the entirety of the program. So the path would be;

textBox1.Pname > <textBox1Pname>NULL</textBox1Pname> > string Pname = Ref.subRef.textBox1Pname;

I had no problem referencing a blank textbox and creating the pdf. It simply leaves that field blank and processes the balance of the information. But because I am now accessing the XML container it is reading a null value. My guess is this condition, null objects, would be a rarity but.... Apples and barrels and all. :)

I realize this was critical. My apologies. It seems that until some process is called the XML element is null. This is throwing the exception from the above named .cs file (which I am assuming is part of MigraDoc as I don't have it in my solution). Personally I would like to just have a value pre assigned to all elements, but that is above my pay grade :D That is why I had to provide some error handling. And yes, I am working with the GUI+ and not the WPF foundation.

To my original questions I was wondering if you have any documentation about error handling within the MigraDoc or PDFsharp code. That is, are there any particular ways that errors should be handled when calling MigraDoc or PDFsharp statements that would be more beneficial than another route during any portion of the process. From the answer below I assume that the error handling is a function of C# and I just had not properly thought out my error handling within the bounds of your product.

As to the appropriateness question, I suppose if it works it's appropriate :lol: but I was hoping to hear it from someone here so I would know if some known issue exists where I might run into trouble down the road with what I've done.

Thank you very much, Thomas, for responding so quickly. I'm sure y'all have more important things to do than mess with this newb, but I very much appreciate that you did.

Have a Great Day.

Frank Pytel


Top
 Profile  
Reply with quote  
PostPosted: Wed Feb 26, 2014 5:46 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
frankpytel wrote:
And again, I messed up.
I wouldn't say that.
The creator of PDFsharp thought that passing null should be treated as an error and throws an exception. He could have decided to treat null and "" the same way - but he didn't.

I guess I have written string.IsNullOrEmpty more than thousand times by now.
Like this:
Code:
if (!string.IsNullOrEmpty(Pname))
    AddText(Pname)


Re error handling:
We use a framework where empty strings are "" and not null so we have no problems using our strings with MigraDoc.
With your framework I'd use AddText(Pname ?? "") or code like above.
MigraDoc normally only throws exceptions when you use it incorrectly. I add some if statements during development and testing to avoid exceptions for problems that can be expected.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Feb 26, 2014 6:01 pm 
Offline

Joined: Wed Feb 26, 2014 3:48 pm
Posts: 8
Copy that. Thanks Thomas

Frank Pytel


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: Google [Bot] and 365 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