Inline Structure Elements

In this step-by-step guide, we’ll show you how to use inline structure elements with Aspose.PDF for .NET. Aspose.PDF is a powerful library that lets you manipulate PDF documents programmatically. Inline structure elements allow you to create a hierarchical structure in your PDF document using headings of different levels and paragraphs.

Let’s dive into the code and learn how to use inline structure elements with Aspose.PDF for .NET.

Prerequisites

Before you begin, make sure you have the following:

  1. Aspose.PDF library for .NET installed.
  2. A basic knowledge of the C# programming language.

Step 1: Setting up the environment

To get started, open your C# development environment and create a new project. Make sure you have added a reference to the Aspose.PDF library for .NET in your project.

// The path to the documents directory.
string dataDir = "YOUR DOCUMENTS DIRECTORY";

Step 2: Creating the document

The first step is to create a new PDF document using the Document class.

// Create the PDF document
Document document = new Document();

Step 3: Work with tagged content

Then we get the tagged content of the document to work with.

// Get the tagged content of the document
ITaggedContent taggedContent = document.TaggedContent;

Step 4: Set document title and language

We can now set the document title and language.

// Define the document title and language
taggedContent.SetTitle("Tagged PDF document");
taggedContent.SetLanguage("fr-FR");

Step 5: Add structural elements online

Now we are going to add inline structure elements such as headings of different levels and paragraphs to our document.

// Get the root structure element
StructureElement rootElement = taggedContent.RootElement;

// Add headers of different levels
HeaderElement h1 = taggedContent.CreateHeaderElement(1);
HeaderElement h2 = taggedContent.CreateHeaderElement(2);
HeaderElement h3 = taggedContent.CreateHeaderElement(3);
HeaderElement h4 = taggedContent.CreateHeaderElement(4);
HeaderElement h5 = taggedContent.CreateHeaderElement(5);
HeaderElement h6 = taggedContent.CreateHeaderElement(6);
rootElement.AppendChild(h1);
rootElement.AppendChild(h2);
rootElement.AppendChild(h3);
rootElement.AppendChild(h4);
rootElement.AppendChild(h5);
rootElement.AppendChild(h6);

// Add content to each header
SpanElement spanH11 = taggedContent.CreateSpanElement();
spanH11.SetText("H1.");
h1.AppendChild(spanH11);
SpanElement spanH12 = taggedContent.CreateSpanElement();
spanH12.SetText("Level 1 header");
h1.AppendChild(spanH12);

SpanElement spanH21 = taggedContent.CreateSpanElement();
spanH21.SetText("H2.");
h2.AppendChild(spanH21);
SpanElement spanH22 = taggedContent.CreateSpanElement();
spanH22.SetText("Level 2 header");
h2.AppendChild(spanH22);

SpanElement spanH31 = taggedContent.CreateSpanElement();
spanH31.SetText("H3.");
h3.AppendChild(spanH31);
SpanElement spanH32 = taggedContent.CreateSpanElement();
spanH32.SetText("Level 3 header");
h3.AppendChild(spanH32);

SpanElement spanH41 = taggedContent.CreateSpanElement();
spanH41.SetText("H4.");
h4.AppendChild(spanH41);
SpanElement spanH42 = taggedContent.CreateSpanElement();
spanH42.SetText("Level 4 header");
h4.AppendChild(spanH42);

SpanElement spanH51 = taggedContent.CreateSpanElement();
spanH51.SetText("H5.");
h5.AppendChild(spanH51);
SpanElement spanH52 = taggedContent.CreateSpanElement();
spanH52.SetText("Level 5 header");
h5.AppendChild(spanH52);

SpanElement spanH61 = taggedContent.CreateSpanElement();
spanH61.SetText("H6.");
h6.AppendChild(spanH61);
SpanElement spanH62 = taggedContent.CreateSpanElement();
spanH62.SetText("Heading level 6");
h6.AppendChild(spanH62);

// Add a paragraph
ParagraphElement p = taggedContent.CreateParagraphElement();
p.SetText("P.");
rootElement.AppendChild(p);

// Add content to the paragraph
SpanElement span1 = taggedContent.CreateSpanElement();
span1.SetText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
p.AppendChild(span1);
SpanElement span2 = taggedContent.CreateSpanElement();
span2.SetText("Aenean nec lectus ac sem faucibus imperdiet.");
p.AppendChild(span2);
SpanElement span3 = taggedContent.CreateSpanElement();
span3.SetText("Sed ut erat ac magna ullamcorper hendrerit.");
p.AppendChild(span3);
SpanElement span4 = taggedContent.CreateSpanElement();
span4.SetText("Cras pellentesque libero semper, gravida magna sed, luctus leo.");
p.AppendChild(span4);
SpanElement span5 = taggedContent.CreateSpanElement();
span5.SetText("Fusce lectus odio, laoreet nec ullamcorper ut, molestie eu elit.");
p.AppendChild(span5);
SpanElement span6 = taggedContent.CreateSpanElement();
span6.SetText("Interdum et malesuada fames ac ante ipsum primis in faucibus. ");
p.AppendChild(span6);
SpanElement span7 = taggedContent.CreateSpanElement();
span7.SetText("Aliquam lacinia sit amet elit ac consectetur. So cursus condimentum ligula, vitae volutpat sem tristique eget. ");
p.AppendChild(span7);
SpanElement span8 = taggedContent.CreateSpanElement();
span8.SetText("Nulla in consectetur massa. Vestibulum vitae lobortis ante. Nulla ullamcorper pellentesque justo rhoncus accumsan. ");
p.AppendChild(span8);
SpanElement span9 = taggedContent.CreateSpanElement();
span9.SetText("Mauris ornare eu odio non lacinia. Aliquam massa leo, rhoncus ac iaculis eget, tempus et magna. Sed non consectetur elit.");
p.AppendChild(span9);
SpanElement span10 = taggedContent.CreateSpanElement();
span10.SetText("Sed vulputate, quam sed lacinia luctus, ipsum nibh fringilla purus, vitae posuere risus odio id massa. Cras sed venenatis lacus.");
p.AppendChild(span10);

Here we create inline structure elements, such as headings of different levels and a paragraph, and add content to them.

Step 6: Save the tagged PDF document

Finally, we save the tagged PDF document.

// Save the tagged PDF document
document.Save(dataDir + "InlineStructureElements.pdf");

Sample source code for Inline Structure Elements using Aspose.PDF for .NET


// The path to the documents directory.
string dataDir = "YOUR DOCUMENT DIRECTORY";

// Create Pdf Document
Document document = new Document();

// Get Content for work with TaggedPdf
ITaggedContent taggedContent = document.TaggedContent;

// Set Title and Language for Documnet
taggedContent.SetTitle("Tagged Pdf Document");
taggedContent.SetLanguage("en-US");

// Get Root Structure Element
StructureElement rootElement = taggedContent.RootElement;
HeaderElement h1 = taggedContent.CreateHeaderElement(1);
HeaderElement h2 = taggedContent.CreateHeaderElement(2);
HeaderElement h3 = taggedContent.CreateHeaderElement(3);
HeaderElement h4 = taggedContent.CreateHeaderElement(4);
HeaderElement h5 = taggedContent.CreateHeaderElement(5);
HeaderElement h6 = taggedContent.CreateHeaderElement(6);
rootElement.AppendChild(h1);
rootElement.AppendChild(h2);
rootElement.AppendChild(h3);
rootElement.AppendChild(h4);
rootElement.AppendChild(h5);
rootElement.AppendChild(h6);
SpanElement spanH11 = taggedContent.CreateSpanElement();
spanH11.SetText("H1. ");
h1.AppendChild(spanH11);
SpanElement spanH12 = taggedContent.CreateSpanElement();
spanH12.SetText("Level 1 Header");
h1.AppendChild(spanH12);
SpanElement spanH21 = taggedContent.CreateSpanElement();
spanH21.SetText("H2. ");
h2.AppendChild(spanH21);
SpanElement spanH22 = taggedContent.CreateSpanElement();
spanH22.SetText("Level 2 Header");
h2.AppendChild(spanH22);
SpanElement spanH31 = taggedContent.CreateSpanElement();
spanH31.SetText("H3. ");
h3.AppendChild(spanH31);
SpanElement spanH32 = taggedContent.CreateSpanElement();
spanH32.SetText("Level 3 Header");
h3.AppendChild(spanH32);
SpanElement spanH41 = taggedContent.CreateSpanElement();
spanH41.SetText("H4. ");
h4.AppendChild(spanH41);
SpanElement spanH42 = taggedContent.CreateSpanElement();
spanH42.SetText("Level 4 Header");
h4.AppendChild(spanH42);
SpanElement spanH51 = taggedContent.CreateSpanElement();
spanH51.SetText("H5. ");
h5.AppendChild(spanH51);
SpanElement spanH52 = taggedContent.CreateSpanElement();
spanH52.SetText("Level 5 Header");
h5.AppendChild(spanH52);
SpanElement spanH61 = taggedContent.CreateSpanElement();
spanH61.SetText("H6. ");
h6.AppendChild(spanH61);
SpanElement spanH62 = taggedContent.CreateSpanElement();
spanH62.SetText("Level 6 Header");
h6.AppendChild(spanH62);
ParagraphElement p = taggedContent.CreateParagraphElement();
p.SetText("P. ");
rootElement.AppendChild(p);
SpanElement span1 = taggedContent.CreateSpanElement();
span1.SetText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. ");
p.AppendChild(span1);
SpanElement span2 = taggedContent.CreateSpanElement();
span2.SetText("Aenean nec lectus ac sem faucibus imperdiet. ");
p.AppendChild(span2);
SpanElement span3 = taggedContent.CreateSpanElement();
span3.SetText("Sed ut erat ac magna ullamcorper hendrerit. ");
p.AppendChild(span3);
SpanElement span4 = taggedContent.CreateSpanElement();
span4.SetText("Cras pellentesque libero semper, gravida magna sed, luctus leo. ");
p.AppendChild(span4);
SpanElement span5 = taggedContent.CreateSpanElement();
span5.SetText("Fusce lectus odio, laoreet nec ullamcorper ut, molestie eu elit. ");
p.AppendChild(span5);
SpanElement span6 = taggedContent.CreateSpanElement();
span6.SetText("Interdum et malesuada fames ac ante ipsum primis in faucibus. ");
p.AppendChild(span6);
SpanElement span7 = taggedContent.CreateSpanElement();
span7.SetText("Aliquam lacinia sit amet elit ac consectetur. Donec cursus condimentum ligula, vitae volutpat sem tristique eget. ");
p.AppendChild(span7);
SpanElement span8 = taggedContent.CreateSpanElement();
span8.SetText("Nulla in consectetur massa. Vestibulum vitae lobortis ante. Nulla ullamcorper pellentesque justo rhoncus accumsan. ");
p.AppendChild(span8);
SpanElement span9 = taggedContent.CreateSpanElement();
span9.SetText("Mauris ornare eu odio non lacinia. Aliquam massa leo, rhoncus ac iaculis eget, tempus et magna. Sed non consectetur elit. ");
p.AppendChild(span9);
SpanElement span10 = taggedContent.CreateSpanElement();
span10.SetText("Sed vulputate, quam sed lacinia luctus, ipsum nibh fringilla purus, vitae posuere risus odio id massa. Cras sed venenatis lacus.");
p.AppendChild(span10);

// Save Tagged Pdf Document
document.Save(dataDir + "InlineStructureElements.pdf");

Conclusion

Congratulation ! You have learned how to use inline structure elements with Aspose.PDF for .NET. You can now create a hierarchical structure in your PDF document by using headings of different levels and paragraphs. Explore more features of Aspose.PDF to discover its full potential.

FAQ’s

Q: What are inline structure elements in a PDF document, and how do they contribute to creating a hierarchical structure?

A: Inline structure elements in a PDF document, such as headings of different levels and paragraphs, are used to create a hierarchical structure that organizes and presents content in a structured manner. These elements allow you to establish a clear hierarchy and flow of information within the document.

Q: How can inline structure elements enhance the readability and organization of a PDF document?

A: Inline structure elements, particularly headings and paragraphs, help improve the readability and organization of a PDF document by providing a logical structure. Headings indicate different levels of importance and help readers navigate the content, while paragraphs group related information together.

Q: How does Aspose.PDF for .NET facilitate the use of inline structure elements?

A: Aspose.PDF for .NET offers classes and methods to create and manipulate inline structure elements, such as headings and paragraphs. These elements can be customized, organized hierarchically, and enriched with content to improve the visual presentation and accessibility of the document.

Q: What is the purpose of the taggedContent object in relation to inline structure elements?

A: The taggedContent object, obtained from the TaggedContent property of a Document, allows you to work with structured elements, including inline structure elements. It enables you to create, customize, and organize headings and paragraphs within the document.

Q: How do inline structure elements aid in creating a clear document hierarchy?

A: Inline structure elements, such as headings of varying levels, contribute to establishing a clear and well-defined hierarchy in the document. Readers can quickly identify the main topics, subtopics, and related content, making the document easier to navigate and comprehend.

Q: Can I customize the appearance and formatting of inline structure elements using Aspose.PDF for .NET?

A: Yes, you can customize the appearance and formatting of inline structure elements. You can set properties such as font styles, sizes, colors, alignment, indentation, and spacing to achieve the desired visual presentation for headings and paragraphs.

Q: How do I create and add headings of different levels to a PDF document using inline structure elements in Aspose.PDF for .NET?

A: You can create headings of different levels using the CreateHeaderElement method and then append them to the root structure element. Subsequently, you can add content to each heading element using the CreateSpanElement method to create spans of text.

Q: Can I use inline structure elements to create lists, bullet points, or other types of content organization in a PDF document?

A: While inline structure elements themselves are primarily used for headings and paragraphs, you can use them in combination with other features offered by Aspose.PDF for .NET to create lists, bullet points, tables, and other types of content organization for a comprehensive document structure.

Q: How do inline structure elements contribute to document accessibility?

A: Inline structure elements play a crucial role in enhancing document accessibility. Properly structured headings and paragraphs provide a clear document hierarchy that aids screen readers and other assistive technologies in accurately interpreting and conveying the content to users with disabilities.

Q: Can I explore more advanced uses of inline structure elements, such as creating interactive elements or embedding multimedia?

A: Absolutely! While this tutorial focuses on creating headings and paragraphs, Aspose.PDF for .NET offers advanced features to create interactive elements, embed multimedia, add hyperlinks, and more. Check the library’s documentation and examples to delve into these advanced capabilities.