Aspose.Words Headers, Footers and Tables from HTML

Is there a way for Aspose.Words to detect a header or footer or table from an HTML string?
I’m trying to convert HTML string to doc but the output can’t determine the header or footer

@kdepadua

Aspose.Words does not have a built-in feature specifically for detecting headers, footers, or tables directly from an HTML string. However, you can programmatically add headers and footers to a Word document using the DocumentBuilder class after you have processed the HTML content.

To insert a table from HTML into a Word document, you can follow these steps:

  1. Set Up Your Document: Define the directory where your Word document will be saved.
  2. Create a New Document: Instantiate a new Word document.
  3. Insert HTML Table: Use the DocumentBuilder to insert your HTML table into the Word document. Note that the appearance of the table will be as defined in your HTML code.
  4. Save the Document: Finally, save your document to ensure that the changes are written to the file system.

Here is a code example demonstrating how to insert a table from HTML:

using Aspose.Words;
using Aspose.Words.Tables;

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// HTML string containing a table
string htmlTable = "<table><tr><td>Cell 1</td><td>Cell 2</td></tr></table>";

// Insert the HTML table
builder.InsertHtml(htmlTable);

// Save the document
doc.Save("output.docx");

For adding headers and footers, you can also use the DocumentBuilder class. Here’s a brief example of how to add a header and footer:

// Add a header
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Write("This is the header text.");

// Add a footer
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Write("This is the footer text.");

// Save the document
doc.Save("output_with_header_footer.docx");

By following these steps, you can effectively manage headers, footers, and tables in your Word documents using Aspose.Words for .NET.

For more detailed guidance, you can refer to the official documentation on working with headers and footers, as well as inserting tables from HTML.

Sources:
[1]: Insert Table From Html | Aspose.Words Document Processing API
[2]: Create Header Footer | Aspose.Words Document Processing API

@kdepadua You can use Aspose.Words roundtrip information in HTML to insert headers/footers from HTML, for example see the following HTML:

<html>
<body>
    <div>
        <div style="-aw-headerfooter-type:header-primary; clear:both">
            <p>This is primary header</p>
        </div>
        <p>This is the main body.</p>
        <div style="-aw-headerfooter-type:footer-primary; clear:both">
            <p>This is primary footer</p>
        </div>
    </div>
</body>
</html>

As you can see two divs are marked with special -aw-headerfooter-type style, which is recognized by Aspose.Words and content of these divs will be imported as header and footer of the document.

Tables from HTML are imported as Table nodes into Aspose.Words Document Object Model.

Thank you @alexey.noskov … Is there documentation for other Aspose.Words roundtrip information in HTML?

@kdepadua You can create what is required in MS Word, convert the document to HTML using Aspose.Words and see how Aspose.Words writes roundtrip information.

1 Like