I have a requirement of generating a report out of pre-defined document template from my C# code. While generating report I have to replace placeholders in the template with text or images. I am using OpenXmlPowerTool’s Regex feature to replace placeholder
with simple text or paragraph contents.
But I am unable to replace the placeholder with multiple images using Regex feature. Could anyone please suggest or share the code to achieve this?
My export functionality is stuck because of images not getting replaced. Please help
Accroding to your description,as far as I think,it's impossible to replace the placeholder with images using Regex.Regex.replace must use string to replace the placeholder.I suggest you could do this:
1.Search for the placeholder.
2.Determine the parent of the placeholder
3.Insert the image (Drawing element) after the placeholder.
4.Remove the palceholder.
The codes just like this:
Text PlaceHolder = word_doc.MainDocumentPart.Document.Body.Descendants<Text>()
.Where((x) => x.Text == "$image_tag$").First();
if (textPlaceHolder == null)
{
Console.WriteLine("Text holder not found!");
}
else
{
var parent = PlaceHolder.Parent;
if(!(parent is Run)) // Parent should be a run element.
{
Console.Out.WriteLine("Parent is not run");
}
else
{
// Insert image (the image created with your function) after text place holder.
PlaceHolder.Parent.InsertAfter<Drawing>(element, PlaceHolder);
// Remove text place holder.
PlaceHolder.Remove();
}
}
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
Thanks for the solution suggested. But the solution is using OpenXML SDK reference and have a very complex way of doing it by creating the Drawing object!!!
Is it not possible with any method in OpenXmlPowerTools like we have Regex for text replacements? I am using OpenXmlPowerTools for Text and Paragraph replacements and I don't want to bring in any other tool for image replacement as text replacements with
powertools are working perfectly fine. So would prefer that only.
The replacement must be a string.You could use replace to paragraph.So,the image could be appended into the paragraph and then it as a paragraph replace the placeholder.
So,as far as I think,if you want to repalce to image,you must use drawing.
Best regards,
Yijing Sun
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
I tried the solution you suggested earlier with the reference to Microsoft Docs but it is not replacing/adding images in the document. It is also discarding my previously done text replacements. Meaning after adding Drawing objects to the Run element it
is not saving it to the document even after calling reportDocument.Save() explicitly.
The problem is for Text replacement I am using OpenXmlPowerTools methods and saving document using PutXDocument() and for Image replacement I am using OpenXml SDK methods and saving the document using ReportDocument.Save() method as it does not have PutXDocument().
and because of this my previously done text replacements are getting discarded after image replacements.
As far as I think,you could still use OpenXml SDK methods to replace placeholder text and images.
You could do just like this:
1.Make a temporary copy of your template file, naming it whatever you want.
2.Perform your OpenXml changes on the above file.
3.Save the appropriate sections (ie. using the .myWordDocument.MainDocumentPart.Document.Save() method for the main content or someHeaderPart.Header.Save() method for a particular header).
Best regards,
Yijing Sun
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
Thanks for helping me out. The solution you suggested earlier for image replacement worked. I am able to replace standalone placeholders with images using Drawing object. But the only issue I am facing now is it does not replace the placeholder if the placeholder
is inside any paragraph. For example I have one paragraph in my template doc as below with two placeholders in it
"The searches conducted on the open public domain revealed that Company has its own official website as .... The website of the company reveals that the company is “As per website......” The screenshot of the website has been shown below: {InternetScreenShot01}
On further searches into product categories and we found {Status} brand / mark products was mentioned on the website. The screenshot of the search results showing the {Status} brand / mark products has been shown below: {InternetScreenShot02}"
Here {InternetScreenShot01}, {InternetScreenShot02} are the two placeholders which I want to replace with respective image. But with the solution you suggested it is not replacing such placeholders are it is unable to treat them as a standalone element.
Could you please suggest how should I handle this?
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
Member
1 Points
22 Posts
How to replace placeholder text with multiple images in word document using OpenXmlPowerTools in...
Jul 02, 2020 12:40 PM|Mayurib|LINK
Hi,
I have a requirement of generating a report out of pre-defined document template from my C# code. While generating report I have to replace placeholders in the template with text or images. I am using OpenXmlPowerTool’s Regex feature to replace placeholder with simple text or paragraph contents.
But I am unable to replace the placeholder with multiple images using Regex feature. Could anyone please suggest or share the code to achieve this?
My export functionality is stuck because of images not getting replaced. Please help
Thanks in advance.
Contributor
4020 Points
1566 Posts
Re: How to replace placeholder text with multiple images in word document using OpenXmlPowerTools...
Jul 03, 2020 05:38 AM|yij sun|LINK
Hi Mayurib,
Accroding to your description,as far as I think,it's impossible to replace the placeholder with images using Regex.Regex.replace must use string to replace the placeholder.I suggest you could do this:
1.Search for the placeholder.
2.Determine the parent of the placeholder
3.Insert the image (Drawing element) after the placeholder.
4.Remove the palceholder.
The codes just like this:
More details,you could refer to below article:
https://docs.microsoft.com/en-us/office/open-xml/how-to-insert-a-picture-into-a-word-processing-document?redirectedfrom=MSDN#code-snippet-5
Best regards,
Yijing Sun
Member
1 Points
22 Posts
Re: How to replace placeholder text with multiple images in word document using OpenXmlPowerTools...
Jul 03, 2020 06:31 PM|Mayurib|LINK
Hi Yijing Sun,
Thanks for the solution suggested. But the solution is using OpenXML SDK reference and have a very complex way of doing it by creating the Drawing object!!!
Is it not possible with any method in OpenXmlPowerTools like we have Regex for text replacements? I am using OpenXmlPowerTools for Text and Paragraph replacements and I don't want to bring in any other tool for image replacement as text replacements with powertools are working perfectly fine. So would prefer that only.
Thanks.
Contributor
4020 Points
1566 Posts
Re: How to replace placeholder text with multiple images in word document using OpenXmlPowerTools...
Jul 06, 2020 09:46 AM|yij sun|LINK
Hi Mayurib,
As far as I think,there's no way to replace placeholder to image using Regex like text and paragraph.
The replacement must be a string.You could use replace to paragraph.So,the image could be appended into the paragraph and then it as a paragraph replace the placeholder.
So,as far as I think,if you want to repalce to image,you must use drawing.
Best regards,
Yijing Sun
Member
1 Points
22 Posts
Re: How to replace placeholder text with multiple images in word document using OpenXmlPowerTools...
Jul 09, 2020 12:11 PM|Mayurib|LINK
Hi Yijing Sun,
I tried the solution you suggested earlier with the reference to Microsoft Docs but it is not replacing/adding images in the document. It is also discarding my previously done text replacements. Meaning after adding Drawing objects to the Run element it is not saving it to the document even after calling reportDocument.Save() explicitly.
The problem is for Text replacement I am using OpenXmlPowerTools methods and saving document using PutXDocument() and for Image replacement I am using OpenXml SDK methods and saving the document using ReportDocument.Save() method as it does not have PutXDocument(). and because of this my previously done text replacements are getting discarded after image replacements.
Could you suggest any fix for this?
Thanks.
Contributor
4020 Points
1566 Posts
Re: How to replace placeholder text with multiple images in word document using OpenXmlPowerTools...
Jul 10, 2020 07:44 AM|yij sun|LINK
Hi Mayurib,
As far as I think,you could still use OpenXml SDK methods to replace placeholder text and images.
You could do just like this:
1.Make a temporary copy of your template file, naming it whatever you want.
2.Perform your OpenXml changes on the above file.
3.Save the appropriate sections (ie. using the .myWordDocument.MainDocumentPart.Document.Save() method for the main content or someHeaderPart.Header.Save() method for a particular header).
Best regards,
Yijing Sun
Member
1 Points
22 Posts
Re: How to replace placeholder text with multiple images in word document using OpenXmlPowerTools...
Jul 29, 2020 07:51 PM|Mayurib|LINK
Hi Yijing Sun,
Thanks for helping me out. The solution you suggested earlier for image replacement worked. I am able to replace standalone placeholders with images using Drawing object. But the only issue I am facing now is it does not replace the placeholder if the placeholder is inside any paragraph. For example I have one paragraph in my template doc as below with two placeholders in it
"The searches conducted on the open public domain revealed that Company has its own official website as .... The website of the company reveals that the company is “As per website......” The screenshot of the website has been shown below: {InternetScreenShot01} On further searches into product categories and we found {Status} brand / mark products was mentioned on the website. The screenshot of the search results showing the {Status} brand / mark products has been shown below: {InternetScreenShot02}"
Here {InternetScreenShot01}, {InternetScreenShot02} are the two placeholders which I want to replace with respective image. But with the solution you suggested it is not replacing such placeholders are it is unable to treat them as a standalone element.
Could you please suggest how should I handle this?
Thanks.
Contributor
4020 Points
1566 Posts
Re: How to replace placeholder text with multiple images in word document using OpenXmlPowerTools...
Jul 31, 2020 07:40 AM|yij sun|LINK
Hi Mayurib,
Accroding to your description,as far as I think you could use content place holders (SdtElement) instead of simple text place holders.
More details,you could refer to below article:
https://stackoverflow.com/questions/19159437/replace-text-holder-with-image-in-openxml
Best regards,
Yijing Sun