We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Copy text and replace fields

I am trying find one or more paragraphs in a word document between some known tags and paste a copy of this further up the document. I then want to find any fields within the copied section and replace these with some alternative text.


I have found and copied the paragraphs using the ExtractContent and InsertContent functions that are documented on this site and I have put a named bookmark around the new section. Can you tell me the best way to find and replace all of the fields within this bookmark ?

Thanks
Fiona

Hi Fiona,


Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing.

  1. Your input Word document.
  2. Your target/expected document showing the desired final output (you can use Microsoft Word to create this document)

I will then investigate your scenario and provide you code to achieve the same using Aspose.Words.

Best regards,

I have attached an Input and Output document.


I need to find the text between <LAYOUT_SECTION> and </LAYOUT_SECTION> markers and put a copy of this just above the <TRAILER_SECTION> marker.

I will also un-hide the copied in text and replace any Quote fields within in this with some calculated content.

All of the current templates that we use are .doc files, but these can be converted to .docx if needed.
We do all of this work currently with word automation, but want to move to using a more programmatic version.

Thanks
Fiona

Hi Fiona,


Thanks for the additional information. Please try run the following code snippet which helps you achieve what you’re looking for:

Document doc = new
Document(@“C:\Temp\input.doc”);

Paragraph startPara = null;

Paragraph endPara = null;

Paragraph targetPara = null;

NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);

foreach (Paragraph paragraph in paragraphs)

{

if (paragraph.ToString(SaveFormat.Text).Trim().Equals(""))

startPara = paragraph;

if (paragraph.ToString(SaveFormat.Text).Trim().Equals(""))

endPara = paragraph;

if (paragraph.ToString(SaveFormat.Text).Trim().Equals(""))

targetPara = paragraph;

}

ArrayList content = ExtractContent(startPara, endPara, false);

foreach (CompositeNode node in content)

{

targetPara.ParentNode.InsertBefore(node, targetPara);

NodeCollection runs = node.GetChildNodes(NodeType.Run, true);

foreach (Run run in runs)

run.Font.Hidden = false;

NodeCollection fields = node.GetChildNodes(NodeType.FieldStart, true);

foreach (FieldStart fieldStart in fields)

{

Paragraph parentPara = null;

if (fieldStart.FieldType == FieldType.FieldQuote)

{

parentPara = fieldStart.ParentParagraph;

Field quoteField = fieldStart.GetField();

quoteField.Remove();

parentPara.Runs.Add(new Run(doc, "Your customer text"));

}

}

}

doc.Save(@"C:\Temp\out.docx");


If we can help you with anything else, please feel free to ask.

Best regards,

Thanks for the quick response. This code looks very short and neat. I will make use of it.


Fiona