We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose.word :Table of Content Heading alignment issue

Hi ,


I am new to Aspose.Word .

I have created a Table of content in my word document but their headings are not properly aligned. I have attached my word document for reference.
My code:
using (Stream fileIO = System.IO.File.Open(Server.MapPath(filePath), FileMode.Open, FileAccess.Read))
{
Aspose.Words.Document doc = new Aspose.Words.Document(fileIO);
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

builder.InsertTableOfContents("\o “1-3” \h \z \u");
builder.InsertBreak(BreakType.ParagraphBreak);

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln(“Heading 1”);

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln(“Heading 1.1”);
builder.Writeln(“Heading 1.2”);
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln(“Heading 2”);
builder.Writeln(“Heading 3”);
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln(“Heading 3.1”);
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;
builder.Writeln(“Heading 3.1.1”);
builder.Writeln(“Heading 3.1.2”);
builder.Writeln(“Heading 3.1.3”);
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln(“Heading 3.2”);
builder.Writeln(“Heading 3.3”);
doc.UpdateFields();
doc.UpdatePageLayout();
doc.Save(“c:\x.doc”);
}
Hi Joydev,

Thanks for your inquiry. We have tested the scenario using latest version of Aspose.Words for .NET 16.8.0 and have not found the shared issue. Please use Aspose.Words for .NET 16.8.0. We have attached the output Pdf with this post for your kind reference.

Hi Tahir,


I am using Aspose.word.dll version 16.8.0.
I missed my first line in the code .I am reading from a Template file and then appending the table of content in it.If I don’t pass fileIO object in the document class I am getting the result with headers properly aligned.But If I pass the fileIO stream to the Document class my headers get wrongly aligned.I have attached my Template file for your reference.

In my Template file I have 3 text starting with @@Table which I am replacing by table,image and attachment.Along with that I need to have a TableofContent also.

Please suggest if I need to make any changes in the template file for achieving my scenario.

My code starts with:

string filePath = ConfigurationManager.AppSettings[“Templatefile”].ToString();

if (!string.IsNullOrEmpty(filePath))
{
using (Stream fileIO = System.IO.File.Open(Server.MapPath(filePath), FileMode.Open, FileAccess.Read))
{
Aspose.Words.Document doc = new Aspose.Words.Document(fileIO);
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

Hi Joydev,

Thanks for sharing the detail. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-14243. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.
Hi Joydev,

Thanks for your patience. Our product team has completed the analysis of your issue and the root cause has been identified. The Normal style of the Template.docx document has left indention 3 cm. The document has no TOC1 style, since it has no a table of contents. Aspose.Words imports all necessary styles (TOCx) from embedded document in library. So, after importing the TOC1 style into the destination document, its left indention becomes 3 cm as in the Normal style.

Please set the LeftIndent of TOC1 style to 0 as shown in following highlighted code. Hope this helps you. Please confirm if this solves your issue.

Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + "Template.docx");

Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

builder.Writeln();

builder.MoveToDocumentStart();

Field field = builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");

field.Start.ParentParagraph.ParagraphFormat.LeftIndent = 0;

builder.InsertBreak(BreakType.ParagraphBreak);

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;

builder.Writeln("Heading 1");

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;

builder.Writeln("Heading 1.1");

builder.Writeln("Heading 1.2");

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;

builder.Writeln("Heading 2");

builder.Writeln("Heading 3");

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;

builder.Writeln("Heading 3.1");

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;

builder.Writeln("Heading 3.1.1");

builder.Writeln("Heading 3.1.2");

builder.Writeln("Heading 3.1.3");

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;

builder.Writeln("Heading 3.2");

builder.Writeln("Heading 3.3");

doc.Styles[StyleIdentifier.Toc1].ParagraphFormat.LeftIndent = 0;

doc.UpdateFields();

doc.UpdatePageLayout();

doc.Save(MyDir + "Out v17.1.0.docx");