If processing a Word document that has a paragraph like this:
\t This is some text
Hi Simon,
In the attached file, in Word if you hide paragraph marks they look like they’re in the same list, but if you turn on paragraph marks you’ll see that the second one is actually typed out (using spaces and tabs to get it to line up).
Hi Simon,
Document doc = new Document(MyDir + “Example.docx”);<o:p></o:p>
// Iterate through all paragraphs in the document
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
Console.WriteLine(para.GetText().Contains(ControlChar.Tab));
}
//Insert the same spaces and tab character into the document.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();
builder.Writeln();
builder.Writeln(" " + ControlChar.Tab + "New text");
doc.Save(MyDir + "output.docx");
OK thanks for clearing that up for me, I thought tab stops were used whenever tab characters were used.
Perhaps I need to insert a dummy run and then use the LayoutCollector to calculate the indent in pixels and then convert it back to points… do you have a suggestion as to how I can achieve the above?
Document doc = new Document(MyDir + "Example.docx");
// Iterate through all paragraphs in the document
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
Console.WriteLine(para.ParagraphFormat.LeftIndent);
if (para.IsListItem)
{
Console.WriteLine(para.ListFormat.List.ListLevels[0].NumberPosition);
Console.WriteLine(para.ListFormat.List.ListLevels[0].TextPosition);
}
}
Paragraph paragraph1 = (Paragraph)doc.GetChild(NodeType.Paragraph, 1, true);
paragraph1.Range.Replace("An example", "", new FindReplaceOptions { ReplacingCallback = new FindAndInsertBookmark("bookmark2") });
Bookmark bm = paragraph1.Range.Bookmarks["bookmark2"];
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
var renderObject = collector.GetEntity(bm.BookmarkStart);
layoutEnumerator.Current = renderObject;
RectangleF location2 = layoutEnumerator.Rectangle;
Console.WriteLine("Calculated position of 'An example'" + (location2.X - doc.FirstSection.PageSetup.LeftMargin));
bm.Remove();
doc.Save(MyDir + "output.docx");