【发布时间】:2021-01-13 23:56:44
【问题描述】:
所以直到最近这段代码才有效,然后用户更改了标题的格式。
我正在使用 C# 和 OpenXML 从标题中编辑术语 [Sample Client] 并将其替换为图像。这在过去有效,实际上适用于某些标题,但不适用于其他标题。我不知道为什么。我得到的错误是“无法插入 OpenXmlElement “newChild”,因为它是树的一部分”。
在线失败:
textPlaceHolder.Parent.InsertAfter<DocumentFormat.OpenXml.Wordprocessing.Drawing>(element, textPlaceHolder);
我找到了一些答案,提到我必须使用 CloneNode,但我不确定如何实现,或者为什么。我需要一些帮助。
对不起,糟糕的代码。
private static string AddImageToHeader(WordprocessingDocument wordDoc, string relationshipId, int xwidth, int yheight)
{
LogWriter log = new LogWriter("");
// Define the reference of the image.
string results = "";
var element =
new DocumentFormat.OpenXml.Wordprocessing.Drawing(
new DW.Inline(
//new DW.Extent() { Cx = 990000, Cy = 792000L },
new DW.Extent() { Cx = xwidth, Cy = yheight },
new DW.EffectExtent()
{
LeftEdge = 0L,
TopEdge = 0L,
RightEdge = 0L,
BottomEdge = 0L
},
new DW.DocProperties()
{
Id = (UInt32Value)1U,
Name = "Header Image"
},
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 = "New Bitmap Image.jpg"
},
new PIC.NonVisualPictureDrawingProperties()),
new PIC.BlipFill(
new A.Blip(
new A.BlipExtensionList(
new A.BlipExtension()
{
Uri =
"{28A0092B-C50C-407E-A947-70E740481C1C}"
})
)
{
Embed = relationshipId,
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 = xwidth, Cy = yheight }),
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"
});
// Append the reference to body, the element should be in a Run.
// Search for text holder
Text textPlaceHolder = null;
try
{
foreach (var headerPart in wordDoc.MainDocumentPart.HeaderParts)
{
//Gets the text in headers
textPlaceHolder = headerPart.RootElement.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().Where((x) => x.Text.Contains("Sample Client")).First();
// Insert image (the image created with your function) after text place holder.
textPlaceHolder.Parent.InsertAfter<DocumentFormat.OpenXml.Wordprocessing.Drawing>(element, textPlaceHolder);
// Remove text place holder.
textPlaceHolder.Remove();
// }
foreach (var currentText in headerPart.RootElement.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>())
{
currentText.Text = currentText.Text.Replace("[", "");
currentText.Text = currentText.Text.Replace("]", "");
}
}
}
catch (Exception ex)
{
results = results + "\r\n" + ex.Message.ToString();
log.LogWrite("ERROR adding image to header: "+ex.Message.ToString());
log.ErrorWrite("ERROR adding image to header: " + ex.Message.ToString());
textPlaceHolder = null;
Console.WriteLine(results);
}
if (textPlaceHolder == null)
{
log.ErrorWrite("Error textPlaceHolder is null");
}
else
{
var parent = textPlaceHolder.Parent;
if (!(parent is Run)) // Parent should be a run element.
{
results = results + "\r\nERROR: Parent is not run\r\n";
//log.LogWrite("ERROR: Parent is not run");
}
else
{
// Insert image (the image created with your function) after text place holder.
textPlaceHolder.Parent.InsertAfter<DocumentFormat.OpenXml.Wordprocessing.Drawing>(element, textPlaceHolder);
// Remove text place holder.
textPlaceHolder.Remove();
}
}
return results;
}
【问题讨论】:
-
这个问题似乎是因为他们开始在文档中使用多个标题。一些标题正在工作,而有些则没有。我在想我缺少与多个标题有关的东西。
标签: c# image header openxml docx