PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue May 21, 2024 5:21 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: Tue May 27, 2014 11:28 am 
Offline

Joined: Tue Jun 18, 2013 7:48 am
Posts: 8
Hi support,

we got an issue regarding DefaultPageSetup. If we try to count pages with a function as follows

Code:
public static int CountPages(Section section)
{
   DocumentRenderer docRenderer = new DocumentRenderer(section.Document);
   docRenderer.PrepareDocument();
   return docRenderer.FormattedDocument.PageCount;
}


we get an Exception:

Quote:
Exception: System.NullReferenceException
Message: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
Source: MigraDoc.DocumentObjectModel
bei MigraDoc.DocumentObjectModel.PageSetup.get_DefaultPageSetup()
bei MigraDoc.DocumentObjectModel.Visitors.VisitorBase.VisitSection(Section section)
bei MigraDoc.DocumentObjectModel.Section.MigraDoc.DocumentObjectModel.Visitors.IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, Boolean visitChildren)
bei MigraDoc.DocumentObjectModel.Sections.MigraDoc.DocumentObjectModel.Visitors.IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, Boolean visitChildren)
bei MigraDoc.DocumentObjectModel.Document.MigraDoc.DocumentObjectModel.Visitors.IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, Boolean visitChildren)
bei MigraDoc.DocumentObjectModel.Visitors.VisitorBase.Visit(DocumentObject documentObject)
bei MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
bei Dornbracht.Documents.Pdf.PdfUtil.CountPages(Section section)


It seems this issue only occurs when two PDFs are generated at the same time, but extreme rarely. We didn't changed anything at DefaultPageSetup so we think, this could be a bug?

Any help would be appreciated.

Best regards
FEN


Top
 Profile  
Reply with quote  
PostPosted: Tue May 27, 2014 11:43 am 
Offline
PDFsharp Guru
User avatar

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

This is "Ask the community", not "Ask the support".

You do not show the code that creates the document (do you create any?), you do not show the code that adds the section to the document.

See also:
http://www.pdfsharp.net/wiki/MigraDocHe ... ample.ashx

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Jun 06, 2014 12:43 pm 
Offline

Joined: Tue Jun 18, 2013 7:48 am
Posts: 8
Hi Thomas,

sorry for that mistake, i thought this is the support forum. Hi Community :) Surely we creates a document and also sections to the document. Here's the code:

Code:
public static void CreateDoc(Language language, out Document migraDocument, out Section section, double marginTop = 4.7, double marginLeft = 1.5, double marginBottom = 3.3, int fontSize = 10)
{
   // Create a new MigraDoc document
   migraDocument = new Document();
   CreateSection(language,migraDocument,out section,marginTop,marginLeft,marginBottom,fontSize);
}

public static void CreateSection(Language language, Document migraDocument, out Section section, double marginTop = 4.7, double marginLeft = 1.5, double marginBottom = 3.3, int fontSize = 10)
{
   // Create a new MigraDoc document
   section = migraDocument.AddSection();
         
   section.PageSetup.LeftMargin = XUnit.FromCentimeter(marginLeft).Point;
   section.PageSetup.RightMargin = XUnit.FromCentimeter(marginLeft).Point;
   section.PageSetup.TopMargin = XUnit.FromCentimeter(marginTop).Point;
   section.PageSetup.BottomMargin = XUnit.FromCentimeter(marginBottom).Point;
}


With these two static methods, we initialize documents for a number of PDFs. A call to these methods looks like as follows:

Code:
public PdfDocument CreatePdf()
{
   string titleOfDocument = "Titel";
   try
   {
      Document document;
      Section section;
      PdfUtil.CreateDoc(_language, out document, out section, 5.0, 1.2, 2.0);

      addHeader(section);
      CreatePage(section);
      addFooter(section);

      return PdfUtil.RenderPdf(document, titleOfDocument);
   }
   catch (Exception ex)
   {
      PdfUtil.SendErrorMail(ex, titleOfDocument, Context.Language.ToString());
      return null;
   }
}


The CountPages function call is inside the addFooter function:

Code:
private void addFooter(Section section)
{
   Table table;
   createTwoColumnTable(section,out table);
         
   Row row = table.AddRow();
   row.VerticalAlignment = VerticalAlignment.Bottom;

   //Seitenanzahl
   Paragraph paragraphFooter = new Paragraph();
   paragraphFooter.Format.Font.Size = 9;
   paragraphFooter.Format.LineSpacing = 9;
   paragraphFooter.Format.Font.Color = _blackColor;

   int countPages = PdfUtil.CountPages(section);
         
   paragraphFooter.AddText("Seite " + countPages);
   row[0].Add(paragraphFooter);

}



I hope my explanations are comprehensible. Could this issue be associated with the size of a pdf file?

Best regards
Fabian


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 10, 2014 8:08 am 
Offline
PDFsharp Guru
User avatar

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

Delete the CountPages method and use MigraDoc in the intended way: just call "paragraph.AddPageField();" to have MigraDoc add the current page number to your document while MigraDoc renders the document once. Use "AddNumPagesField()" to get the total page count of the document or "AddSectionPagesField()" to get the page count of the section.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Jun 16, 2014 8:43 am 
Offline

Joined: Tue Jun 18, 2013 7:48 am
Posts: 8
Hi Thomas,

thanks for the tip, that does the trick. I don't know, why we create an own function for counting pages, but "AddNumPagesField()" works like a charm.

Best regards


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