Hi Hafeez this is document related to page2 issue iam inable to insert toc in page2 if iam trying to insert toc in page2 it is inserted inside of table but i want to insert outside of tableToc_starts_from.zip (424.0 KB)
The following code should insert TOC field after the Table on second page:
Document doc = new Document("C:\\Temp\\Toc_starts_from\\Toc_starts_from.doc");
Node[] runs = doc.GetChildNodes(NodeType.Run, true).ToArray();
for (int i = 0; i < runs.Length; i++)
{
Run run = (Run)runs[i];
int length = run.Text.Length;
Run currentNode = run;
for (int x = 1; x < length; x++)
{
currentNode = SplitRun(currentNode, 1);
}
}
DocumentBuilder builder = new DocumentBuilder(doc);
NodeCollection smallRuns = doc.GetChildNodes(NodeType.Run, true);
LayoutCollector collector = new LayoutCollector(doc);
int pageIndex = 2;
foreach (Run run in smallRuns)
{
if (collector.GetStartPageIndex(run) == pageIndex)
{
if (run.GetAncestor(NodeType.Table) == null)
{
builder.MoveTo(run);
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
builder.Writeln();
break;
}
}
}
doc.UpdateFields();
doc.Save("C:\\Temp\\Toc_starts_from\\21.9.doc");
public static Run SplitRun(Run run, int position)
{
Run afterRun = (Run)run.Clone(true);
afterRun.Text = run.Text.Substring(position);
run.Text = run.Text.Substring((0), (0) + (position));
run.ParentNode.InsertAfter(afterRun, run);
return afterRun;
}
Hi Hafeez,
This solution is working fine but if we have a paragraph or figure after table it is not working . iam getting output at last page of document can you help me to resolve this issue… i have attached the documents regardingsrinu code (2).zip (412.2 KB)
the issue
While using the latest 21.9 version of Aspose.Words, I have produced an output Word DOC document by using the code from my previous post and attached it here for your reference (see 21.9.zip (402.6 KB)). Do you see the same problem in this output? Can you please provide screenshot (encircle the problematic area in this DOC file)?
Hi Hafeez ,
The above code is working in console in our project we are getting wrong output please check the files which i have attached . and is it possible to write to code using paragraphs instead of runsissue docs.zip (809.3 KB)
we are using latest 21.9 version in our project
Please open “Toc_starts_from (2).doc” that you attached in your previous post with MS Word. Go to the desired page and manually insert Table of Contents in it by using MS Word. Save As the Word file to DOC format and attach this expected DOC file (showing the desired behavior) here for our reference. We will then provide you code to get the same desired output by using Aspose.Words.
Please try the following code:
Document doc = new Document("C:\\Temp\\issue docs\\Toc_starts_from (2).doc");
DocumentBuilder builder = new DocumentBuilder(doc);
LayoutCollector collector = new LayoutCollector(doc);
int pageIndex = 2;
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
if (collector.GetStartPageIndex(para) == pageIndex)
{
if (para.GetAncestor(NodeType.Table) == null)
{
builder.MoveTo(para);
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
builder.Writeln();
break;
}
}
}
doc.UpdateFields();
doc.Save("C:\\Temp\\issue docs\\21.10.doc");
Hi hafeez,
i want to insert table of contents before table…example a document has table in page2 starting and i need to send that table to next page and insert toc in page2. can you help me
In this case, please use the following code to move the table to the start of third page.
Document doc = new Document(@"C:\Temp\QC0215_Output_2.3.P QOS - DPPrefilter Change 0.5mg-3g\\QC0215_Output_2.3.P QOS - DPPrefilter Change 0.5mg-3g.doc");
LayoutCollector collector = new LayoutCollector(doc);
// Get the table that you want to move to next page
Table table = doc.FirstSection.Body.Tables[0];
// Get the first Paragraph of page 3
int pageIndex = 3;
Paragraph paragraph = null;
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
if (collector.GetStartPageIndex(para) == pageIndex)
{
paragraph = para;
break;
}
}
// insert table at the start of 3rd page
paragraph.ParentNode.InsertBefore(table, paragraph);
doc.Save(@"C:\Temp\\QC0215_Output_2.3.P QOS - DPPrefilter Change 0.5mg-3g\\21.10.docx");
Thanks for help hafeez
Hi Haffez ,
when we create toc at that we create toc title also . i have issue with this title which need to apply font family styles to that particular title but when i applied font family to that tilte it is applied to everywhere in the document . will help me to do this ihave attached files below in that i have marked title please see it once … the tiltle which i circled to that particular one i have apply the font family (ex of font family ---- coutoc.zip (2.3 MB)
rier, calibri, timesnewroman)
Please try the following C# code to adjust font formatting of TOC Title/Heading paragraph:
Document doc = new Document("C:\\Temp\\toc (1)\\QC0459_Output_2.3 Introduction KY.doc");
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
if (para.ParagraphFormat.StyleName.Trim().Equals("TOC Heading Centered") && para.Range.Text.Trim().Equals("TABLE OF CONTENTS"))
foreach (Run run in para.Runs)
{
run.Font.Name = "Verdana";
run.Font.Bold = true;
}
doc.Save(@"C:\Temp\\toc (1)\\21.10.doc");
Thanks for the help hafeez but iam getting same output as before which i have send you the document… is there any chance without using toc heading centered can we apply this verdana to this toc title name only …toc6.zip (100.6 KB)
please use source file not the output file … by using source we have to generate toc and for that generated toc title only we have to apply font family style… if we use source file in that we dont have any style before for the 3.2.P.3.5 Process Validation and or Evaluation - Media Fill_DMPA-SC-ACTD_Test.zip (39.4 KB)
document … so if we use source file we can get right output
Considering the “3.2.P.3.5 Process Validation and or Evaluation - Media Fill_DMPA-SC-ACTD_Test.doc” as the source file, please also generate output Word DOCX containing the TOC and title and provide this output Word file showing the undesired behavior here for further testing. Please also create a standalone simple console application (source code that you are currently using without any compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. Please do not include Aspose DLL files in it to reduce the file size.
Hi hafeez,
iam sending toc code which iam working and toc heading code also please see it and above documents which i already send to you please check with that documents we have some security issues for other documents to send to you …toc test.zip (1.1 KB)
@awais.hafeez,
is that possible to use multiple paragraph formats to a single builder, if possible please tell me how to switch between paragraph formats. here I am trying to create toc lot lof with the same builder but with different font parameters. i need to know is that possible or not if not suggest any other way
The problem occurs because of applying “TOC Heading Centered” style to the TOC title paragraph. When you apply this style, the entry becomes visible in Table of Contents too with font formatting applied. This is expected behavior. But you can workaround this by manually applying the font formatting to title paragraph and this will avoid the appearance of title in TOC altogether. Please check the following sample code:
Document doc = new Document(@"C:\Temp\TOC\\2.3 Introduction KY.doc");
Node[] runs = doc.GetChildNodes(NodeType.Run, true).ToArray();
for (int i = 0; i < runs.Length; i++)
{
Run run = (Run)runs[i];
int length = run.Text.Length;
Run currentNode = run;
for (int x = 1; x < length; x++)
{
currentNode = SplitRun(currentNode, 1);
}
}
DocumentBuilder builder = new DocumentBuilder(doc);
NodeCollection smallRuns = doc.GetChildNodes(NodeType.Run, true);
LayoutCollector collector = new LayoutCollector(doc);
int pageIndex = 2;
foreach (Run run in smallRuns)
{
if (collector.GetStartPageIndex(run) == pageIndex)
{
if (run.GetAncestor(NodeType.Table) == null)
{
builder.MoveTo(run.ParentParagraph.PreviousSibling);
builder.InsertBreak(BreakType.SectionBreakNewPage);
// Write TOC Title with custom Font formatting
//// do not use following style to prevent it from appearing in TOC
// builder.ParagraphFormat.StyleName = "TOC Heading Centered";
// instead manually format the title
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Name = "Courier";
builder.Font.Size = 14;
builder.Writeln("TABLE OF CONTENTS");
// reset DocumentBuilder's formatting
builder.Font.ClearFormatting();
builder.ParagraphFormat.ClearFormatting();
// Insert TOC
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
builder.Writeln();
builder.InsertBreak(BreakType.SectionBreakNewPage);
break;
}
}
}
doc.UpdateFields();
doc.Save(@"C:\Temp\\TOC\\21.10.docx");
Attachment: source and output docx files.zip (2.2 MB)
Thank you @hafeez for your help…
i had another issue that i want to add section break for existing toc like i want to add section after table of contents and after that list of tables will be created at that time also i want to add section break . means after toc section break and after list of tables section break and after list of figures section breaks should be added can you help me with this. i have attached the file
2.3 Introduction to Quality Overall Summary.zip (90.9 KB)