I am attempting to use OpenXML with C# to create a DOCX word document. The text and formatting on the document work very well. But, when I add an image to the document, Word 2007 will not open the document. It's detailed error is "Unspecified Error".
Below is a program that demonstrates the error. The resultant .DOCX opens fine in Word 2010 but fails in Word 2007. I have tried the 2.0 and 2.5 versions of the SDK. Any thoughts would be appreciated.
using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using A = DocumentFormat.OpenXml.Drawing;
using PIC = DocumentFormat.OpenXml.Drawing.Pictures;
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
namespace ImageTest2007a
{
class Program
{
static void Main(string[] args)
{
string imagefilename = @"c:\work\temp\ANI.jpg";
// Create the Word document
using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(@"C:\temp\t1.docx", WordprocessingDocumentType.Document))
{
// Create the "Main Part" of the document. Not really used much externally here.
MainDocumentPart theDoc = wordDocument.AddMainDocumentPart(); // Not directly referenced as 'theDOC'. The reference wordDocument.MainDocumentPart is used.
theDoc.Document = new Document();
// Create and attach the body to the document. The body of the document is where the document content is placed.
Body theBody = theDoc.Document.AppendChild(new Body()); // Not directly referenced as 'body'. The reference within wordDocument.MainDocumentPart.Document.Body is used.
// attach the image
ImagePart imagePart = theDoc.AddImagePart(ImagePartType.Png);
using (FileStream stream = new FileStream(imagefilename, FileMode.Open))
{
imagePart.FeedData(stream);
}
AddImageToBody(wordDocument, theDoc.GetIdOfPart(imagePart));
theDoc.Document.Save();
}
}
private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
{
// Define the reference of the image.
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 = "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)));
}
}
}