Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Apr 03, 2025 8:02 am

Hello,

We are trying out using a PdfTable in our document. When we assign the StructureElement to the PdfTable, we get a NullReferenceException. When we do not assign the StructureElement, the table works, but the table structure is (of course) not embedded in the document structure.
Here is a part of the code that triggers the error:

Code: Select all
var x = 2.0f;
var y = 2.0f
var pageStructure = document.DocumentStructure.AppendChildElement(PdfStandardStructTypes.Part);
var sectionStructure = pageStructure.AppendChildElement(PdfStandardStructTypes.Section);
var tableStructure = sectionStructure.AppendChildElement(PdfStandardStructTypes.Table);

String[] data = {
    Key;Description;Value",
    "1;First description;First value"
};
String[][] dataSource = new String[data.Length][];
for (int j = 0; j < data.Length; j++)
{
    dataSource[j] = data[j].Split(';');
}

var pdfTable = new PdfTable();
pdfTable.DataSource = dataSource;
pdfTable.StructureElement = tableStructure;
pdfTable.Columns.Add(new PdfColumn("Key"));
pdfTable.Columns.Add(new PdfColumn("Description"));
pdfTable.Columns.Add(new PdfColumn("Value"));

pdfTable.Draw(page, x, y);


This is the error:

System.NullReferenceException: Object reference not set to an instance of an object.
at spr꙲.悽()
at spr꙲.忾(Int32 A_0, PdfLayoutParams A_1, Boolean A_2)
at spr꙲.叁(PdfLayoutParams A_0)
at spr㧁.悽(PdfLayoutParams A_0)
at Spire.Pdf.Tables.PdfTable.Layout(PdfLayoutParams param)
at Spire.Pdf.Graphics.PdfLayoutWidget.Draw(PdfPageBase page, RectangleF layoutRectangle, PdfTextLayout format)
at Spire.Pdf.Graphics.PdfLayoutWidget.Draw(PdfPageBase page, Single x, Single y, PdfTextLayout format)
at Spire.Pdf.Graphics.PdfLayoutWidget.Draw(PdfPageBase page, Single x, Single y)
at ... (own code)


Can you tell us if we are doing something wrong here?

berrydictu
 
Posts: 22
Joined: Fri Dec 15, 2023 12:58 pm

Fri Apr 04, 2025 8:17 am

Hello,

Thanks for your inquiry.
Please test whether the following code meets your needs. If not, please reply to us with more details about your issue.
Code: Select all
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();

//Add a page
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins(20));

//Set tab order
page.SetTabOrder(TabOrder.Structure);

//Create an object of PdfTaggedContent class
PdfTaggedContent taggedContent = new PdfTaggedContent(doc);

//Set language and title for the document
taggedContent.SetLanguage("en-US");
taggedContent.SetTitle("test");

//Set PDF/UA1 identification
taggedContent.SetPdfUA1Identification();

//Create font and brush
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 14), true);
PdfSolidBrush brush = new PdfSolidBrush(Color.Black);

//Add a "document" element
PdfStructureElement document = taggedContent.StructureTreeRoot.AppendChildElement(PdfStandardStructTypes.Part);
var sectionStructure = document.AppendChildElement(PdfStandardStructTypes.Section);
var table = sectionStructure.AppendChildElement(PdfStandardStructTypes.Table);
//Add a "table" element
//PdfStructureElement table = document.AppendChildElement(PdfStandardStructTypes.Table);
PdfTable pdfTable = new PdfTable();
pdfTable.Style.DefaultStyle.Font = font;

String[] data = {
    "Key; Description; Value",
    "1;First description;First value"
 };
String[][] dataSource = new String[data.Length][];
for (int j = 0; j < data.Length; j++)
{
    dataSource[j] = data[j].Split(';');
}

pdfTable.DataSource = dataSource;
pdfTable.Style.ShowHeader = true;
pdfTable.StructureElement = table;
pdfTable.Draw(page.Canvas, new PointF(0, 280), 300f);
//Save the document to file
doc.SaveToFile("CreatePDFUA.pdf");
Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 686
Joined: Mon Dec 27, 2021 2:23 am

Fri Apr 04, 2025 1:44 pm

The difference between our implementation and your implementation is that we used another overload of the draw method. We used the (PdfPageBase, float, float). In your example code, you use the (PdfCanvas, PointF, float) overload.
When we use the first overload, the NullReferenceException occurs. When we use the second overload, it works like a charm. Looks like one of the overloads of the Draw method has some unexpected behaviour.

Now, when we continue developing with the table, we encounter another issue.
We would like to set the cellpadding only of the first cell of each row, and not of the following cells. We suspect that this was possible in an earlier version of Spire.PDF by accessing the Cells[i].Style.CellPadding property of a row, but that this was (accidentally?) removed in newer versions. Can you tell us if this is still possible?

berrydictu
 
Posts: 22
Joined: Fri Dec 15, 2023 12:58 pm

Mon Apr 07, 2025 10:14 am

Hello,

Thanks for your feedback.
To help us investigate your issue more accurately. Could you please write back and tell us what you are currently trying to achieve? Some screenshots might help.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 686
Joined: Mon Dec 27, 2021 2:23 am

Wed Apr 16, 2025 2:43 pm

We wanted to align text in the table exactly the way we did before, which we did by using rectangles. By using the PdfTable, there are other factors in play, like the borders, cellpadding, etc.
We now solved our issue by tweaking the column widths in stead of the cellpadding, so you can assume the issue is solved now. Thank you for your help :)

berrydictu
 
Posts: 22
Joined: Fri Dec 15, 2023 12:58 pm

Thu Apr 17, 2025 2:32 am

Hello,

Thansk for your feedback. If you have any other questions about our products in the future, please feel free to write to us.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 686
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.PDF

cron