Replace text with image
Home › Forums › WordprocessingML › Replace text with image
This topic contains 2 replies, has 2 voices, and was last updated by Anonymous 4 years, 2 months ago.
-
AuthorPosts
-
September 10, 2019 at 2:36 pm #7951
I’m stuck and need some help. My requirement is this: I give users a word document as a template and a way to upload their letterhead as an image which I store as a binary in a SQL db. When I process the word doc, if my code finds the tag [Letterhead], it will replace the tag with their letterhead image.
I’ve studied the examples and screencasts (which are great), and so far I am streaming the image into the file. I just haven’t been able to replace the tag and get the image to show.
My questions:
1. How do I programatically locate the tag that I want to replace?
2. How do insert my image markup into that location?My code is below. Note that I am streaming the document from a binary (byte[] array) stored in a SQL db and I am also streaming the image. (so I’m not opening either from a file). Also, I’ve created a class DocImage, which just holds the image binary and its properties.
Thanks for the help.
using (MemoryStream stream = new MemoryStream())
{
stream.Write(binary, 0, (int)binary.Length);
using (WordprocessingDocument doc = WordprocessingDocument.Open(stream, true))
{
string imageRId = “r” + Guid.NewGuid().ToString().Replace(“-“, String.Empty);
MainDocumentPart mainPart = doc.MainDocumentPart;ImagePartType imagePartType = new ImagePartType();
imagePartType = ImagePartType.Jpeg;
ImagePart imagePart = mainPart.AddImagePart(imagePartType, imageRId);
using (MemoryStream ms = new MemoryStream(letterhead.Image))
{
imagePart.FeedData(ms);
}
string relType = imagePart.RelationshipType.ToString();XElement imageElement = createImageElement(letterhead, imageRId);
…
private static XElement createImageElement(DocImage letterhead, string imageRId)
{
object imageWidth;
object imageHeight;
object imageWidthExtent;
object imageHeightExtent;
imageWidth = letterhead.EMU(letterhead.WidthPx);
imageHeight = letterhead.EMU(letterhead.HeightPx); ;
imageWidthExtent = 0;
imageHeightExtent = 0;XElement imageElement = new XElement(W.drawing,
new XElement(WP.inline,
new XAttribute(“distT”, “0”),
new XAttribute(“distB”, “0”),
new XAttribute(“distL”, “0”),
new XAttribute(“distR”, “0”),
new XElement(WP.extent,
new XAttribute(“cx”, imageWidthExtent),
new XAttribute(“cy”, imageHeightExtent)
),
new XElement(WP.effectExtent,
new XAttribute(“l”, “0”),
new XAttribute(“t”, “0”),
new XAttribute(“r”, “0”),
new XAttribute(“b”, “0”)
),
new XElement(WP.docPr,
new XAttribute(“id”, “1”),
new XAttribute(“name”, letterhead.Filename)
),
new XElement(WP.cNvGraphicFramePr,
new XElement(A.graphicFrameLocks,
new XAttribute(XNamespace.Xmlns + “a”, A.a),
new XAttribute(“noChangeAspect”, “1”))
),
new XElement(A.graphic,
new XAttribute(XNamespace.Xmlns + “a”, A.a),
new XElement(A.graphicData,
new XAttribute(“uri”, Pic.pic),
new XElement(Pic._pic,
new XAttribute(XNamespace.Xmlns + “pic”, Pic.pic),
new XElement(Pic.nvPicPr,
new XElement(Pic.cNvPr,
new XAttribute(“id”, “0”),
new XAttribute(“name”, letterhead.Filename)
),
new XElement(Pic.cNvPicPr)
),
new XElement(Pic.blipFill,
new XElement(A.blip,
new XAttribute(R.r + “embed”, imageRId)
),
new XElement(A.stretch,
new XElement(A.fillRect)
)
),
new XElement(Pic.spPr,
new XElement(A.xfrm,
new XElement(A.off,
new XAttribute(“x”, “0”),
new XAttribute(“y”, “0”)
),
new XElement(A.ext,
new XAttribute(“cx”, imageWidth),
new XAttribute(“cy”, imageHeight)
)
),
new XElement(A.prstGeom,
new XAttribute(“prst”, “rect”),
new XElement(A.avLst)
)
)
)
)
)
)
);
return imageElement;
}September 13, 2019 at 10:34 pm #7953I’ve resolved my issue.
I went back to Eric’s blog ‘Inserting an Image into a Bookmark in an OpenXML WordprocessingML Document’. At the bottom, there is a link to an example, however that link downloads a project ‘RetrieveBookmarkText.zip’, which does not have the examples about images. I looked through the folder though and found and downloaded the project in ‘ReplaceBookmark.zip’, and that had all the examples I needed to resolve my issue.
I suggest changing the link in that article to:
http://www.ericwhite.com/blog/wp-content/uploads/2016/03/ReplaceBookmark.zipThanks for making these available. They are great!
October 2, 2020 at 11:07 am #9726
AnonymousOpen Windows Explorer.
Locate the image file that you want to use.
Take right click on the file and select Copy.
Open MS Word document in which replacement is to be done.
Press Ctrl + H to launch Find and Replace dialog box. -
AuthorPosts
You must be logged in to reply to this topic.