用OpenXML中的图像替换文本持有人 [英] Replace text holder with image in OpenXML
问题描述
下面,我有使用OpenXML在Word的文档末尾插入图像的代码. 我需要做的是尝试在文档中找到名为[Image Holder]的项,并将其替换为我要传递的图像.
Below I have code that inserts an image at the end of a document in word using OpenXML. What I need to do is try and find and item called [Image Holder] within the document and replace that with the image I'm passing over.
这是将其添加到文档末尾的当前代码
Here is the current code that adds it to the end of a document
var element =
new Drawing(
new DW.Inline(
new DW.Extent() { Cx = 990000L, Cy = 792000L },
new DW.EffectExtent()
{
LeftEdge = 0L,
TopEdge = 0L,
RightEdge = 0L,
BottomEdge = 0L
},
new DW.DocProperties()
{
Id = (UInt32Value)1U,
Name = "NGSignature"
},
new DW.NonVisualGraphicFrameDrawingProperties(
new A.GraphicFrameLocks() { NoChangeAspect = true }),
new A.Graphic(
new A.GraphicData(
new PIC.Picture(
new PIC.NonVisualPictureProperties(
new PIC.NonVisualDrawingProperties()
{
Id = (UInt32Value)0U,
Name = "NGSignature.jpg"
},
new PIC.NonVisualPictureDrawingProperties()),
new PIC.BlipFill(
new A.Blip(
new A.BlipExtensionList(
new A.BlipExtension()
{
Uri =
"{28A0092B-C50C-407E-A947-70E740481C1C}"
})
)
{
Embed = relationship_id,
CompressionState =
A.BlipCompressionValues.Print
},
new A.Stretch(
new A.FillRectangle())),
new PIC.ShapeProperties(
new A.Transform2D(
new A.Offset() { X = 0L, Y = 0L },
new A.Extents() { Cx = 990000L, Cy = 792000L }),
new A.PresetGeometry(
new A.AdjustValueList()
) { Preset = A.ShapeTypeValues.Rectangle }))
) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
)
{
DistanceFromTop = (UInt32Value)0U,
DistanceFromBottom = (UInt32Value)0U,
DistanceFromLeft = (UInt32Value)0U,
DistanceFromRight = (UInt32Value)0U,
EditId = "50D07946"
});
word_doc.MainDocumentPart.Document.Body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(element)));
更新 好的,我在OpenXML工具中打开了文件,发现以下几行包含了我需要在XML文件中替换的数据.
UPDATE Ok i opened the file in the OpenXML tool and found the following lines that contain the data I need to replace inside the XML file.
<w:r>
<w:rPr>
<w:sz w:val="20" />
</w:rPr>
<w:instrText xml:space="preserve"> REF NG_MACRO "HOLDER" "3fd95b6f-4c63-42fb-ba2e-dc6d57975c57" </w:instrText>
</w:r>
和
<w:r>
<w:rPr>
<w:sz w:val="20" />
</w:rPr>
<w:t xml:space="preserve">{HOLDER}</w:t>
</w:r>
第二部分是打开文档时看到的内容. 第一部分我不确定,但是创建它的应用程序会将其放入.
The second part is what I see when I open the document. The first part i'm not sure but the application that creates it puts it in.
要正确执行此操作,我猜测需要删除第1部分,然后用图像部分替换第2部分.
To do this correctly i'm guessing the 1 part needs removed and then the image section to replace the 2nd part.
推荐答案
要将文本持有人替换为给定的图像,请执行以下步骤:
To replace a text holder with an given image use the following steps:
- 搜索文本占位符.
- 确定文本占位符的父项
- 将图像(
Drawing
元素)插入文本占位符之后. - 删除文本位置持有人.
- Search for the text place holder.
- Determine the parent of the text place holder
- Insert the image (
Drawing
element) after the text place holder. - Remove the text palce holder.
下面的代码实现了上述步骤:
The code below implements the steps described above:
// Search for text holder
Text textPlaceHolder = 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 = textPlaceHolder.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.
textPlaceHolder.Parent.InsertAfter<Drawing>(element, textPlaceHolder);
// Remove text place holder.
textPlaceHolder.Remove();
}
}
您还可以使用内容占位符(SdtElement
)代替简单的文本占位符.
You could also use content place holders (SdtElement
) instead of simple text place holders.
这篇关于用OpenXML中的图像替换文本持有人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!