文檔半島外圍網上直營>>Aspose中文文檔>>將圖像插入 Word 文檔
                將圖像插入 Word 文檔
Aspose.Words是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式處理,并允許將各類文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
使用 Aspose.WordsDocumentBuilder是一個與文檔關聯的功能強大的類,允許從頭開始構建動態文檔或向現有文檔添加新元素。它提供了插入文本、段落、列表、表格、圖像和其他內容的方法,以及字體、段落、部分格式和其他內容的規范。
DocumentBuilder 補充了 Aspose.Words 文檔對象模型 (DOM) 中可用的類和方法,以簡化最常見的文檔構建任務,例如插入文本、表格、字段和超鏈接。
以下代碼示例演示如何將圖像插入到文檔中的指定位置和尺寸:
// For complete examples and data files, please go to //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);
點擊復制
使用 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;
點擊復制
	
以下代碼示例演示如何使用主函數InsertAPicture和子函數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 = "http://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)));
}
	點擊復制
	

 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網安備
            50010702500608號
渝公網安備
            50010702500608號
             
            
 客服熱線
客服熱線