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
This PR implements embedding files of a particular type displayed as regular content in Word.
[x] embedding of HTML document into Word document.AddEmbeddedDocument(file);
[x] embedding of RTF document into Word document.AddEmbeddedDocument(file);
[x] embedding of TEXT document into Word document.AddEmbeddedDocument(file);
[x] Find embedded documents in Word, document.EmbeddedDocuments list
[x] Save embedded document back to file document.EmbeddedDocuments[0].Save("C:\\TEMP\\EmbeddedFileHTML.html");
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);
}
}
This PR implements embedding files of a particular type displayed as regular content in Word.
document.AddEmbeddedDocument(file);
document.AddEmbeddedDocument(file);
document.AddEmbeddedDocument(file);
document.EmbeddedDocuments
listdocument.EmbeddedDocuments[0].Save("C:\\TEMP\\EmbeddedFileHTML.html");
Example 1:
Example for saving: