OpenXML 添加图像在 Document 媒体文件夹中但不显示在 Office 中
OpenXML added Image is in the Document media folder but not displayed in Office
我在创建 Word 文档 (openXML) 和向其中添加图像时遇到问题。我已经尝试了很多我发现的代码片段以及官方 MS 文档 (https://docs.microsoft.com/de-de/office/open-xml/how-to-insert-a-picture-into-a-word-processing-document)。
为了让它正常工作,我基本上从上面的 Link 复制了完整的代码,并包装了 2 个 WPF FileDialogs 以首先创建一个文档,然后选择要添加的图像。我还在图像前添加了一些文本,以检查访问文档是否正常。
一切都编译和运行没有错误。但是,创建的文档只有我的文字,没有图像的迹象。我经常读到有人遇到诸如“红色 X”之类的错误或缺少添加图像的资源的某些迹象,但对我而言,该文档只包含添加的文本。
但是,当我将文档作为存档打开时(例如使用 winrar),我可以看到我选择的图像位于我的文档的“媒体”文件夹中。
为什么我用office打开文档没有任何显示?
下面是我的代码,但它基本上来自官方 SDK:
using A = DocumentFormat.OpenXml.Drawing;
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using PIC = DocumentFormat.OpenXml.Drawing.Pictures;
public static void ExportToDoc()
{
SaveFileDialog exportFile = new SaveFileDialog
{
Filter = "Word Document (*.docx)|*.docx",
AddExtension = true,
FileName = "Proposed-Filename.docx"
};
switch (exportFile.ShowDialog())
{
case true:
WordprocessingDocument doc = WordprocessingDocument.Create(exportFile.FileName, WordprocessingDocumentType.Document);
// Make some changes to the document.
MainDocumentPart mainDocumentPart = doc.AddMainDocumentPart();
mainDocumentPart.Document = new Document();
Document document = mainDocumentPart.Document;
Body body = document.AppendChild(new Body());
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("Created text in body."));
//Prepare Image
OpenFileDialog openImage = new OpenFileDialog
{
Filter = "Bild (*.jpg)| *.jpg"
};
//Document is created
doc.Close();
switch (openImage.ShowDialog())
{
case true:
InsertAPicture(exportFile.FileName, openImage.FileName);
break;
default:
break;
}
Console.WriteLine("Bild angehängt");
break;
default:
break;
}
}
//from MSDN after this point:
public static void InsertAPicture(string document, string fileName)
{
using (WordprocessingDocument wordprocessingDocument =
WordprocessingDocument.Open(document, true))
{
MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
using (FileStream stream = new FileStream(fileName, FileMode.Open))
{
imagePart.FeedData(stream);
}
AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart));
}
}
private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
{
// Define the reference of the image.
var element =
new DocumentFormat.OpenXml.Office.Drawing.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 = "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 = 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"
});
// Append the reference to body, the element should be in a Run.
wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));
}
}
好的,我自己想出来了:
我为绘图使用了错误的命名空间。就是这样。
有趣的是,这根本没有产生任何错误,但却不起作用。
因此,如果您遇到此问题,这就是我所做的更改:
private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
{
// Define the reference of the image.
var element =
new DocumentFormat.OpenXml.Office.Drawing.Drawing(
更改为:
private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
{
// Define the reference of the image.
var element =
new DocumentFormat.OpenXml.Wordprocessing.Drawing(
我在创建 Word 文档 (openXML) 和向其中添加图像时遇到问题。我已经尝试了很多我发现的代码片段以及官方 MS 文档 (https://docs.microsoft.com/de-de/office/open-xml/how-to-insert-a-picture-into-a-word-processing-document)。
为了让它正常工作,我基本上从上面的 Link 复制了完整的代码,并包装了 2 个 WPF FileDialogs 以首先创建一个文档,然后选择要添加的图像。我还在图像前添加了一些文本,以检查访问文档是否正常。
一切都编译和运行没有错误。但是,创建的文档只有我的文字,没有图像的迹象。我经常读到有人遇到诸如“红色 X”之类的错误或缺少添加图像的资源的某些迹象,但对我而言,该文档只包含添加的文本。 但是,当我将文档作为存档打开时(例如使用 winrar),我可以看到我选择的图像位于我的文档的“媒体”文件夹中。
为什么我用office打开文档没有任何显示?
下面是我的代码,但它基本上来自官方 SDK:
using A = DocumentFormat.OpenXml.Drawing;
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using PIC = DocumentFormat.OpenXml.Drawing.Pictures;
public static void ExportToDoc()
{
SaveFileDialog exportFile = new SaveFileDialog
{
Filter = "Word Document (*.docx)|*.docx",
AddExtension = true,
FileName = "Proposed-Filename.docx"
};
switch (exportFile.ShowDialog())
{
case true:
WordprocessingDocument doc = WordprocessingDocument.Create(exportFile.FileName, WordprocessingDocumentType.Document);
// Make some changes to the document.
MainDocumentPart mainDocumentPart = doc.AddMainDocumentPart();
mainDocumentPart.Document = new Document();
Document document = mainDocumentPart.Document;
Body body = document.AppendChild(new Body());
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("Created text in body."));
//Prepare Image
OpenFileDialog openImage = new OpenFileDialog
{
Filter = "Bild (*.jpg)| *.jpg"
};
//Document is created
doc.Close();
switch (openImage.ShowDialog())
{
case true:
InsertAPicture(exportFile.FileName, openImage.FileName);
break;
default:
break;
}
Console.WriteLine("Bild angehängt");
break;
default:
break;
}
}
//from MSDN after this point:
public static void InsertAPicture(string document, string fileName)
{
using (WordprocessingDocument wordprocessingDocument =
WordprocessingDocument.Open(document, true))
{
MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
using (FileStream stream = new FileStream(fileName, FileMode.Open))
{
imagePart.FeedData(stream);
}
AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart));
}
}
private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
{
// Define the reference of the image.
var element =
new DocumentFormat.OpenXml.Office.Drawing.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 = "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 = 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"
});
// Append the reference to body, the element should be in a Run.
wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));
}
}
好的,我自己想出来了:
我为绘图使用了错误的命名空间。就是这样。
有趣的是,这根本没有产生任何错误,但却不起作用。
因此,如果您遇到此问题,这就是我所做的更改:
private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
{
// Define the reference of the image.
var element =
new DocumentFormat.OpenXml.Office.Drawing.Drawing(
更改为:
private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
{
// Define the reference of the image.
var element =
new DocumentFormat.OpenXml.Wordprocessing.Drawing(