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: 25
Joined: Fri Dec 15, 2023 12:58 pm

Fri Apr 04, 2025 8:14 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: 757
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.PDF

cron