将图像插入.docx文件

时间:2014-02-06 00:54:28

标签: c# .net c#-2.0 openxml docx

我正在开发一个具有动态创建DocX文件功能的程序。正如标题所示,我在将图像插入DocX文件时遇到问题。我认为其中一个问题是我使用的是C#2.0。 (在回答这个问题时,我想强调一点,我不想切换到C#3.0,所以请不要试图说服我。)

我查看了http://msdn.microsoft.com/en-us/library/office/bb497430.aspx上的MSDN文章,但是当我将MSDN使用的C#3.0代码转换为C#2.0时,我得到的文档无法打开,它给了我错误:“该文件无法打开Testing.docx,因为内容存在问题,“和”没有可用的错误详细信息。“

这是我的代码:

ImagePart ip_Part;
WordprocessingDocument wpd_Doc = WordprocessingDocument.Create("C:\\Convert\\Testing.docx", WordprocessingDocumentType.Document);
public Testing()
{
    wpd_Doc.AddMainDocumentPart();
    wpd_Doc.MainDocumentPart.Document = new Document();
    wpd_Doc.MainDocumentPart.Document.Body = new Body();

    ip_Part = wpd_Doc.MainDocumentPart.AddImagePart(ImagePartType.Png);
    System.IO.FileStream fs_Stream = new System.IO.FileStream("image.png", System.IO.FileMode.Open);
    ip_Part.FeedData(fs_Stream);

    AddImageToBody("image.png", wpd_Doc.MainDocumentPart.GetIdOfPart(ip_Part));

    AppendText("Here is a test bulleted list:");

    wpd_Doc.MainDocumentPart.Document.Save();

    //Close the document.
    wpd_Doc.Close();
}

private void AddImageToBody(string s_ImagePath, string s_RelationshipId)
{
    //OpenXmlElement oxe_Element = new Numbering();
    Drawing d_Drawing = new Drawing();
    DrawWord.Inline i_Inline = new DrawWord.Inline();
    DrawWord.Extent e_Extent = new DrawWord.Extent();
    e_Extent.Cx = 600075; //63px / 96dpi * 914400
    e_Extent.Cy = 600075; //63px / 96dpi * 914400
    i_Inline.Extent = e_Extent;

    DrawWord.DocProperties dp_Prop = new DrawWord.DocProperties();
    //dp_Prop.Id = uint.Parse(System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString());
    dp_Prop.Id = 1;
    dp_Prop.Name = "Picture 1";
    dp_Prop.Description = "An automated image.";
    i_Inline.DocProperties = dp_Prop;

    DrawWord.NonVisualGraphicFrameDrawingProperties nvgfdp_Prop = new DrawWord.NonVisualGraphicFrameDrawingProperties();
    Draw.GraphicFrameLocks gfl_Locks = new Draw.GraphicFrameLocks();
    gfl_Locks.NoChangeAspect = true;
    nvgfdp_Prop.GraphicFrameLocks = gfl_Locks;
    i_Inline.NonVisualGraphicFrameDrawingProperties = nvgfdp_Prop;

    Draw.Graphic g_Graphic = new Draw.Graphic();
    Draw.GraphicData gd_Data = new DocumentFormat.OpenXml.Drawing.GraphicData();
    gd_Data.Uri = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
    DrawPic.Picture p_Pic = new DrawPic.Picture();
    DrawPic.NonVisualPictureProperties nvpp_Prop = new DrawPic.NonVisualPictureProperties();
    DrawPic.NonVisualDrawingProperties nvdp_Prop = new DrawPic.NonVisualDrawingProperties();
    nvdp_Prop.Id = uint.Parse(System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString());
    //nvdp_Prop.Name = s_ImagePath;
    nvdp_Prop.Name = "New_Image.png";
    nvpp_Prop.NonVisualDrawingProperties = nvdp_Prop;

    DrawPic.NonVisualPictureDrawingProperties nvpdp_Prop = new DrawPic.NonVisualPictureDrawingProperties();
    nvpp_Prop.NonVisualPictureDrawingProperties = nvpdp_Prop;
    p_Pic.NonVisualPictureProperties = nvpp_Prop;

    DrawPic.BlipFill bf_Fill = new DrawPic.BlipFill();
    Draw.Blip b_Blip = new Draw.Blip();
    Draw.ExtensionList el_List = new Draw.ExtensionList();
    Draw.BlipExtension be_Extension = new Draw.BlipExtension();
    be_Extension.Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}";
    el_List.Append(be_Extension);
    b_Blip.Append(el_List);
    b_Blip.Embed = s_RelationshipId;
    b_Blip.CompressionState = Draw.BlipCompressionValues.Print;
    bf_Fill.Blip = b_Blip;

    Draw.Stretch s_Stretch = new Draw.Stretch();
    Draw.FillRectangle fr_Rect = new Draw.FillRectangle();
    s_Stretch.FillRectangle = fr_Rect;
    bf_Fill.Append(s_Stretch);
    p_Pic.BlipFill = bf_Fill;

    DrawPic.ShapeProperties sp_Prop = new DrawPic.ShapeProperties();
    Draw.Transform2D t2d_Transform = new Draw.Transform2D();
    Draw.Offset o_Offset = new Draw.Offset();

    o_Offset.X = 0;
    o_Offset.Y = 0;
    t2d_Transform.Offset = o_Offset;

    Draw.Extents e_Extents = new Draw.Extents();
    e_Extents.Cx = 600075; //63px / 96dpi * 914400
    e_Extents.Cy = 600075; //63px / 96dpi * 914400
    t2d_Transform.Extents = e_Extents;
    sp_Prop.Transform2D = t2d_Transform;

    Draw.PresetGeometry pg_Geom = new Draw.PresetGeometry();
    Draw.AdjustValueList avl_List = new Draw.AdjustValueList();
    pg_Geom.AdjustValueList = avl_List;
    pg_Geom.Preset = Draw.ShapeTypeValues.Rectangle;
    sp_Prop.Append(pg_Geom);
    p_Pic.ShapeProperties = sp_Prop;
    gd_Data.Append(p_Pic);
    g_Graphic.GraphicData = gd_Data;
    i_Inline.Graphic = g_Graphic;
    d_Drawing.Inline = i_Inline;
    //oxe_Element.Append(d_Drawing);
    //Run r_Run = new Run(d_Drawing);
    wpd_Doc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(d_Drawing)));
}

(变量名称不好,但这只是一个测试程序,所以我没有花太多时间。而且,我的行间距模仿MSDN在其示例中所具有的Xml格式。如果我可以使用我会使用C#3.0语法,但我使用的是Visual Studio 2005。)

1 个答案:

答案 0 :(得分:0)

我找到了答案。令人惊讶的是,我没有找到这个网页 - 因为它是一些谷歌页面关闭了一些奇怪的关键字搜索 - 完全有效:http://blog.stuartwhiteford.com/?p=33。虽然它是用Collection Initializations编写的,但我能够将它们更改为符合C#2.0标准。