Header and footer in Tagged Aspose.PDF

Hello Aspose,

We use Aspose.PDF .NET for generation pdf file with user report result. Previously we used a table class and approach that mentioned here Create or Add Table In PDF using C#|Aspose.PDF for .NET. Now we have new request to make our documents readable. We change the file generation from using the table class to using the tags Create Tagged PDF using C#|Aspose.PDF for .NET. It works fine for the table. But we have additional use case. User can add header and footer to the file. We do not found any information how to add header and footer for the tagged PDF file. At the moment we have only come up with a workaround. The code with the example is below. To resolve it for the current moment we use next steps:

  1. Generate Tagged PDF
  2. Save PDF file. This is necessary so that the document will have pages.
  3. Go through all the pages and add HeaderFooter to each page.
  4. Save result file.

This is just workaround to show the header and footer. But this approach do not resolve the issue with accessibility.
We have several question to resolve it:

  1. How we can add header and footer Tagged PDF to avoid using HeaderFooter?
  2. Do we have any opportunity to process the Pages of Document without saving?
using Aspose.Pdf;
using Aspose.Pdf.LogicalStructure;
using Aspose.Pdf.Text;

Document pdfDocument = new Document();

var taggedContent = pdfDocument.TaggedContent;


TableElement taggedTable = taggedContent.CreateTableElement();
taggedTable.RepeatingRowsCount = 3;
taggedContent.RootElement.AppendChild(taggedTable);

var headerElement = taggedContent.CreateHeaderElement(1);
headerElement.SetText("Header Element");
taggedContent.RootElement.AppendChild(headerElement);

var taggedHeaderRow = taggedTable.CreateTHead();
var tableHeaderRow = taggedHeaderRow.CreateTR();
tableHeaderRow.CreateTD().SetText("Header 1");
tableHeaderRow.CreateTD().SetText("Header 2");
tableHeaderRow.CreateTD().SetText("Header 3");

var body = taggedTable.CreateTBody();

for (int i = 0; i < 100; i++)
{
   var taggedDataRow = body.CreateTR();
   taggedDataRow.CreateTD().SetText("Row " + i + " Col 1");
   taggedDataRow.CreateTD().SetText("Row " + i + " Col 1");
   taggedDataRow.CreateTD().SetText("Row " + i + " Col 1");
}

pdfDocument.Save("output2.pdf");

foreach (var page in pdfDocument.Pages) 
{
   var footer = new HeaderFooter();
   footer.Paragraphs.Add(new TextFragment("Somne"));
   page.Footer = footer;

   var header = new HeaderFooter();
   header.Paragraphs.Add(new TextFragment("Somn e 2"));
   page.Header = header;
}

pdfDocument.Save("output2.pdf");

@Raman_Panfilenka

To add headers and footers to a tagged PDF in Aspose.PDF without using the HeaderFooter class, you can utilize the tagged content structure to create header and footer elements directly. This approach ensures that the document remains accessible and compliant with PDF/UA standards.

Steps to Add Header and Footer in Tagged PDF

  1. Create a Tagged PDF Document: Start by creating a new PDF document and setting up its tagged content.

  2. Define Header and Footer Elements: Instead of using the HeaderFooter class, you can create header and footer elements using the tagged content methods.

  3. Append Header and Footer to the Document: Use the AppendChild method to add these elements to the root of the tagged content.

Here’s an example code snippet demonstrating how to achieve this:

using Aspose.Pdf;
using Aspose.Pdf.LogicalStructure;
using Aspose.Pdf.Text;

Document pdfDocument = new Document();
var taggedContent = pdfDocument.TaggedContent;

// Create a header element
var headerElement = taggedContent.CreateHeaderElement(1);
headerElement.SetText("This is the Header");
taggedContent.RootElement.AppendChild(headerElement);

// Create a footer element
var footerElement = taggedContent.CreateFooterElement(1);
footerElement.SetText("This is the Footer");
taggedContent.RootElement.AppendChild(footerElement);

// Create a table element
TableElement taggedTable = taggedContent.CreateTableElement();
taggedTable.RepeatingRowsCount = 3;
taggedContent.RootElement.AppendChild(taggedTable);

// Add table header and body
var taggedHeaderRow = taggedTable.CreateTHead();
var tableHeaderRow = taggedHeaderRow.CreateTR();
tableHeaderRow.CreateTD().SetText("Header 1");
tableHeaderRow.CreateTD().SetText("Header 2");
tableHeaderRow.CreateTD().SetText("Header 3");

var body = taggedTable.CreateTBody();
for (int i = 0; i < 100; i++)
{
    var taggedDataRow = body.CreateTR();
    taggedDataRow.CreateTD().SetText("Row " + i + " Col 1");
    taggedDataRow.CreateTD().SetText("Row " + i + " Col 2");
    taggedDataRow.CreateTD().SetText("Row " + i + " Col 3");
}

// Save the document
pdfDocument.Save("output.pdf");

Processing Pages Without Saving

Currently, the Aspose.PDF library requires saving the document to process pages. However, you can manipulate the document structure in memory before saving it, which allows you to make changes without writing to disk until you’re ready to finalize the document.

Conclusion

By creating header and footer elements directly within the tagged content structure, you can maintain the accessibility of your PDF documents while avoiding the use of the HeaderFooter class. This method ensures that your headers and footers are properly tagged and compliant with accessibility standards.

For more detailed information on creating tagged PDFs and managing their structure, you can refer to the Aspose documentation on Creating Tagged PDF and Style Table Element.

Sources:
[1]: Create Tagged PDF using C#|Aspose.PDF for .NET - Aspose Documentation
[2]: Style Table Element | Aspose.PDF for .NET API Reference

I have check it on the last version of Aspose.PDF. TaggedContent do not have the CreateFooterElement method. Beside this CreateHeaderElement will create just a one header. But we need a header that will be represented at the top of all pages. Same for the footer. It should be represented at the bottom of all pages.

@Raman_Panfilenka

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-58434

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@Raman_Panfilenka

Possible workaround for this case can be adopted as following:

A header which is repeated on every page might be included on the top of the first page as content (rather than as a header) and a footer which is repeated on every page might be included on the last page as content (rather than as a footer).

“Document management applications — Electronic document file format enhancement for accessibility — Part 1:Use of ISO 32000-1 (PDF/UA-1)”
which has the following:
“7.8 Page headers and footers
Running headers and footers shall be identified as Pagination artifacts and shall be classified as either Header or Footer subtypes as per ISO 32000-1:2008, 14.8.2.2.2, Table 330.”

According to the standard above we are afraid that we cannot add header and footer as tags.

Do we have any opportunity to process the Pages of Document without saving?

We automatically add pages in the Save method to fit all content. The line pdfDocument.Pages.Add(); will not help in this case.