I want to generate TOC for an existing word document. Aspose should be able to scan the whole document and find all the headings and then generate TOC at the begining of the page!
We will add a "temp special character" to all the headings in the document so that Aspose can find all the headings in the document just by finding the special character!
I have some documents in .rtf format that do not have the styles applied for the headings and the document doesn’t have the TOC. I also have an existing DOTX file which has the custom styles defined for all the headings in the document.
Our document will have a “Special tags” embedding all the text for which we need to apply the style as heading (read the style from the DOTX file). And then finally applying TOC.
Here is what Aspose should do:
-
Scan the document (.rtf in this case) and find the “special tags”.
-
Where ever the special tags (Start and End) are found, apply the custom styles to only the content by reading the custom style from the DOTX file corresponding to the tag.
-
Remove the Start and End tags between Start and End tags,
Eg:
a. Start-AA-Heading1EMPLOYEREnd-AA-Heading1
Here, Start-AA-Heading1 and End-AA-Heading1 are the start and end tags. We need to apply the style Cust_AA_Heading1 style (available in .DOTX file) to the text “EMPLOYERE” and delete the Start-AA-Heading1 and End-AA-Heading1 texts from the document.
b. Start-AA-Heading2EXECUTION PAGEEnd-AA-Heading2
Similarly, we need to apply the Cust_AA_Heading2 style (available in .DOTX file) to the text “Heading2EXECUTION PAGE” and then delete the texts Start-AA-Heading2 and PAGEEnd-AA-Heading2 from the document.
- Then finally run the TOC and save the file as .doc file. ( we know this step works)
I have attached the sample .rtf and .dotx file. Please provide the sample code to read the styles from .dotx file, traversing the input (.rtf) file and find out the start and end tags to apply the custom styles for the content between the tags, and then finally deleting those tags from the document.
If you could provide or share some code base which addresses the similar scenario as mine, It would be greatfuil. I already have the aspose license.
My sample code is given below:
// Load an existing Word document in memory
Document _pAsposeDocument = new Document("C:\\AA_Input.rtf");
//set the full file path of the customStyles file related to the document
_pAsposeDocument.AttachedTemplate = "C:\\AA_Style_Template.dotx";
//import custom styles from .DOTX document into the BIP document
Document CustomStylesDoc = new Document(_pAsposeDocument.AttachedTemplate);
foreach (Style srcStyle in CustomStylesDoc.Styles)
{
if (!srcStyle.BuiltIn)
_pAsposeDocument.Styles.AddCopy(srcStyle);
}
//HERE , I want to get the text between the start and End tags and then find out the corresponding style in the document and apply to the content between the tags. How can I achieve it? How to find the StyleIdentifier value for the custom style that is added from the .dotX file.
//Apply TOC
// Create a document builder to insert content with into document.
DocumentBuilder builder = new DocumentBuilder(_pAsposeDocument);
// Insert a table of contents at the beginning of the document.
builder.MoveToDocumentStart();
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
// Start the actual document content on the second page.
builder.InsertBreak(BreakType.PageBreak);
// Call the method below to update the TOC.
_pAsposeDocument.UpdateFields();
//Save the file as .doc _pAsposeDocument.Save("C:\\AA_Output.doc");