最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#采用OpenXml給word里面插入圖片

 更新時(shí)間:2014年09月24日 10:40:14   投稿:shichen2014  
這篇文章主要介紹了C#采用OpenXml給word里面插入圖片的方法,參考了MSDN官網(wǎng)的示例加以說明,是OpenXml操作Word的一個(gè)非常重要的應(yīng)用,需要的朋友可以參考下

本文實(shí)例講述了C#采用OpenXml給word里面插入圖片的方法,分享給大家供大家參考。具體分析如下:

首先需要指出的是在MSDN官網(wǎng)有完整的OpenXML教程,雖然是全英文的不過還是很有幫助的。

注,原來摘抄代碼里面沒有模板,在copy過來發(fā)現(xiàn)插入word中的圖片大小不一樣,我們?nèi)绾尾檎以O(shè)置圖片大小帶代碼的那一塊,建議自己用在word里面插入一張圖片,通過OpenXml Tools 反編譯出C#代碼,然后改變圖片的大小,再次反編譯。

使用byeond compare 【http://www.scootersoftware.com/ 下載地址】比較C#代碼,就會(huì)發(fā)現(xiàn)是因?yàn)閚ew DW.Extent() { Cx = 990000L, Cy = 792000L}  是因?yàn)檫@段設(shè)置造成的。以后其實(shí)很多地方都可以借助OpenXml Tools反編譯來進(jìn)行對比。查看設(shè)置樣式的屬性位置。

MSDN openxml學(xué)習(xí)鏈接:  http://msdn.microsoft.com/en-us/library/office/bb491088(v=office.15).aspx。感興趣的朋友可以查看一下。

示例代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO;
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using PIC = DocumentFormat.OpenXml.Drawing.Pictures;
using A = DocumentFormat.OpenXml.Drawing;

namespace AddPictureIntoWord
{
  public class Program
  {
    public static void Main(string[] args)
    {
      string picPath = "u=639047729,3872612606&fm=11&gp=0.bmp";
      string filePath = "Test.docx";
      AddPictureIntoWord(filePath, picPath);
    }

    public static void AddPictureIntoWord(string filePath, string picturePath)
    {
      using (WordprocessingDocument doc = WordprocessingDocument.Open(filePath, true))
      {
        string picType = picturePath.Split('.').Last();
        ImagePartType imagePartType;
        ImagePart imagePart = null;
        // 通過后綴名判斷圖片類型, true 表示忽視大小寫
        if (Enum.TryParse<ImagePartType>(picType, true, out imagePartType))
        {
          imagePart = doc.MainDocumentPart.AddImagePart(imagePartType);
        }

        imagePart.FeedData(File.Open(picturePath, FileMode.Open)); // 讀取圖片二進(jìn)制流
        AddImageToBody(doc, doc.MainDocumentPart.GetIdOfPart(imagePart));
      }
    }

    // 摘抄自http://msdn.microsoft.com/EN-US/library/office/bb497430(v=office.15).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-5    
   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 }, // 調(diào)節(jié)圖片大小
             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 }), //與上面的對準(zhǔn)
                     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)));
    }
  }
}

本文示例運(yùn)行效果如下圖所示:

希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論

铜川市| 天长市| 广西| 景泰县| 上思县| 尉氏县| 安溪县| 安陆市| 济源市| 上思县| 扶绥县| 柳州市| 信宜市| 额尔古纳市| 米脂县| 汕尾市| 扬中市| 平利县| 西乌珠穆沁旗| 怀化市| 朔州市| 西昌市| 林周县| 凤阳县| 永丰县| 拜城县| 托克逊县| 卫辉市| 丰都县| 吉木乃县| 德化县| 安化县| 连江县| 营山县| 庆阳市| 长白| 新河县| 伊宁市| 纳雍县| 江陵县| 瑞金市|