How can I view the document in this way as mentioned in screenshot. Can u guide me on this
@KeerthanaRamesh214 This is Aspose.Words demo application - DocumentExplorer. You can find it’s sources here:
https://github.com/aspose-words/Aspose.Words-for-.NET/tree/master/Examples/DocsExamples/DocumentExplorer
Hello @alexey.noskov I have one more issue for the below document when I try to replace bookmark with same name for bk_3 alone during replacement the line break is getting removed how can we avoid this
processed_document_1.docx (18.4 KB)
processed_document_2.docx (19.1 KB)
output.docx (18.4 KB)
I want the same way exactly to be replaced even the bookmark begins with empty line can you help me out here
List<Node> extractedNodes = ExtractContentHelper.ExtractContent(srcBookmark.BookmarkStart, srcBookmark.BookmarkEnd, true);
If I change the false parameter to true to include bookmark start/end nodes, which preserves structural boundaries but that creates duplicate bookmarks how can I prevent that but also preserving the structural boundaries.
@KeerthanaRamesh214 I would recommend you to modify your documents and place bk_3 start at the beginning of the second paragraph instead of placing it at the end of the first paragraph. Please see the following modified documents:
processed_document_1.docx (18.4 KB)
processed_document_2.docx (18.9 KB)
No @alexey.noskov that’s how our client document looks like can I remove the bookmarks which is added after all replace which contains _ will that help me or you see any problem in that can you help me out in this way…
@KeerthanaRamesh214 You can try using the following code to pre-process the document to normalize bookmarks in the document:
private static void NormalizeBookmarks(Document doc)
{
foreach (Bookmark bk in doc.Range.Bookmarks)
{
// Move bookmark start to the start of the next paragraph if it is placed at the end of the paragraph.
BookmarkStart start = bk.BookmarkStart;
Paragraph startParentPara = start.ParentNode as Paragraph;
if (startParentPara != null && startParentPara.LastChild == start)
{
Paragraph nextPara = startParentPara.NextSibling as Paragraph;
if (nextPara != null)
nextPara.PrependChild(start);
}
// Move bookmark end to the end of the previous paragraph if it is placed at the start of the paragraph.
BookmarkEnd end = bk.BookmarkEnd;
Paragraph endParentPara = end.ParentNode as Paragraph;
if (endParentPara != null && endParentPara.FirstChild == end)
{
Paragraph prevPara = endParentPara.PreviousSibling as Paragraph;
if (prevPara != null)
prevPara.AppendChild(end);
}
}
}
Can we don’t modify the bookmark and achieve the same structural boundaries because that is requirement … can I remove the bookmarks which is added after all replace which contains _ will that help me or you see any problem in that can you help me out in this way…
@KeerthanaRamesh214 You can extract content including bookmark start/end nodes. Then before inserting the extracted content into the destination document remove the bookmark from the document that is to be inserted to avoid the bookmark duplication.
Can you help me with a code example if possible
@KeerthanaRamesh214 Sure, you can try modifying your code like this:
// ......................
List<Node> extractedNodes = ExtractContentHelper.ExtractContent(srcBookmark.BookmarkStart, srcBookmark.BookmarkEnd, true);
Document extractedContent = ExtractContentHelper.GenerateDocument(src, extractedNodes);
extractedContent.Range.Bookmarks[srcBookmark.Name].Remove();
// ......................
DOC_1 (2).docx (286.7 KB)
DOC_2 (2).docx (13.0 KB)
output (1).docx (123.5 KB)
Hello @alexey.noskov I am using this document for replace and this is the output why header is getting added i am just trying to replace bk_36
List<Node> extractedNodes = ExtractContentHelper.ExtractContent(srcBookmark.BookmarkStart, srcBookmark.BookmarkEnd, true);
this is extracting the header and footer as well it need to extract the bookmark content alone right help me here
As I have added the logic for replace bookmark in header footer its extracting header footer aswell but that is not correct kindly help me with code
@KeerthanaRamesh214 Please comment the following two lines in the GenerateDocument method:
foreach (HeaderFooter hf in srcSection.HeadersFooters)
importedSection.HeadersFooters.Add(importer.ImportNode(hf, true));
Here is the modified method:
public static Document GenerateDocument(Document srcDoc, List<Node> nodes)
{
// Clone source document to preserve source styles.
Document dstDoc = (Document)srcDoc.Clone(false);
// Import each node from the list into the new document. Keep the original formatting of the node.
NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.UseDestinationStyles);
foreach (Node node in nodes)
{
if (node.NodeType == NodeType.Section)
{
Section srcSection = (Section)node;
Section importedSection = (Section)importer.ImportNode(srcSection, false);
importedSection.AppendChild(importer.ImportNode(srcSection.Body, false));
//foreach (HeaderFooter hf in srcSection.HeadersFooters)
// importedSection.HeadersFooters.Add(importer.ImportNode(hf, true));
dstDoc.AppendChild(importedSection);
}
else
{
Node importNode = importer.ImportNode(node, true);
dstDoc.LastSection.Body.AppendChild(importNode);
}
}
return dstDoc;
}