Hi,
Please let me know how i can insert a string in to a document using Aspose.Word.
I am having an Input RTF file with the below content.
Testing
My objective is to check if the input file conatins a field called and if that field is not present in the input file then insert it BEFORE so my final output will look like
Testing
Please let me know if you require any additional information on this issue. Appreciate your help!
Best Regards
Tony
Hi
Thanks for your request. You can use DocumentBuilder for inserting text into the document.
https://docs.aspose.com/words/net/document-builder-overview/
Could you please attach your input document? I will investigate the document and provide you more information or code example.
Best regards
Hi Alexey,
Thanks for attending this issue.
Attaching the Input and output documents. As mentioned in the earlier mail my requirement is to insert a text between another text. In the attached sample i need to have inserted before
Another issue which i could see is the below text is getting created in the output document generated using the sample project which i created for testing.
Evaluation Only. Created with Aspose.Words. Copyright 2003-2008 Aspose Pty Ltd.
I am sure our company has purchased a copy of ASPOSE license, but i took the latest version of the product from your site (Aspose.Words.dll - version 5.2.2.0) and has the Aspose.Words.lic (original license file) file in the assemblies folder. Please help me to resolve the licensing issue also.
regards
Tony
Hi
Thanks for your inquiry.
- I think you can try using ReplaceEvaluator to achieve this. Please see the following code:
public void Test199()
{
// Open document
Document doc = new Document(@"Test199\in.doc");
// Create regex
Regex regex = new Regex(Regex.Escape(""));
doc.Range.Replace(regex, new ReplaceEvaluator(ReplaceEvaluatorInsertText), false);
// Save document
doc.Save(@"Test199\out.doc");
}
private ReplaceAction ReplaceEvaluatorInsertText(object sender, ReplaceEvaluatorArgs e)
{
// Get parent Paragraph of matched node
Paragraph par = (Paragraph)e.MatchNode.GetAncestor(NodeType.Paragraph);
// Create paragraph that will be inserted before
Paragraph newPar = new Paragraph(e.MatchNode.Document);
Run run = new Run(e.MatchNode.Document, "");
newPar.AppendChild(run);
// Insert new paragraph into the docuemnt
par.ParentNode.InsertBefore(newPar, par);
return ReplaceAction.Stop;
}
Also see the following link for more information:
https://docs.aspose.com/words/net/find-and-replace/
2. Please check points described in FAQ ( see “Licensing” section)
https://forum.aspose.com/t/aspose-words-faq/2711
Hope this helps.
Best regards.
Hi Alexey,
Thanks, I was able to solve both of my issues with your suggestion.
regards
Tony
Hi Alexey,
When i try to insert a text in to my RTF document, then the original font available in the document is lost. Do you have any mechanism in aspose to retain the original font available in the RTF document?
Attaching the Input and output documents. I am using the below code to insert a header called inside my RTF document. When we use the Compare and Merge feature of microsoft word we can see that there is font difference in the Input and Output document.
Document doc = null;
string m_strHeader;
m_strHeader = "";
doc = new Document(strFileName);
string strContent = doc.Range.Text.ToString();
if (!strContent.Contains(m_strHeader))
{
Regex regex = new Regex(Regex.Escape(""));
doc.Range.Replace(regex,
new ReplaceEvaluator(ReplaceEvaluatorInsertText), false);
doc.Save(strFileName);
}
private ReplaceAction ReplaceEvaluatorInsertText(object sender, ReplaceEvaluatorArgs e)
{
// Get parent Paragraph of matched node
Paragraph par = (Paragraph)e.MatchNode.GetAncestor(NodeType.Paragraph);
// Create paragraph that will be inserted before
Paragraph newPar = new Paragraph(e.MatchNode.Document);
Run run = new Run(e.MatchNode.Document, m_strHeader);
newPar.AppendChild(run);
// Insert new paragraph into the document
par.ParentNode.InsertBefore(newPar, par);
return ReplaceAction.Stop;
}
regards
Tony
Hi
Thanks for your request. You can clone paragraph that you should replace. Please try using the following ReplaceEvaluator.
private ReplaceAction ReplaceEvaluatorInsertText(object sender, ReplaceEvaluatorArgs e)
{
// Get parent Paragraph of matched node
Paragraph par = (Paragraph)e.MatchNode.GetAncestor(NodeType.Paragraph);
// Create paragraph that will be inserted before
Paragraph newPar = (Paragraph)par.Clone(false);
Run run = (Run)par.Runs[0].Clone(false);
run.Text = m_strHeader;
newPar.AppendChild(run);
// Insert new paragraph into the document
par.ParentNode.InsertBefore(newPar, par);
return ReplaceAction.Stop;
}
Hope this helps.
Best regards.
Hi Alexey,
Thanks for the help. Half of my issue is resolved (" ")header field is inserted with the same font as other header fields. But i fear the font inside the tag is not matching with the original document. In the Input document, the font in Arial for the entire document, but in the output document, the font is changed to Times New Roman at the end of line/paragraph. Please find attached the Input.rtf, output.rtf and the merged.doc (which i created using the compare and merge feature of MS Word).
Appreciate your help.
Thanks
Tony
Hi
Thanks for your request. It seems to be a known issue #6127 in our defect database. As a workaround you can change font of the last paragraph programmatically:
doc.LastSection.Body.LastParagraph.ParagraphBreakFont.Name = "Arial";
Best regards.
The issues you have found earlier (filed as 6127) have been fixed in this update.