• <menu id="w2i4a"></menu>
  • logo Aspose中文文檔

    文檔首頁(yè)>>Aspose中文文檔>>將圖像插入 Word 文檔

    將圖像插入 Word 文檔


    Aspose.Words是一種高級(jí)Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無(wú)需在跨平臺(tái)應(yīng)用程序中直接使用Microsoft Word。

    Aspose API支持流行文件格式處理,并允許將各類(lèi)文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。

    Aspose.Words for .NET 最新下載

    使用 Aspose.Words

    DocumentBuilder是一個(gè)與文檔關(guān)聯(lián)的功能強(qiáng)大的類(lèi),允許從頭開(kāi)始構(gòu)建動(dòng)態(tài)文檔或向現(xiàn)有文檔添加新元素。它提供了插入文本、段落、列表、表格、圖像和其他內(nèi)容的方法,以及字體、段落、部分格式和其他內(nèi)容的規(guī)范。

    DocumentBuilder 補(bǔ)充了 Aspose.Words 文檔對(duì)象模型 (DOM) 中可用的類(lèi)和方法,以簡(jiǎn)化最常見(jiàn)的文檔構(gòu)建任務(wù),例如插入文本、表格、字段和超鏈接。

    以下代碼示例演示如何將圖像插入到文檔中的指定位置和尺寸:

    // For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    
    builder.InsertImage(dataDir + "Watermark.png",
    RelativeHorizontalPosition.Margin,
    100,
    RelativeVerticalPosition.Margin,
    100,
    200,
    100,
    WrapType.Square);
    dataDir = dataDir + "DocumentBuilderInsertFloatingImage_out.doc";
    doc.Save(dataDir);

    點(diǎn)擊復(fù)制

    使用 Open XML SDK

    需要添加的命名空間:


    using System.IO;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    using NUnit.Framework;
    using A = DocumentFormat.OpenXml.Drawing;
    using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
    using Paragraph = DocumentFormat.OpenXml.Wordprocessing.Paragraph;
    using PIC = DocumentFormat.OpenXml.Drawing.Pictures;
    using Run = DocumentFormat.OpenXml.Wordprocessing.Run;

    點(diǎn)擊復(fù)制


    以下代碼示例演示如何使用主函數(shù)InsertAPicture和子函數(shù)AddImageToBody將圖像插入到 Word 文檔的正文部分:

    public static void InsertPictureInWordDocumentFeature()
    {
    using (WordprocessingDocument wordprocessingDocument =
    WordprocessingDocument.Create(ArtifactsDir + "Insert picture - OpenXML.docx",
    WordprocessingDocumentType.Document))
    {
    MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;
    ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
    using (FileStream stream = new FileStream(MyDir + "Aspose.Words.png", FileMode.Open))
    {
    imagePart.FeedData(stream);
    }
    AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart));
    }
    }
    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 = 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 = 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 = "https://schemas.openxmlformats.org/drawingml/2006/picture" })
    )
    {
    DistanceFromTop = 0U,
    DistanceFromBottom = 0U,
    DistanceFromLeft = 0U,
    DistanceFromRight = 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)));
    }
    

    點(diǎn)擊復(fù)制


    您可以從Aspose.Words GitHub 下載此示例的示例文件。
    掃碼咨詢


    添加微信 立即咨詢

    電話咨詢

    客服熱線
    023-68661681

    TOP
    三级成人熟女影院,欧美午夜成人精品视频,亚洲国产成人乱色在线观看,色中色成人论坛 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();