如何在 C# 中使用 Open xml 从 docx 文件中获取图像名称或 ID?
How to get Image Name or Id from docx file using Open xml in C#?
我正在使用 Mvc.Net API 打开 XML。我需要替换 .docx 中的多个图像 file.I 替换当前场景中的图像,但我在代码端没有获得图像的任何 ID 或名称,因此面临替换这些图像的困难。
这是我的代码
List<ImagePart> imgPartList = doc.MainDocumentPart.ImageParts.ToList();
foreach(ImagePart imgPart in imgPartList)
{
string Id=doc.MainDocumentPart.GetIdOfPart(imgPart);
byte[] imageBytes = File.ReadAllBytes(ImagePath);
BinaryWriter writer = new BinaryWriter(imgPart.GetStream());
writer.Write(imageBytes);
writer.Close();
}
能否获取ImagePart中Image的名称?
我会这样做:
List<ImagePart> imgPartList = doc.MainDocumentPart.ImageParts.ToList();
foreach(ImagePart imgPart in imgPartList)
{
var imageId = document.MainDocumentPart.GetIdOfPart(imgPart.Id);
byte[] imageBytes = File.ReadAllBytes(ImagePath);
BinaryWriter writer = new BinaryWriter(imgPart.GetStream());
writer.Write(imageBytes);
writer.Close();
}
还有
This answer could help you
我正在使用 Mvc.Net API 打开 XML。我需要替换 .docx 中的多个图像 file.I 替换当前场景中的图像,但我在代码端没有获得图像的任何 ID 或名称,因此面临替换这些图像的困难。 这是我的代码
List<ImagePart> imgPartList = doc.MainDocumentPart.ImageParts.ToList();
foreach(ImagePart imgPart in imgPartList)
{
string Id=doc.MainDocumentPart.GetIdOfPart(imgPart);
byte[] imageBytes = File.ReadAllBytes(ImagePath);
BinaryWriter writer = new BinaryWriter(imgPart.GetStream());
writer.Write(imageBytes);
writer.Close();
}
能否获取ImagePart中Image的名称?
我会这样做:
List<ImagePart> imgPartList = doc.MainDocumentPart.ImageParts.ToList();
foreach(ImagePart imgPart in imgPartList)
{
var imageId = document.MainDocumentPart.GetIdOfPart(imgPart.Id);
byte[] imageBytes = File.ReadAllBytes(ImagePath);
BinaryWriter writer = new BinaryWriter(imgPart.GetStream());
writer.Write(imageBytes);
writer.Close();
}
还有
This answer could help you