// Open the main document part for editing MainDocumentPart mainPart = wordDoc.MainDocumentPart; // Create a new alternative format import part for the PDF file AlternativeFormatImportPart pdfPart = mainPart.AddAlternativeFormatImportPart( AlternativeFormatImportPartType.Pdf); // Read the PDF file and write its contents to the alternative format import part using (FileStream pdfFile = new FileStream("document.pdf", FileMode.Open)) { pdfPart.FeedData(pdfFile); } // Create a new paragraph in the document body and add a reference to the alternative format import part Paragraph para = new Paragraph(); mainPart.Document.Body.Append(para); AltChunk altChunk = new AltChunk(); altChunk.Id = mainPart.GetIdOfPart(pdfPart); para.Append(altChunk);
// Open the main document part for editing MainDocumentPart mainPart = wordDoc.MainDocumentPart; // Create a new alternative format import part for the image file AlternativeFormatImportPart imagePart = mainPart.AddAlternativeFormatImportPart( AlternativeFormatImportPartType.Png); // Read the image file and write its contents to the alternative format import part using (FileStream imageFile = new FileStream("image.png", FileMode.Open)) { imagePart.FeedData(imageFile); } // Create a new paragraph in the document body and add a reference to the alternative format import part Paragraph para = new Paragraph(); mainPart.Document.Body.Append(para); Drawing drawing = new Drawing(); para.Append(drawing); Inline inline = new Inline(); drawing.Append(inline); Graphic graphic = new Graphic(); inline.Append(graphic); graphic.GraphicData = new GraphicData(); graphic.GraphicData.Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"; Picture picture = new Picture(); graphic.GraphicData.Append(picture); picture.NonVisualPictureProperties = new NonVisualPictureProperties(); picture.NonVisualPictureProperties.Append(new NonVisualDrawingProperties() { Id = 0, Name = "image.png" }); picture.NonVisualPictureProperties.Append(new NonVisualPictureDrawingProperties()); picture.NonVisualPictureProperties.Append(new ApplicationNonVisualDrawingProperties()); BlipFill blipFill = new BlipFill(); picture.Append(blipFill); blipFill.Blip = new DocumentFormat.OpenXml.Drawing.Blip() { Embed = mainPart.GetIdOfPart(imagePart), CompressionState = DocumentFormat.OpenXml.Drawing.BlipCompressionValues.Print }; blipFill.Append(new Stretch(new FillRectangle()));This example adds a PNG image file named "image.png" as an alternative format import part to the main document part. It then creates a new paragraph in the document body and adds a reference to the alternative format import part using a Drawing element that contains a GraphicData element and a Picture element. The BlipFill element within the Picture element references the alternative format import part. Package library: DocumentFormat.OpenXml