Hi, I am trying to wrap entire content in FieldTOA to StructuredDocumentTag using below code but it looks like it is not wrapping it. Could you please help me to fix this issue?
@KCSR it is possible wrap TOA inside SDT, to achieve this the SDT must be of type SdtType.RichText and the markup level must be set to MarkupLevel.Block, please use the following code as reference:
In my case the SDT will be provided by other source and this SDT type is RichText and it contains ID and Title. In my code, I will be finding the SDT in the document and insert TOA in that location. Below is the code that i am using to find the SDT location which is working as expected.
Where âInsertToaLocationâ is the Id value of the SDT provided by other source.
You mentioned that SDTâs markup should be set to MarkupLevel.Block, is it possible to set that after finding the SDT in above code OR should i ask them to do this setting before sharing the SDT to me?
I did go through the code that you have shared, it looks like we are moving the builder to SDT start and inserting the FieldToa. I donât see builder.MoveToStructuredDocumentTag(pref, 0); instead I see only builder.MoveTo(pref) so i think in my case it is going to SDT location and inserting TOA after that SDT location instead of wrapping the TOA content inside that SDT.
Could you please help me on the above?
In simple words, I need to wrap the entire TOA Content inside the SDT which already exist in the document. TOA Content will be constructed by my code which is already implemented.
@KCSR Unfortunately, there is no way change markup level on SDT, you can only remove old SDT and insert another with different markup level.
In the code you posted in your initial post, the TOA field is inserted after the SDT, since you are using Paragraph.InsertField method.
In your case you should use DocumentBuilder and move it inside the SDT:
Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder= new DocumentBuilder(doc);
// Get SDT. For demonstraction purposes get the first one.
StructuredDocumentTag targetSdt = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
// Move buildr inside SDT.
builder.MoveToStructuredDocumentTag(targetSdt, 0);
// Insert TOA field
builder.InsertField(FieldType.FieldTOA, false);
doc.Save(@"C:\Temp\out.docx");
Thanks Alexey, I tried upgrading my Aspose version to latest, but I need to get the license yet. Just to make sure things are working expected. I tested the below method
I see only the below content is wrapped inside the SDT. Is this because we have not set SDT to MarkupLevel.Block? TOA \h \f \c 8 \e " "
In your earlier comments you suggested to remove old SDT and insert another with different markup level. I tried below but I still see the SDT exist. Could you please help me how we achieve this? I want to insert the new SDT in the same place of old SDT. I can find the old SDT as shown in below code.
I am able to get the TOA content wrapped in the SDT now.
TOA structure also looks good as shown in below screenshot. Could you please let me know why my page numbers are not created as jumplinks and it is not jumping to referenced text(bookmark) in that page?
In above code bookmark is the bookmark that needs to be referenced to page numbers so that on click of the page number it has to jump to that particular bookmark.
Thanks Alexey, Is there any way we can tweak to make that page number field in TOA clickable during the TOA creation or after the TOA creation is completed? If yes could you please share code sample using which I can achieve this?
I am afraid if I use TOC option, I might not get the TOA structure same as I showed in my last message.
@KCSR Unfortunately, there is no easy way to achieve this. I think it would be easier to build âlike TOAâ content manually with links to the corresponding TA fields. To achieve this you should collects all TA fields, insert insert bookmarks at the TA fields and then construct the âlike TOAâ. The simplified code can look like this:
Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
LayoutCollector collector = new LayoutCollector(doc);
// Collect TA fields.
List<FieldTA> taFields = doc.Range.Fields.Where(f => f.Type == FieldType.FieldTOAEntry).Cast<FieldTA>().ToList();
// Insert bookmakrs at TA field
int bkIndex = 0;
Dictionary<string, Dictionary<string, string>> tocCategories = new Dictionary<string, Dictionary<string, string>>();
foreach (FieldTA ta in taFields)
{
string taCategory = ta.EntryCategory;
if (!tocCategories.Keys.Contains(taCategory))
tocCategories.Add(taCategory, new Dictionary<string, string>());
string bkName = string.Format("_toa_entry_{0}", bkIndex);
bkIndex++;
// Format text of TOA entry, that consis of long citation tab and page number.
string toaEntryText = string.Format("{0}\t{1}", ta.LongCitation, collector.GetStartPageIndex(ta.Start));
tocCategories[taCategory].Add(bkName, toaEntryText);
// insert bookmark.
builder.MoveToField(ta, true);
builder.StartBookmark(bkName);
builder.EndBookmark(bkName);
}
// Now move document builder at the location where TOA should be build
// For demonstration purposes move to the beginning of the document.
builder.MoveToDocumentStart();
double tabPosition = builder.PageSetup.PageWidth - builder.PageSetup.RightMargin - builder.PageSetup.LeftMargin;
// Construct the like TOA content.
foreach (string catogory in tocCategories.Keys)
{
builder.ParagraphFormat.ClearFormatting();
builder.Font.ClearFormatting();
builder.Font.Size = 14;
builder.Font.Bold = true;
builder.Writeln(catogory);
builder.Font.ClearFormatting();
builder.ParagraphFormat.TabStops.Clear();
builder.ParagraphFormat.TabStops.Add(tabPosition, TabAlignment.Right, TabLeader.Dots);
foreach (string bkName in tocCategories[catogory].Keys)
{
builder.InsertHyperlink(tocCategories[catogory][bkName], bkName, true);
builder.Writeln();
}
}
doc.Save(@"C:\Temp\out.docx");
One last query, If I save the document after constructing and inserting TOA to document, the final document looks good but if I convert that document to ooxml(code below) then the document is having more space between the lines in TOA constructed region in other words the styles are getting changed. In this case our page numbers calculated in TOA constructed section will go wrong since the number of pages also increases because of introducing more spaces. Any suggestions on this?
Thanks Alexey, I tried with your suggested âDocumentToFlatOpcStringâ and still I see some difference in space between the lines and also if you observe the below screenshots font set for âAbdulhaseeb v. Calbone,â and âAllen v. Muskogee,â was TimesRoman before converting the DocumentToFlatOpcString and After Converting it is Calibri.
Alexey, I had not set the Font name for âAbdulhaseeb v. Calbone,â and âAllen v. Muskogee,â explicitly and I think because of which the Font name was changing to default name i.e., Calibri.
Now the problem is only with the space between lines and this added spaces after the âDocumentToFlatOpcStringâ convertion will move the content and TOA pages will be showed wrong in this case. Could you please let me know if we can set this space between the lines also?
But I see there is some space inserted just after the paragraph text - after converting document to FlatOpcString using âDocumentToFlatOpcStringâ. This Space is inserted only for TOA Headings and not for TOA Entries.
In Word there is âRemove Space After Paragraphâ option (below screenshot) under âLine and Paragraph Spacingâ settings
Could you please let me know how do we set the âRemove Space After Paragraphâ for builder in Aspose? If we set this option we should be good.
@KCSR To remove spacing after the paragraph, you can set ParagraphFormat.SpaceAfter property of the appropriate paragraph to zero.
When you use InsertBreak(BreakType.ParagraphBreak) it insert a paragraph break, when you use InsertBreak(BreakType.LineBreak) it inserts a soft line breaks. In the later case the lines are in the same paragraph and spacing between these lines is controlled by ParagraphFormat.LineSpacingRule and ParagraphFormat.LineSpacing properties. While spacing between paragraphs is controlled by ParagraphFormat.SpaceAfter and ParagraphFormat.SpaceBefore properties.
Also, could you please clarify - the constructed TOA looks properly when you save the document to DOCX, but if save the same document as FlatOPC it looks improperly, right? If so, could you please attach output DOCX and FlatOpc documents produced on your side?