2014-02-06 87 views
0

我正在開發一個具有動態創建DocX文件功能的程序。如標題所示,我在將圖像插入DocX文件時遇到問題。正如我所看到的,其中一個問題是我正在使用C#2.0。 (在回答這個問題時,我想強調一下,我不希望切換到C#3.0,所以請不要試圖說服我。)

我看了一下MSDN文章http://msdn.microsoft.com/en-us/library/office/bb497430.aspx,但是當我將MSDN使用的C#3.0代碼轉換爲C#2.0,我得到一個不打開的文檔,它給了我一個錯誤:「文件」Testing.docx無法打開,因爲內容有問題「和」沒有錯誤可用的細節「。將圖像插入.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))); 
} 

(變量名是壞的,但是這只是一個測試程序,所以我沒有花太多的時間另外,我還行間隔模仿。如果我可以使用C#3.0語法,但是我使用的是Visual Studio 2005.)

+0

您的問題不太可能與C#版本有關。這更可能是由於.NET的版本。 –

+0

我在聲明中的含義是,我可能已經搞砸了使用此代碼構建的id樹,因爲在C#3.0中添加了Collection Initializing,語法如此不同。 – Zapparatus

回答

0

我找到了答案。令人驚訝的是,我沒有找到這個網頁 - 因爲它是一些谷歌頁面搜索一些奇怪的關鍵字 - 完全工作:http://blog.stuartwhiteford.com/?p=33。雖然它是用集合初始化編寫的,但我能夠將它們更改爲符合C#2.0標準。