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)
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)
@k.sukumar You can use code like the following to insert section break after TOC field:
Document doc = new Document(@"C:\Temp\in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
NodeCollection fieldStarts = doc.GetChildNodes(NodeType.FieldStart, true);
foreach (FieldStart start in fieldStarts)
{
if (start.FieldType == FieldType.FieldTOC)
{
Field toc = start.GetField();
Paragraph tocEnd = (Paragraph)toc.End.GetAncestor(NodeType.Paragraph);
builder.MoveTo(tocEnd);
builder.InsertBreak(BreakType.SectionBreakNewPage);
}
}
doc.Save(@"C:\temp\out.docx");
Thank you alexey,
iam facing issue when i created toc , lot, and lof i have attached the document … in that when i created toc iam getting list of tables two times how to remove that can you help me with this.
iam actually removing the already toc and adding new toc but when i run project the breakpoint comming to remove filed line but it is not removing . iahve attached the files with pictures and documentstoc issue.zip (181.0 KB)
List<Node> listFieldTOC = TempDoc.GetChildNodes(NodeType.FieldStart, true).Where(x => ((FieldStart)x).FieldType == FieldType.FieldTOC).ToList();
//Below code for checking toc/LOT/LOF for the file having below 5 pages excluding toc/lot/lof
if (pagecount >= 5 && listFieldTOC.Count > 0)
{
string Tempfilename = rObj.DestFilePath;
Tempfilename = Tempfilename.Replace(rObj.File_Name, "Temp_" + rObj.File_Name);
List<Node> SectionBreakslst = TempDoc.GetChildNodes(NodeType.Paragraph, true).Where(x => x.Range.Text.Contains(ControlChar.SectionBreak)).ToList();
foreach (Paragraph pr in SectionBreakslst)
{
Paragraph prbreak = pr;
Paragraph prcl = null;
if (pr.PreviousSibling != null && pr.PreviousSibling.NodeType == NodeType.Paragraph)
{
prcl = (Paragraph)prbreak.PreviousSibling;
}
if (prcl != null && !prcl.ParagraphFormat.StyleName.Trim().ToUpper().Contains("NoTOC") && (prcl.ParagraphFormat.StyleName.Trim().ToUpper() == "LIST OF FIGURES" || prcl.ParagraphFormat.StyleName.Trim().ToUpper() == "TOC HEADING CENTERED" || prcl.ParagraphFormat.StyleName.Trim().ToUpper() == "LIST OF TABLES" || prcl.ParagraphFormat.StyleName.Trim().ToUpper() == "TABLE OF FIGURES" || prcl.ParagraphFormat.StyleName.Trim().ToUpper().Contains("TOC ")))
{
if (prcl.IsEndOfSection)
{
prbreak.ParentSection.Remove();
}
}
if (pr.NextSibling != null && pr.NextSibling.NodeType == NodeType.Paragraph)
{
prcl = (Paragraph)prbreak.NextSibling;
}
if (prcl != null && !prcl.ParagraphFormat.StyleName.Trim().ToUpper().Contains("NoTOC") && (prcl.ParagraphFormat.StyleName.Trim().ToUpper() == "LIST OF FIGURES" || prcl.ParagraphFormat.StyleName.Trim().ToUpper() == "TOC HEADING CENTERED" || prcl.ParagraphFormat.StyleName.Trim().ToUpper() == "LIST OF TABLES" || prcl.ParagraphFormat.StyleName.Trim().ToUpper() == "TABLE OF FIGURES" || prcl.ParagraphFormat.StyleName.Trim().ToUpper().Contains("TOC ")))
{
if (prcl.IsEndOfSection)
{
prbreak.ParentSection.Remove();
}
}
}
foreach (FieldStart start in TempDoc.GetChildNodes(NodeType.FieldStart, true).Where(x => ((FieldStart)x).FieldType == FieldType.FieldTOC))
{
if (start.ParentParagraph.PreviousSibling != null && start.ParentParagraph.PreviousSibling.NodeType == NodeType.Paragraph)
{
Paragraph pr1 = (Paragraph)start.ParentParagraph.PreviousSibling;
if (pr1 != null && (pr1.Range.Text.Trim().ToUpper().Contains("TABLE OF CONTENTS") || pr1.Range.Text.Trim().ToUpper().Contains("LIST OF TABLES") || pr1.Range.Text.Trim().ToUpper().Contains("LIST OF FIGURES")))
pr1.Remove();
}
start.GetField().Remove();
}
Paragraph prB = new Paragraph(TempDoc);
List<Node> paragraphs = TempDoc.GetChildNodes(NodeType.Paragraph, true).Where(x => !((Paragraph)x).ParagraphFormat.StyleName.Trim().ToUpper().Contains("NoTOC") && (((Paragraph)x).ParagraphFormat.StyleName.Trim().ToUpper() == "LIST OF FIGURES" || ((Paragraph)x).ParagraphFormat.StyleName.Trim().ToUpper() == "TOC HEADING CENTERED" || ((Paragraph)x).ParagraphFormat.StyleName.Trim().ToUpper() == "LIST OF TABLES" || ((Paragraph)x).ParagraphFormat.StyleName.Trim().ToUpper() == "TABLE OF FIGURES" || ((Paragraph)x).ParagraphFormat.StyleName.Trim().ToUpper().Contains("TOC "))).ToList();
foreach (Paragraph pr in paragraphs)
{
if ((pr.Range.Text.Contains(ControlChar.SectionBreak) && pr.IsEndOfSection) && prB != pr)
pr.ParentSection.Remove();
if (pr.NextSibling != null && pr.NextSibling.NodeType == NodeType.Paragraph && pr.NextSibling.Range.Text.Contains(ControlChar.SectionBreak))
{
prB = (Paragraph)pr.NextSibling;
if (prB.IsEndOfSection)
prB.ParentSection.Remove();
}
pr.Remove();
}
@k.sukumar Unfortunately your requirements is not quite clear. Could you please attach your input, current output and expected output create in MS Word along with code that produces the output document? We will check and provide you more information.