public static string AddImage([NotNull] this WordprocessingDocument doc, string imageFile) { using (FileStream stream = new FileStream(imageFile, FileMode.Open)) { return(doc.AddImage(stream)); } }
private static void ProcessWord() { var file = @"test.docx"; using (WordprocessingDocument doc = WordprocessingDocument.Open(file, true)) { var body = doc.MainDocumentPart.Document.Body; if (doc.HasStyleId("2")) { Console.WriteLine("has styleId 2"); } if (doc.HasStyleId("3")) { Console.WriteLine("has styleId 3"); } var styleNames = doc.MainDocumentPart.StyleDefinitionsPart.Styles.Descendants <StyleName>(); if (styleNames.Any()) { } var table = doc.MainDocumentPart.Document.Body.Elements <Table>().First(); TableRow row = table.Elements <TableRow>().ElementAt(0); TableCell cell = row.Elements <TableCell>().ElementAt(0); var pic = cell.Descendants <PIC.Picture>().FirstOrDefault(); if (pic != null) { var imageFile = @"tmp.jpg"; var relationshipId = doc.AddImage(imageFile); pic.BlipFill.Blip.Embed.Value = relationshipId; } } }
public void AddImage() { WordprocessingDocument word = WordProcessingExtensions.WordDocument(); using (FileStream fs = File.OpenRead($@"{AppDomain.CurrentDomain.BaseDirectory}\Extensions\Word\Templates\Example.JPG")) { Assert.True(word.AddImage(fs)); word.SaveAs($@"{AppDomain.CurrentDomain.BaseDirectory}\TestResults\Extensions\Word\ImageDocument.docx"); } }