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
280 stars 50 forks source link

Add images to headers/footers/tables and other image improvements #53

Closed PrzemyslawKlys closed 1 year ago

PrzemyslawKlys commented 1 year ago

This should resolve:

And it should improve a bit:

using (WordDocument document = WordDocument.Create(filePath)) {
    document.BuiltinDocumentProperties.Title = "This is sparta";
    document.BuiltinDocumentProperties.Creator = "Przemek";
    var filePathImage = System.IO.Path.Combine(imagePaths, "Kulek.jpg");

    document.AddHeadersAndFooters();

    var header = document.Header.Default;
    var paragraphHeader = header.AddParagraph("This is header");

    // add image to header, directly to paragraph
    header.AddParagraph().AddImage(filePathImage, 100, 100);

    // add image to footer, directly to paragraph
    document.Footer.Default.AddParagraph().AddImage(filePathImage, 100, 100);

    // add image to header, but to a table
    var table = header.AddTable(2, 2);
    table.Rows[1].Cells[1].Paragraphs[0].Text = "Test123";
    table.Rows[1].Cells[0].Paragraphs[0].AddImage(filePathImage, 50, 50);
    table.Alignment = TableRowAlignmentValues.Right;

    var paragraph = document.AddParagraph("This paragraph starts with some text");
    paragraph.Text = "0th This paragraph started with some other text and was overwritten and made bold.";
    paragraph.Bold = true;

    // add table with an image, but to document
    var table1 = document.AddTable(2, 2);
    table1.Rows[1].Cells[1].Paragraphs[0].Text = "Test - In document";
    table1.Rows[1].Cells[0].Paragraphs[0].AddImage(filePathImage, 50, 50);

    // lets add image to paragraph
    paragraph.AddImage(System.IO.Path.Combine(imagePaths, "PrzemyslawKlysAndKulkozaurr.jpg"), 22, 22);
}
internal static void Example_AddingImagesInline(string folderPath, bool openWord) {
    Console.WriteLine("[*] Creating standard document with inline images");
    string filePath = System.IO.Path.Combine(folderPath, "DocumentWithInlineImages2.docx");
    string imagePaths = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "Images");

    using (WordDocument document = WordDocument.Create(filePath)) {
        var file = System.IO.Path.Combine(imagePaths, "PrzemyslawKlysAndKulkozaurr.jpg");
        var paragraph = document.AddParagraph();
        var pargraphWithImage = paragraph.AddImage(file, 100, 100);

        // Console.WriteLine("Image is inline: " + pargraphWithImage.Image.Rotation);

        pargraphWithImage.Image.VerticalFlip = false;
        pargraphWithImage.Image.HorizontalFlip = false;
        pargraphWithImage.Image.Rotation = 270;
        pargraphWithImage.Image.Shape = ShapeTypeValues.Cloud;

        document.Save(openWord);
    }
}
internal static void Example_AddingImagesSample4(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 paragraph1 = document.AddParagraph("This paragraph starts with some text");
    paragraph1.AddImage(System.IO.Path.Combine(imagePaths, "PrzemyslawKlysAndKulkozaurr.jpg"), 200, 200);
    paragraph1.Image.Shape = ShapeTypeValues.Cube;

    var paragraph2 = document.AddParagraph("Image will be placed behind text");
    paragraph2.AddImage(System.IO.Path.Combine(imagePaths, "PrzemyslawKlysAndKulkozaurr.jpg"), 200, 200, WrapImageText.BehindText, "Przemek and Kulek on an image");

    var paragraph3 = document.AddParagraph("Image will be in front of text");
    paragraph3.AddImage(System.IO.Path.Combine(imagePaths, "PrzemyslawKlysAndKulkozaurr.jpg"), 200, 200, WrapImageText.InFrontText, "Przemek and Kulek on an image");

    var paragraph5 = document.AddParagraph("Image will be Square");
    paragraph5.AddImage(System.IO.Path.Combine(imagePaths, "PrzemyslawKlysAndKulkozaurr.jpg"), 200, 200, WrapImageText.Square, "Przemek and Kulek on an image");

    var paragraph6 = document.AddParagraph("Image will be Through");
    paragraph6.AddImage(System.IO.Path.Combine(imagePaths, "PrzemyslawKlysAndKulkozaurr.jpg"), 200, 200, WrapImageText.Through, "Przemek and Kulek on an image");

    var paragraph7 = document.AddParagraph("Image will be Tight");
    paragraph7.AddImage(System.IO.Path.Combine(imagePaths, "PrzemyslawKlysAndKulkozaurr.jpg"), 200, 200, WrapImageText.Tight, "Przemek and Kulek on an image");

    var paragraph8 = document.AddParagraph("Image will be Top And Bottom");
    paragraph8.AddImage(System.IO.Path.Combine(imagePaths, "PrzemyslawKlysAndKulkozaurr.jpg"), 200, 200, WrapImageText.TopAndBottom, "Przemek and Kulek on an image");
    paragraph8.Image.Shape = ShapeTypeValues.Can;

    document.Save(openWord);
}