EvotecIT / OfficeIMO

Fast and easy to use cross-platform .NET library that creates or modifies Microsoft Word (DocX) and later also Excel (XLSX) files without installing any software. Library is based on Open XML SDK
MIT License
286 stars 50 forks source link

Word table cell paragraph add image fix #176

Closed tmheath closed 9 months ago

tmheath commented 9 months ago

This pull request contains an untested fix for enabling a paragraph created by WordTableCell to add an image automatically by adding the argument for the document reference to the called constructed for WordParagraph to pass the WordTableCell's _document attribute.

closes #174

PrzemyslawKlys commented 9 months ago

I've modified your PR a bit. It seems to work fine. Keep in mind it's not necessary to add paragraph in empty table, as it already has at least one paragraph present.

internal static void Example_AddingImagesSampleToTable(string folderPath, bool openWord) {
    Console.WriteLine("[*] Creating standard document with some Images and Samples");
    var filePath = System.IO.Path.Combine(folderPath, "BasicDocumentWithImagesSample4.docx");
    var imagePaths = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "Images");

    using var document = WordDocument.Create(filePath);

    var table = document.AddTable(2, 2);
    table.Rows[0].Cells[0].Paragraphs[0].AddImage(System.IO.Path.Combine(imagePaths, "PrzemyslawKlysAndKulkozaurr.jpg"), 200, 200);

    // not really nessessary to add new paragraph since one is already there by default
    var paragraph = table.Rows[0].Cells[1].AddParagraph();
    paragraph.AddImage(System.IO.Path.Combine(imagePaths, "PrzemyslawKlysAndKulkozaurr.jpg"), 200, 200);

    document.AddHeadersAndFooters();

    var tableInHeader = document.Header.Default.AddTable(2, 2);
    tableInHeader.Rows[0].Cells[0].Paragraphs[0].AddImage(System.IO.Path.Combine(imagePaths, "PrzemyslawKlysAndKulkozaurr.jpg"), 200, 200);

    // not really nessessary to add new paragraph since one is already there by default
    var paragraphInHeader = tableInHeader.Rows[0].Cells[1].AddParagraph();
    paragraphInHeader.AddImage(System.IO.Path.Combine(imagePaths, "PrzemyslawKlysAndKulkozaurr.jpg"), 200, 200);

    document.Save(openWord);
}
tmheath commented 9 months ago

Thanks