|
WordDocument document = new WordDocument("C:\\Users\\Juan Jose\\Documents\\Contrato.doc", FormatType.Doc);
// First argument is text to be replaced and next one is replacement text.
document.Replace("CONTRATISTA", "TEXTOCAMBIADO", false, true);
document.Save(@"DocumentoXXXXXX.docx", FormatType.Docx);
document.Close(); |
It doesn't work for me either
We are trying to replace text and then convert it to PDF but replacement seems not working.
here is sample code
using (FileStream docStream = new FileStream(Path.GetFullPath("path to Docx file"), FileMode.Open, FileAccess.ReadWrite))
{
//Loads file stream into Word document
using (WordDocument wordDocument = new WordDocument(docStream, FormatType.Docx))
{
//Finds the first occurrence of a particular text in the document.
TextSelection selection = wordDocument.Find("{{article-desc}}", false, false);
//Initializes text body part.
TextBodyPart bodyPart = new TextBodyPart(selection);
//Replaces a particular text with the text body part.
wordDocument.Replace("Adventure Works Cycles", bodyPart, false, true);
//Creates file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath($"path to save file"), FileMode.Create, FileAccess.ReadWrite))
{
//Saves the Word document to file stream.
wordDocument.Save(outputFileStream, FormatType.Docx);
}
//Instantiation of DocIORenderer for Word to PDF conversion
using (DocIORenderer render = new DocIORenderer())
{
//Converts Word document into PDF document
PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);
//Saves the PDF document to MemoryStream.
MemoryStream stream = new MemoryStream();
pdfDocument.Save(stream);
stream.Position = 0;
//Download PDF document in the browser.
return File(stream, "application/pdf", "Sample.pdf");
}
}
}
Hi Donaat,
We have tried to reproduce the reported problem using
the given details, but it works properly at our end. For your reference, we
have attached the sample application which we tried to reproduce the reported
problem at our end. We
suggest you to try the attached sample at your end and provide us confirmation
whether you are facing issue in this sample. It can be downloaded from
the below link,
https://www.syncfusion.com/downloads/support/directtrac/general/ze/Find-and-replace1036404960.zip
If the issue is not reproduced using the attached
sample application at your end, then could you please provide the following details for further
investigation,
1. Modify the attached sample as an issue
reproducible sample application and share us.
2. Input Word document which used at
your end. Because we suspect
that the problem might be due to the contents in the input Word document.
3. Syncfusion version used at your side.
Based on the above details, we will check and provide
you appropriate solution at the earliest.
Note: If you have any confidential data in your input Word
document, please replace with some dummy data and provide us the same. We just
need your document to recreate the problem you face. So, once investigated the
issue, we will
delete the document permanently from our side and do not share with anyone.
Regards,
Anto Nihil S
Hi Anto Nihil S,
Thank you for your reply.
I am using the Syncfusion.DocIORenderer.Net.Core version 21.2.5. Also, tried 21.2.4 but still didn't work
I have tried the same version and followed the same code from your sample code project to replace text in Word document. But it still doesn't work for us.
Also, please find the uploaded project here with the docx file that I am using to test find and replace.
Please let us what's wrong with the code. or what can we do to make it work!
Thanks
Donaat, from the given details, we have found that your requirement is to replace the text with other text in the Word document. To achieve your requirement, we suggested you pass text to be found (“{{article-desc}}”) as a first parameter and pass text to replace("******** Text changed *******") as a second parameter in the Replace(String, String, Boolean, Boolean) API.
We have modified the sample application to
achieve your requirement and it can be downloaded from the attachment.
Please refer to the below link to know more about how to
working with find and replace in the Word document,
https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace
Regards,
Anto Nihil S