PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 11:09 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 Nov 21, 2023 11:53 am 
Offline

Joined: Tue Nov 21, 2023 11:48 am
Posts: 3
public void Save(string savePath)
{
try
{
if (pdfRenderer != null)
pdfRenderer = null;

// Create a PDF renderer and render the MigraDoc document to it.
pdfRenderer = new PdfDocumentRenderer()
{
Document = reportDocument.Clone()
};

pdfRenderer.RenderDocument();

if (!File.Exists(savePath))
{
// Save the PDF document to a file.
pdfRenderer.PdfDocument.Save(savePath);
}

pdfRenderer.PdfDocument.Close();
pdfRenderer.PdfDocument.Dispose();

}
catch(Exception ex)
{
}
}


I need help for pdfRenderer.RenderDocument() , i got the following error:

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=MigraDoc.DocumentObjectModel
StackTrace:
at MigraDoc.DocumentObjectModel.Visitors.VisitorBase.VisitHeaderFooter(HeaderFooter headerFooter)
at MigraDoc.DocumentObjectModel.HeaderFooter.MigraDoc.DocumentObjectModel.Visitors.IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, Boolean visitChildren)
at MigraDoc.DocumentObjectModel.HeadersFooters.MigraDoc.DocumentObjectModel.Visitors.IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, Boolean visitChildren)
at MigraDoc.DocumentObjectModel.Section.MigraDoc.DocumentObjectModel.Visitors.IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, Boolean visitChildren)
at MigraDoc.DocumentObjectModel.Sections.MigraDoc.DocumentObjectModel.Visitors.IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, Boolean visitChildren)
at MigraDoc.DocumentObjectModel.Document.MigraDoc.DocumentObjectModel.Visitors.IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, Boolean visitChildren)
at MigraDoc.DocumentObjectModel.Visitors.VisitorBase.Visit(DocumentObject documentObject)
at MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely)
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
at MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()
at ReportGenerator.LatestAttachDieSummaryReport.Save(String savePath) in C:\Users\Desktop\Create PDF using PDFSharp\SummaryReport\ReportGenerator\LatestAttachDieSummaryReport.cs:line 797


Last edited by thomasTan on Tue Nov 21, 2023 12:16 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 21, 2023 12:44 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 916
Location: CCAA
Cannot reproduce.

See also:
http://forum.pdfsharp.net/viewtopic.php?f=2&t=832

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 22, 2023 1:44 am 
Offline

Joined: Tue Nov 21, 2023 11:48 am
Posts: 3
I found out that the root cause is because of Table.SetEdge(int clm, int row, int clms, int rows, Edge edge, BorderStyle style, Unit width).

The error below will prompt during save the document as pdf when Table.SetEdge(int clm, int row, int clms, int rows, Edge edge, BorderStyle style, Unit width) is executed after i add the table to the section. I am wondering why?

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=MigraDoc.DocumentObjectModel


Error Code:
private void DefineTableForCustomerInfo()
{
// Add an empty paragraph for spacing.
section.AddParagraph();

//Add a paragraph for the Customer Information
tableCustomerInfo = section.AddTable();
tableCustomerInfo.Format.Font.Name = ReportDocumentFontName; // Font name
tableCustomerInfo.Format.Font.Size = 9; // Adjust the font size as needed

// Create and populate the table for Customer Information
CreateAndPopulateTable(tableCustomerInfo, 5, 7, tableCustomerInfo.Format.Font.Size.Centimeter, ColumnWidthCM, false);

//Predefine the column width
double[] columnWidths = { 3.75, 0.25, 5.25, 0.5, 3.75, 0.25, 5.25 };

for (int i = 0; i < columnWidths.Length; i++)
{
FormatColumn(tableCustomerInfo.Rows[0].Cells[i], columnWidths[i]);
}

tableCustomerInfo.Rows[0].Cells[0].MergeRight = 6;
tableCustomerInfo.Rows[0].Format.Font.Bold = true;
tableCustomerInfo.Rows[0].Format.Alignment = ParagraphAlignment.Center;
tableCustomerInfo.Rows[0].Cells[0].Shading.Color = Colors.Silver;
tableCustomerInfo.Rows[0].Cells[0].AddParagraph("CUSTOMER INFORMATION");

tableCustomerInfo.SetEdge(0, 0, 1, 1, Edge.Box, BorderStyle.Single, 1);
tableCustomerInfo.SetEdge(0, 0, tableCustomerInfo.Columns.Count, tableCustomerInfo.Rows.Count, Edge.Box, BorderStyle.Single, 1);
}


Pass Code:
private void DefineTableForCustomerInfo()
{
// Add an empty paragraph for spacing.
section.AddParagraph();

//tableCustomerInfo = section.AddTable();

tableCustomerInfo = new Table();
tableCustomerInfo.Format.Font.Name = ReportDocumentFontName; // Font name
tableCustomerInfo.Format.Font.Size = 9; // Adjust the font size as needed

// Create and populate the table for Customer Information
CreateAndPopulateTable(tableCustomerInfo, 5, 7, tableCustomerInfo.Format.Font.Size.Centimeter, ColumnWidthCM, false);

//Predefine the column width
double[] columnWidths = { 3.75, 0.25, 5.25, 0.5, 3.75, 0.25, 5.25 };

for (int i = 0; i < columnWidths.Length; i++)
{
FormatColumn(tableCustomerInfo.Rows[0].Cells[i], columnWidths[i]);
}


tableCustomerInfo.SetEdge(0, 0, 1, 1, Edge.Box, BorderStyle.Single, 1);
tableCustomerInfo.SetEdge(0, 0, tableCustomerInfo.Columns.Count, tableCustomerInfo.Rows.Count, Edge.Box, BorderStyle.Single, 1);

tableCustomerInfo.Rows[0].Cells[0].MergeRight = 6;
tableCustomerInfo.Rows[0].Format.Font.Bold = true;
tableCustomerInfo.Rows[0].Format.Alignment = ParagraphAlignment.Center;
tableCustomerInfo.Rows[0].Cells[0].Shading.Color = Colors.Silver;
tableCustomerInfo.Rows[0].Cells[0].AddParagraph("CUSTOMER INFORMATION");

section.Add(tableCustomerInfo);

}


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 22, 2023 9:01 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 916
Location: CCAA
Thanks for the feedback. Still I just see code snippets.
Are you using version 6.0.0?

Cannot reproduce.

See also:
http://forum.pdfsharp.net/viewtopic.php?f=2&t=832

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 22, 2023 9:19 am 
Offline

Joined: Tue Nov 21, 2023 11:48 am
Posts: 3
I m using PDFsharp-MigraDoc-gdi, version 1.50.5147 as i m dealing with winform and .Net framework 4.7.2


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: Bing [Bot] and 375 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