If somebody like the post and its helpful in your work then, add comments.

Thursday, July 16, 2009

Open XML : Image Part in the Word document

To insert image in the word document. Use reflector tool which comes with Open Xml 2.0 SDk. it will make great help in generatin the code. For inserting a image use below steps:

1. Create a blanck word dcoument.
2. Copy paste the image(which you want to insert).
3. The go to DocumentReflectorTool.
4. Open this document in that tool. copy the image code.
5. Add following code to solution:
ImagePart imgPart = mainPart.AddImagePart(ImagePartType.Jpeg);
using (FileStream strem = new FileStream(@"C:\Bharat.JPG", FileMode.Open))
{
imgPart.FeedData(strem);
}
imagePartID = mainPart.GetIdOfPart(imgPart);
mainPart.AddExtendedPart ("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "media/image1.jpeg", imagePartID);
Paragraph ImageParagraph = new Paragraph();

body.Append(new Paragraph(new Run(GenerateDrawing())));

Copied code from reflector tool :

public static Drawing GenerateDrawing()
{
var element =
new Drawing(
new wp.Inline(
new wp.Extent(){ Cx = 536575L, Cy = 407670L },
new wp.EffectExtent(){ LeftEdge = 19050L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
new wp.DocProperties(){ Id = (UInt32Value)1U, Name = "Picture 1", Description = "C:\\Bharat.JPG" },
new wp.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 = "Picture 1", Description = "C:\\Bharat.JPG" },
new pic.NonVisualPictureDrawingProperties(
new a.PictureLocks(){ NoChangeAspect = true, NoChangeArrowheads = true })),
new pic.BlipFill(
new a.Blip() { Embed = imagePartID, CompressionState = a.BlipCompressionValues.Print },
new a.SourceRectangle(),
new a.Stretch(
new a.FillRectangle())),
new pic.ShapeProperties(
new a.Transform2D(
new a.Offset(){ X = 0L, Y = 0L },
new a.Extents(){ Cx = 536575L, Cy = 407670L }),
new a.PresetGeometry(
new a.AdjustValueList()
){ Preset = a.ShapeTypeValues.Rectangle },
new a.NoFill(),
new a.Outline(
new a.NoFill(),
new a.Miter(){ Limit = 800000 },
new a.HeadEnd(),
new a.TailEnd()
){ Width = 9525 }
){ BlackWhiteMode = a.BlackWhiteModeValues.Auto })
){ Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
){ DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U });
return element;
}

2 comments:

  1. is a great post, very useful.


    how do I make the image to be it text wrapping equals through?

    ReplyDelete
  2. hi,

    i have problem , now i create Picture Library contain images these image i want to insert these image from Picture library to Microsoft Office Word

    ReplyDelete