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
279 stars 49 forks source link

Basic support for embedding RTF & HTML & More (AddEmbeddedDocument) #104

Closed PrzemyslawKlys closed 1 year ago

PrzemyslawKlys commented 1 year ago

This PR implements embedding files of a particular type displayed as regular content in Word.

Example 1:

public static void Example_EmbedFileMultiple(string folderPath, string templateFolder, bool openWord) {
    Console.WriteLine("[*] Creating standard document with multiple files embedded");
    string filePath = System.IO.Path.Combine(folderPath, "MultipleFilesEmedded.docx");

    string htmlFilePath = System.IO.Path.Combine(templateFolder, "SampleFileHTML.html");
    string rtfFilePath = System.IO.Path.Combine(templateFolder, "SampleFileRTF.rtf");
    string txtFilePath = System.IO.Path.Combine(templateFolder, "SampleFileTEXT.txt");

    using (WordDocument document = WordDocument.Create(filePath)) {
        document.AddParagraph("Add RTF document in front of the document");
        // we natively support HTML by using extension to decide what it is
        document.AddEmbeddedDocument(rtfFilePath);

        document.AddPageBreak();

        document.AddParagraph("Add HTML");

        // we natively support HTML by using extension to decide what it is
        document.AddEmbeddedDocument(htmlFilePath);

        document.AddPageBreak();

        document.AddParagraph("Add TEXT 1");

        // we natively support TextPlain by using extension (log/txt) to apply text/plain content type
        document.AddEmbeddedDocument(txtFilePath);

        document.AddPageBreak();

        document.AddParagraph("Add TEXT 2");

        // but you can specify type as you want, if the extension would be .ext, and the content type would be text/plain
        document.AddEmbeddedDocument(txtFilePath, AlternativeFormatImportPartType.TextPlain);

        Console.WriteLine("Embedded documents in word: " + document.EmbeddedDocuments.Count);
        Console.WriteLine("Embedded documents in Section 0: " + document.Sections[0].EmbeddedDocuments.Count);
        Console.WriteLine("Content type 0: " + document.EmbeddedDocuments[0].ContentType);
        Console.WriteLine("Content type 1: " + document.EmbeddedDocuments[1].ContentType);

        document.AddPageBreak();

        document.AddParagraph("Add RTF 2");

        document.AddEmbeddedDocument(rtfFilePath);

        Console.WriteLine("Content type 0: " + document.EmbeddedDocuments[0].ContentType);
        Console.WriteLine("Content type 1: " + document.EmbeddedDocuments[1].ContentType);
        Console.WriteLine("Content type 2: " + document.EmbeddedDocuments[2].ContentType);
        Console.WriteLine("Content type 3: " + document.EmbeddedDocuments[3].ContentType);
        Console.WriteLine("Content type 4: " + document.EmbeddedDocuments[4].ContentType);

        Console.WriteLine("Embedded documents in word: " + document.EmbeddedDocuments.Count);

        document.Save(openWord);
    }
}

Example for saving:

public static void Example_EmbedFileHTML(string folderPath, string templateFolder, bool openWord) {
    Console.WriteLine("[*] Creating standard document with embedded HTML file");
    string filePath = System.IO.Path.Combine(folderPath, "EmbeddedFileHTML.docx");
    string htmlFilePath = System.IO.Path.Combine(templateFolder, "SampleFileHTML.html");
    using (WordDocument document = WordDocument.Create(filePath)) {
        Console.WriteLine("Embedded documents in word: " + document.EmbeddedDocuments.Count);
        Console.WriteLine("Embedded documents in Section 0: " + document.Sections[0].EmbeddedDocuments.Count);

        document.AddParagraph("Add HTML document in DOCX");

        document.AddSection();

        Console.WriteLine("Embedded documents in Section 1: " + document.Sections[1].EmbeddedDocuments.Count);

        document.AddEmbeddedDocument(htmlFilePath);

        document.EmbeddedDocuments[0].Save("C:\\TEMP\\EmbeddedFileHTML.html");

        Console.WriteLine("Embedded documents in word: " + document.EmbeddedDocuments.Count);
        Console.WriteLine("Embedded documents in Section 0: " + document.Sections[0].EmbeddedDocuments.Count);
        Console.WriteLine("Embedded documents in Section 1: " + document.Sections[1].EmbeddedDocuments.Count);
        Console.WriteLine("Content type: " + document.EmbeddedDocuments[0].ContentType);

        document.Save(openWord);
    }
}