gpt4 book ai didi

C# OpenXML 图像居中

转载 作者:太空宇宙 更新时间:2023-11-03 18:30:30 27 4
gpt4 key购买 nike

我正在尝试生成一个文档中心有图像的 DocX 文档,但我已经尝试了几种方法,但没有任何效果。图像正在显示,但在左上角。函数 addImageToBody 来自 MS 网站 (http://msdn.microsoft.com/en-us/library/office/bb497430(v=office.15).aspx)。我已尝试使用 Horizo​​ntalPosition 类 ( http://msdn.microsoft.com/en-us/library/documentformat.openxml.drawing.wordprocessing.horizontalposition(v=office.14).aspx ),但对我不起作用。

添加图片和调用函数:

 MainDocumentPart mainPart = document.MainDocumentPart;

ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png);


using (FileStream stream = new FileStream(@"C:...\Logo.png", FileMode.Open))
{
imagePart.FeedData(stream);

}

AddImageToBody(document, mainPart.GetIdOfPart(imagePart));

和函数:

 private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
{
int defX = 854;
int defY = 350;
int size = 3000;
// Define the reference of the image.
var element =
new DocumentFormat.OpenXml.Wordprocessing.Drawing(
new DW.Inline(
new DW.Extent() { Cx = defX * size, Cy = defY * size },
new DW.EffectExtent()
{
LeftEdge = 0L,
TopEdge = 0L,
RightEdge = 0L,
BottomEdge = 0L
},
new DW.DocProperties()
{
Id = (UInt32Value)1U,
Name = "Picture 1"
},
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 = 1000L, Y = 500L, },
new A.Extents() { Cx = defX * size, Cy = defY * size }),
new A.PresetGeometry(
new A.AdjustValueList()
) { Preset = A.ShapeTypeValues.Rectangle }))
) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
)
{
DistanceFromTop = (UInt32Value)999999,
DistanceFromBottom = (UInt32Value)10000,
DistanceFromLeft = (UInt32Value)10000,
DistanceFromRight = (UInt32Value)10000,
EditId = "50D07946"
});

// Append the reference to body, the element should be in a Run.
wordDoc.MainDocumentPart.Document.Body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(element)));
}

最佳答案

您需要将包含图像的段落居中。您可以使用段落属性(代码的最后一行)来做到这一点:

wordDoc.MainDocumentPart.Document.Body.AppendChild(
new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(element))
{
ParagraphProperties = new DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties()
{
Justification = new DocumentFormat.OpenXml.Wordprocessing.Justification()
{
Val = DocumentFormat.OpenXml.Wordprocessing.JustificationValues.Center
}
}
});

关于C# OpenXML 图像居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23057872/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com