إنشاء PDF مع علامات باستخدام C#
إنشاء PDF مع علامات يعني إضافة (أو إنشاء) عناصر معينة إلى الوثيقة التي ستتيح التحقق من الوثيقة وفقًا لمتطلبات PDF/UA. تُسمى هذه العناصر غالبًا عناصر الهيكل.
تعمل مقتطفات الكود التالية أيضًا مع مكتبة Aspose.PDF.Drawing .
إنشاء PDF مع علامات (سيناريو بسيط)
لإنشاء عناصر الهيكل في وثيقة PDF مع علامات، تقدم Aspose.PDF طرقًا لإنشاء عناصر الهيكل باستخدام واجهة ITaggedContent . تُظهر مقتطفات الكود التالية كيفية إنشاء PDF مع علامات يحتوي على عنصرين: عنوان وفقرة.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateTaggedPdfDocument01 ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Create PDF document
using ( var document = new Aspose . Pdf . Document ())
{
// Get Content for work with TaggedPdf
Aspose . Pdf . Tagged . ITaggedContent taggedContent = document . TaggedContent ;
var rootElement = taggedContent . RootElement ;
// Set Title and Language for Document
taggedContent . SetTitle ( "Tagged Pdf Document" );
taggedContent . SetLanguage ( "en-US" );
Aspose . Pdf . LogicalStructure . HeaderElement mainHeader = taggedContent . CreateHeaderElement ();
mainHeader . SetText ( "Main Header" );
Aspose . Pdf . LogicalStructure . ParagraphElement paragraphElement = taggedContent . CreateParagraphElement ();
paragraphElement . SetText ( "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
"Aenean nec lectus ac sem faucibus imperdiet. Sed ut erat ac magna ullamcorper hendrerit. " +
"Cras pellentesque libero semper, gravida magna sed, luctus leo. Fusce lectus odio, laoreet" +
"nec ullamcorper ut, molestie eu elit. Interdum et malesuada fames ac ante ipsum primis in faucibus." +
"Aliquam lacinia sit amet elit ac consectetur. Donec cursus condimentum ligula, vitae volutpat" +
"sem tristique eget. Nulla in consectetur massa. Vestibulum vitae lobortis ante. Nulla ullamcorper" +
"pellentesque justo rhoncus accumsan. Mauris ornare eu odio non lacinia. Aliquam massa leo, rhoncus" +
"ac iaculis eget, tempus et magna. Sed non consectetur elit. Sed vulputate, quam sed lacinia luctus," +
"ipsum nibh fringilla purus, vitae posuere risus odio id massa. Cras sed venenatis lacus." );
rootElement . AppendChild ( mainHeader );
rootElement . AppendChild ( paragraphElement );
// Save Tagged PDF Document
document . Save ( dataDir + "TaggedPdfDocument_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateTaggedPdfDocument01 ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Create PDF Document
using var document = new Aspose . Pdf . Document ();
// Get Content for work with TaggedPdf
Aspose . Pdf . Tagged . ITaggedContent taggedContent = document . TaggedContent ;
var rootElement = taggedContent . RootElement ;
// Set Title and Language for Document
taggedContent . SetTitle ( "Tagged Pdf Document" );
taggedContent . SetLanguage ( "en-US" );
Aspose . Pdf . LogicalStructure . HeaderElement mainHeader = taggedContent . CreateHeaderElement ();
mainHeader . SetText ( "Main Header" );
Aspose . Pdf . LogicalStructure . ParagraphElement paragraphElement = taggedContent . CreateParagraphElement ();
paragraphElement . SetText ( "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
"Aenean nec lectus ac sem faucibus imperdiet. Sed ut erat ac magna ullamcorper hendrerit. " +
"Cras pellentesque libero semper, gravida magna sed, luctus leo. Fusce lectus odio, laoreet" +
"nec ullamcorper ut, molestie eu elit. Interdum et malesuada fames ac ante ipsum primis in faucibus." +
"Aliquam lacinia sit amet elit ac consectetur. Donec cursus condimentum ligula, vitae volutpat" +
"sem tristique eget. Nulla in consectetur massa. Vestibulum vitae lobortis ante. Nulla ullamcorper" +
"pellentesque justo rhoncus accumsan. Mauris ornare eu odio non lacinia. Aliquam massa leo, rhoncus" +
"ac iaculis eget, tempus et magna. Sed non consectetur elit. Sed vulputate, quam sed lacinia luctus," +
"ipsum nibh fringilla purus, vitae posuere risus odio id massa. Cras sed venenatis lacus." );
rootElement . AppendChild ( mainHeader );
rootElement . AppendChild ( paragraphElement );
// Save Tagged PDF Document
document . Save ( dataDir + "TaggedPdfDocument_out.pdf" );
}
سنحصل على الوثيقة التالية بعد الإنشاء:
إنشاء PDF مع علامات مع عناصر متداخلة (إنشاء شجرة عناصر الهيكل)
في بعض الحالات، نحتاج إلى إنشاء هيكل أكثر تعقيدًا، على سبيل المثال، وضع اقتباسات في فقرة.
لإنشاء شجرة عناصر الهيكل يجب علينا استخدام طريقة AppendChild .
تُظهر مقتطفات الكود التالية كيفية إنشاء شجرة عناصر الهيكل لوثيقة PDF مع علامات:
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateTaggedPdfDocument02 ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Create PDF Document
using ( var document = new Aspose . Pdf . Document ())
{
// Get Content for work with TaggedPdf
Aspose . Pdf . Tagged . ITaggedContent taggedContent = document . TaggedContent ;
var rootElement = taggedContent . RootElement ;
// Set Title and Language for Document
taggedContent . SetTitle ( "Tagged Pdf Document" );
taggedContent . SetLanguage ( "en-US" );
Aspose . Pdf . LogicalStructure . HeaderElement header1 = taggedContent . CreateHeaderElement ( 1 );
header1 . SetText ( "Header Level 1" );
Aspose . Pdf . LogicalStructure . ParagraphElement paragraphWithQuotes = taggedContent . CreateParagraphElement ();
paragraphWithQuotes . StructureTextState . Font = Aspose . Pdf . Text . FontRepository . FindFont ( "Calibri" );
paragraphWithQuotes . AdjustPosition ( new Aspose . Pdf . Tagged . PositionSettings
{
Margin = new Aspose . Pdf . MarginInfo ( 10 , 5 , 10 , 5 )
});
Aspose . Pdf . LogicalStructure . SpanElement spanElement1 = taggedContent . CreateSpanElement ();
spanElement1 . SetText ( "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean nec lectus ac sem faucibus imperdiet. Sed ut erat ac magna ullamcorper hendrerit. Cras pellentesque libero semper, gravida magna sed, luctus leo. Fusce lectus odio, laoreet nec ullamcorper ut, molestie eu elit. Interdum et malesuada fames ac ante ipsum primis in faucibus. Aliquam lacinia sit amet elit ac consectetur. Donec cursus condimentum ligula, vitae volutpat sem tristique eget. Nulla in consectetur massa. Vestibulum vitae lobortis ante. Nulla ullamcorper pellentesque justo rhoncus accumsan. Mauris ornare eu odio non lacinia. Aliquam massa leo, rhoncus ac iaculis eget, tempus et magna. Sed non consectetur elit. " );
Aspose . Pdf . LogicalStructure . QuoteElement quoteElement = taggedContent . CreateQuoteElement ();
quoteElement . SetText ( "Sed vulputate, quam sed lacinia luctus, ipsum nibh fringilla purus, vitae posuere risus odio id massa." );
quoteElement . StructureTextState . FontStyle = Aspose . Pdf . Text . FontStyles . Bold | Aspose . Pdf . Text . FontStyles . Italic ;
Aspose . Pdf . LogicalStructure . SpanElement spanElement2 = taggedContent . CreateSpanElement ();
spanElement2 . SetText ( " Sed non consectetur elit." );
paragraphWithQuotes . AppendChild ( spanElement1 );
paragraphWithQuotes . AppendChild ( quoteElement );
paragraphWithQuotes . AppendChild ( spanElement2 );
rootElement . AppendChild ( header1 );
rootElement . AppendChild ( paragraphWithQuotes );
// Save Tagged PDF Document
document . Save ( dataDir + "TaggedPdfDocument_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateTaggedPdfDocument02 ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Create PDF Document
using var document = new Aspose . Pdf . Document ();
// Get Content for work with TaggedPdf
Aspose . Pdf . Tagged . ITaggedContent taggedContent = document . TaggedContent ;
var rootElement = taggedContent . RootElement ;
// Set Title and Language for Document
taggedContent . SetTitle ( "Tagged Pdf Document" );
taggedContent . SetLanguage ( "en-US" );
Aspose . Pdf . LogicalStructure . HeaderElement header1 = taggedContent . CreateHeaderElement ( 1 );
header1 . SetText ( "Header Level 1" );
Aspose . Pdf . LogicalStructure . ParagraphElement paragraphWithQuotes = taggedContent . CreateParagraphElement ();
paragraphWithQuotes . StructureTextState . Font = Aspose . Pdf . Text . FontRepository . FindFont ( "Calibri" );
paragraphWithQuotes . AdjustPosition ( new Aspose . Pdf . Tagged . PositionSettings
{
Margin = new Aspose . Pdf . MarginInfo ( 10 , 5 , 10 , 5 )
});
Aspose . Pdf . LogicalStructure . SpanElement spanElement1 = taggedContent . CreateSpanElement ();
spanElement1 . SetText ( "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean nec lectus ac sem faucibus imperdiet. Sed ut erat ac magna ullamcorper hendrerit. Cras pellentesque libero semper, gravida magna sed, luctus leo. Fusce lectus odio, laoreet nec ullamcorper ut, molestie eu elit. Interdum et malesuada fames ac ante ipsum primis in faucibus. Aliquam lacinia sit amet elit ac consectetur. Donec cursus condimentum ligula, vitae volutpat sem tristique eget. Nulla in consectetur massa. Vestibulum vitae lobortis ante. Nulla ullamcorper pellentesque justo rhoncus accumsan. Mauris ornare eu odio non lacinia. Aliquam massa leo, rhoncus ac iaculis eget, tempus et magna. Sed non consectetur elit. " );
Aspose . Pdf . LogicalStructure . QuoteElement quoteElement = taggedContent . CreateQuoteElement ();
quoteElement . SetText ( "Sed vulputate, quam sed lacinia luctus, ipsum nibh fringilla purus, vitae posuere risus odio id massa." );
quoteElement . StructureTextState . FontStyle = Aspose . Pdf . Text . FontStyles . Bold | Aspose . Pdf . Text . FontStyles . Italic ;
Aspose . Pdf . LogicalStructure . SpanElement spanElement2 = taggedContent . CreateSpanElement ();
spanElement2 . SetText ( " Sed non consectetur elit." );
paragraphWithQuotes . AppendChild ( spanElement1 );
paragraphWithQuotes . AppendChild ( quoteElement );
paragraphWithQuotes . AppendChild ( spanElement2 );
rootElement . AppendChild ( header1 );
rootElement . AppendChild ( paragraphWithQuotes );
// Save Tagged PDF Document
document . Save ( dataDir + "TaggedPdfDocument_out.pdf" );
}
سنحصل على الوثيقة التالية بعد الإنشاء:
تنسيق هيكل النص
لتهيئة هيكل النص في وثيقة PDF مع علامات، تقدم Aspose.PDF خصائص Font ، FontSize ، FontStyle وForegroundColor من فئة StructureTextState . تُظهر مقتطفات الكود التالية كيفية تنسيق هيكل النص في وثيقة PDF مع علامات:
توضيح عناصر الهيكل
لتوضيح عناصر الهيكل في وثيقة PDF مع علامات، تقدم Aspose.PDF فئة IllustrationElement . تُظهر مقتطفات الكود التالية كيفية توضيح عناصر الهيكل في وثيقة PDF مع علامات:
التحقق من PDF مع علامات
تقدم Aspose.PDF for .NET القدرة على التحقق من وثيقة PDF/UA مع علامات. يدعم التحقق من معيار PDF/UA:
التحقق من XObjects.
التحقق من الإجراءات.
التحقق من المحتوى الاختياري.
التحقق من الملفات المضمنة.
التحقق من حقول Acroform (التحقق من اللغة الطبيعية والاسم البديل والتوقيعات الرقمية).
التحقق من حقول نموذج XFA.
التحقق من إعدادات الأمان.
التحقق من التنقل.
التحقق من التعليقات التوضيحية.
تُظهر مقتطفات الكود أدناه كيفية التحقق من وثيقة PDF مع علامات. ستظهر المشاكل المقابلة في تقرير سجل XML.
ضبط موضع هيكل النص
تُظهر مقتطفات الكود التالية كيفية ضبط موضع هيكل النص في وثيقة PDF مع علامات:
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AdjustPosition ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Create PDF Document
using ( var document = new Aspose . Pdf . Document ())
{
// Get Content for work with TaggedPdf
var taggedContent = document . TaggedContent ;
// Set Title and Language for Document
taggedContent . SetTitle ( "Tagged Pdf Document" );
taggedContent . SetLanguage ( "en-US" );
// Create paragraph
var p = taggedContent . CreateParagraphElement ();
taggedContent . RootElement . AppendChild ( p );
p . SetText ( "Text." );
// Adjust position
p . AdjustPosition ( new Aspose . Pdf . Tagged . PositionSettings
{
HorizontalAlignment = Aspose . Pdf . HorizontalAlignment . None ,
Margin = new Aspose . Pdf . MarginInfo
{
Left = 300 ,
Right = 0 ,
Top = 20 ,
Bottom = 0
},
VerticalAlignment = Aspose . Pdf . VerticalAlignment . None ,
IsFirstParagraphInColumn = false ,
IsKeptWithNext = false ,
IsInNewPage = false ,
IsInLineParagraph = false
});
// Save Tagged PDF Document
document . Save ( dataDir + "AdjustTextPosition_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AdjustPosition ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Create PDF Document
using var document = new Aspose . Pdf . Document ();
// Get Content for work with TaggedPdf
var taggedContent = document . TaggedContent ;
// Set Title and Language for Document
taggedContent . SetTitle ( "Tagged Pdf Document" );
taggedContent . SetLanguage ( "en-US" );
// Create paragraph
var p = taggedContent . CreateParagraphElement ();
taggedContent . RootElement . AppendChild ( p );
p . SetText ( "Text." );
// Adjust position
p . AdjustPosition ( new Aspose . Pdf . Tagged . PositionSettings
{
HorizontalAlignment = Aspose . Pdf . HorizontalAlignment . None ,
Margin = new Aspose . Pdf . MarginInfo
{
Left = 300 ,
Right = 0 ,
Top = 20 ,
Bottom = 0
},
VerticalAlignment = Aspose . Pdf . VerticalAlignment . None ,
IsFirstParagraphInColumn = false ,
IsKeptWithNext = false ,
IsInNewPage = false ,
IsInLineParagraph = false
});
// Save Tagged PDF Document
document . Save ( dataDir + "AdjustTextPosition_out.pdf" );
}
إنشاء PDF مع علامات تلقائيًا مع تحويل PDF/UA-1
تتيح Aspose.PDF إنشاء تلقائي لعلامات الهيكل المنطقية الأساسية عند تحويل وثيقة إلى PDF/UA-1. يمكن للمستخدمين بعد ذلك تحسين هذه الهيكل المنطقي الأساسي يدويًا، مما يوفر رؤى إضافية حول محتويات الوثيقة.
لإنشاء هيكل وثيقة منطقي، قم بإنشاء مثيل من فئة Aspose.Pdf.AutoTaggingSettings ، واضبط خاصية AutoTaggingSettings.EnableAutoTagging على true
، وخصصها لخاصية PdfFormatConversionOptions.AutoTaggingSettings .
إذا كانت الوثيقة تحتوي بالفعل على علامات هيكل منطقي، فإن تمكين العلامات التلقائية سيدمر الهيكل المنطقي الحالي وينشئ هيكلًا جديدًا.
إنشاء PDF مع علامات مع حقول النموذج
يمكنك وضع علامات على حقول النموذج التفاعلية في وثيقة PDF لضمان تضمينها في شجرة الهيكل المنطقي للوصول (PDF/UA). تُظهر المقتطفات أدناه كيفية إنشاء حقل نموذج، وتسجيله في AcroForm، وربطه بعنصر هيكل /Form
في شجرة الهيكل.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreatePdfWithTaggedFormField ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Create PDF document
using ( var document = new Aspose . Pdf . Document ())
{
document . Pages . Add ();
// Get Content for work with TaggedPdf
Aspose . Pdf . Tagged . ITaggedContent taggedContent = document . TaggedContent ;
Aspose . Pdf . LogicalStructure . StructureElement rootElement = taggedContent . RootElement ;
// Create a visible signature form field (AcroForm)
var signatureField = new Aspose . Pdf . Forms . SignatureField ( document . Pages [ 1 ], new Aspose . Pdf . Rectangle ( 50 , 50 , 100 , 100 ));
signatureField . PartialName = "Signature1" ;
signatureField . AlternateName = "signature 1" ;
// Add the signature field to the document's AcroForm
document . Form . Add ( signatureField );
// Create a /Form structure element in the tag tree
Aspose . Pdf . LogicalStructure . FormElement form = taggedContent . CreateFormElement ();
form . AlternativeText = "form 1" ;
// Link the /Form tag to the signature field via an /OBJR reference
form . Tag ( signatureField );
// Add the /Form structure element to the document’s logical structure tree
rootElement . AppendChild ( form );
// Save PDF document
document . Save ( dataDir + "CreatePdfWithTaggedFormField_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreatePdfWithTaggedFormField ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Create PDF document
using var document = new Aspose . Pdf . Document ();
document . Pages . Add ();
// Get Content for work with TaggedPdf
Aspose . Pdf . Tagged . ITaggedContent taggedContent = document . TaggedContent ;
Aspose . Pdf . LogicalStructure . StructureElement rootElement = taggedContent . RootElement ;
// Create a visible signature form field (AcroForm)
var signatureField = new Aspose . Pdf . Forms . SignatureField ( document . Pages [ 1 ], new Aspose . Pdf . Rectangle ( 50 , 50 , 100 , 100 ));
signatureField . PartialName = "Signature1" ;
signatureField . AlternateName = "signature 1" ;
// Add the signature field to the document's AcroForm
document . Form . Add ( signatureField );
// Create a /Form structure element in the tag tree
Aspose . Pdf . LogicalStructure . FormElement form = taggedContent . CreateFormElement ();
form . AlternativeText = "form 1" ;
// Link the /Form tag to the signature field via an /OBJR reference
form . Tag ( signatureField );
// Add the /Form structure element to the document’s logical structure tree
rootElement . AppendChild ( form );
// Save PDF document
document . Save ( dataDir + "CreatePdfWithTaggedFormField_out.pdf" );
}
إنشاء PDF مع علامات مع صفحة جدول المحتويات (TOC)
قم بإنشاء وثيقة PDF مع علامات تحتوي على صفحة جدول محتويات (TOC) قابلة للوصول.
قم بإنشاء PDF مع علامات يتضمن صفحة جدول محتويات قابلة للوصول، تحتوي على عنوان الصفحة وقائمة فرعية متداخلة.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreatePdfWithTOCpageAdvanced ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Create PDF document
using ( var doc = new Aspose . Pdf . Document ())
{
// Get tagged content for the PDF structure
Aspose . Pdf . Tagged . ITaggedContent content = doc . TaggedContent ;
Aspose . Pdf . LogicalStructure . StructureElement rootElement = content . RootElement ;
content . SetLanguage ( "en-US" );
// Add the table of contents (TOC) page
Aspose . Pdf . Page tocPage = doc . Pages . Add ();
tocPage . TocInfo = new Aspose . Pdf . TocInfo ();
tocPage . TocInfo . Title = new Aspose . Pdf . Text . TextFragment ( "Table of Contents" );
// Create a TOC structure element
Aspose . Pdf . LogicalStructure . TOCElement tocElement = content . CreateTOCElement ();
// Create a header element for the TOC page title
Aspose . Pdf . LogicalStructure . HeaderElement headerForTOCpageTitle = content . CreateHeaderElement ( 1 );
tocElement . LinkTocPageTitleToHeaderElement ( tocPage , headerForTOCpageTitle );
// Add the TOC page title header and TOC element to the document structure tree
rootElement . AppendChild ( headerForTOCpageTitle );
rootElement . AppendChild ( tocElement );
// Add a content page
doc . Pages . Add ();
// Create a header element and set its text
Aspose . Pdf . LogicalStructure . HeaderElement header = content . CreateHeaderElement ( 1 );
header . SetText ( "1. Header" );
// Add the header to the document structure
rootElement . AppendChild ( header );
// Create a TOC item (TOCI) element
Aspose . Pdf . LogicalStructure . TOCIElement toci = content . CreateTOCIElement ();
// Add the TOCI element to the TOC element
tocElement . AppendChild ( toci );
// Add an entry to the TOC page and link it to the TOCI element
header . AddEntryToTocPage ( tocPage , toci );
// Add a logical reference to the header within the TOCI element
toci . AddRef ( header );
// Create a list element for TOCI subitems
Aspose . Pdf . LogicalStructure . ListElement listElement = content . CreateListElement ();
for ( var i = 1 ; i <= 3 ; i ++)
{
// Create a list item (LI) element
Aspose . Pdf . LogicalStructure . ListLIElement li = content . CreateListLIElement ();
// Add the list item to the list element
listElement . AppendChild ( li );
// Create a sub-header element and set its properties
Aspose . Pdf . LogicalStructure . HeaderElement subHeader = content . CreateHeaderElement ( 2 );
subHeader . StructureTextState . FontSize = 14 ;
subHeader . Language = "en-US" ;
subHeader . SetText ( $@"1.{i} subheader " );
// Add an entry to the TOC page and link it to the LI element
subHeader . AddEntryToTocPage ( tocPage , li );
// Add a logical reference to the subheader element
li . AddRef ( subHeader );
// Create a paragraph element and set its text and language
Aspose . Pdf . LogicalStructure . ParagraphElement p = content . CreateParagraphElement ();
p . SetText ( "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." );
p . Language = "en-US" ;
// Add the sub-header and paragraph to the document structure
rootElement . AppendChild ( subHeader );
rootElement . AppendChild ( p );
}
// Add the list element as a child to the TOCI element
toci . AppendChild ( listElement );
// --- Additional TOC header example ---
// Create a second header element (see comments above for header 1)
Aspose . Pdf . LogicalStructure . HeaderElement header2 = content . CreateHeaderElement ( 1 );
header2 . SetText ( "2. Header" );
rootElement . AppendChild ( header2 );
Aspose . Pdf . LogicalStructure . TOCIElement toci2 = content . CreateTOCIElement ();
tocElement . AppendChild ( toci2 );
header2 . AddEntryToTocPage ( tocPage , toci2 );
toci2 . AddRef ( header2 );
// Save the PDF document
doc . Save ( dataDir + "CreatePdfWithTOCpageAdvanced_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreatePdfWithTOCpageAdvanced ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Create PDF document
using var doc = new Aspose . Pdf . Document ();
// Get tagged content for the PDF structure
Aspose . Pdf . Tagged . ITaggedContent content = doc . TaggedContent ;
Aspose . Pdf . LogicalStructure . StructureElement rootElement = content . RootElement ;
content . SetLanguage ( "en-US" );
// Add the table of contents (TOC) page
Aspose . Pdf . Page tocPage = doc . Pages . Add ();
tocPage . TocInfo = new Aspose . Pdf . TocInfo ();
tocPage . TocInfo . Title = new Aspose . Pdf . Text . TextFragment ( "Table of Contents" );
// Create a TOC structure element
Aspose . Pdf . LogicalStructure . TOCElement tocElement = content . CreateTOCElement ();
// Create a header element for the TOC page title
Aspose . Pdf . LogicalStructure . HeaderElement headerForTOCpageTitle = content . CreateHeaderElement ( 1 );
tocElement . LinkTocPageTitleToHeaderElement ( tocPage , headerForTOCpageTitle );
// Add the TOC page title header and TOC element to the document structure tree
rootElement . AppendChild ( headerForTOCpageTitle );
rootElement . AppendChild ( tocElement );
// Add a content page
doc . Pages . Add ();
// Create a header element and set its text
Aspose . Pdf . LogicalStructure . HeaderElement header = content . CreateHeaderElement ( 1 );
header . SetText ( "1. Header" );
// Add the header to the document structure
rootElement . AppendChild ( header );
// Create a TOC item (TOCI) element
Aspose . Pdf . LogicalStructure . TOCIElement toci = content . CreateTOCIElement ();
// Add the TOCI element to the TOC element
tocElement . AppendChild ( toci );
// Add an entry to the TOC page and link it to the TOCI element
header . AddEntryToTocPage ( tocPage , toci );
// Add a logical reference to the header within the TOCI element
toci . AddRef ( header );
// Create a list element for TOCI subitems
Aspose . Pdf . LogicalStructure . ListElement listElement = content . CreateListElement ();
for ( var i = 1 ; i <= 3 ; i ++)
{
// Create a list item (LI) element
Aspose . Pdf . LogicalStructure . ListLIElement li = content . CreateListLIElement ();
// Add the list item to the list element
listElement . AppendChild ( li );
// Create a sub-header element and set its properties
Aspose . Pdf . LogicalStructure . HeaderElement subHeader = content . CreateHeaderElement ( 2 );
subHeader . StructureTextState . FontSize = 14 ;
subHeader . Language = "en-US" ;
subHeader . SetText ( $@"1.{i} subheader " );
// Add an entry to the TOC page and link it to the LI element
subHeader . AddEntryToTocPage ( tocPage , li );
// Add a logical reference to the subheader element
li . AddRef ( subHeader );
// Create a paragraph element and set its text and language
Aspose . Pdf . LogicalStructure . ParagraphElement p = content . CreateParagraphElement ();
p . SetText ( "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." );
p . Language = "en-US" ;
// Add the sub-header and paragraph to the document structure
rootElement . AppendChild ( subHeader );
rootElement . AppendChild ( p );
}
// Add the list element as a child to the TOCI element
toci . AppendChild ( listElement );
// --- Additional TOC header example ---
// Create a second header element (see comments above for header 1)
Aspose . Pdf . LogicalStructure . HeaderElement header2 = content . CreateHeaderElement ( 1 );
header2 . SetText ( "2. Header" );
rootElement . AppendChild ( header2 );
Aspose . Pdf . LogicalStructure . TOCIElement toci2 = content . CreateTOCIElement ();
tocElement . AppendChild ( toci2 );
header2 . AddEntryToTocPage ( tocPage , toci2 );
toci2 . AddRef ( header2 );
// Save the PDF document
doc . Save ( dataDir + "CreatePdfWithTOCpageAdvanced_out.pdf" );
}