Code Monkey home page Code Monkey logo

Comments (4)

PrzemyslawKlys avatar PrzemyslawKlys commented on June 24, 2024

Only directly as HTML file. I guess it would be possible to add a way to convert HTML directly without being in a file, but so far not available.

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);
    }
}

One would need to add a method that skips the first few lines and goes straight for content.

public WordEmbeddedDocument(WordDocument wordDocument, string fileName, AlternativeFormatImportPartType? alternativeFormatImportPartType) {
AlternativeFormatImportPartType partType;
if (alternativeFormatImportPartType == null) {
FileInfo fileInfo = new FileInfo(fileName);
if (fileInfo.Extension == ".rtf") {
partType = AlternativeFormatImportPartType.Rtf;
} else if (fileInfo.Extension == ".html") {
partType = AlternativeFormatImportPartType.Html;
} else if (fileInfo.Extension == ".log" || fileInfo.Extension == ".txt") {
partType = AlternativeFormatImportPartType.TextPlain;
} else {
throw new Exception("Only RTF and HTML files are supported for now :-)");
}
} else {
partType = alternativeFormatImportPartType.Value;
}
AltChunk altChunk = new AltChunk {
Id = GetAltChunkId(wordDocument)
};
MainDocumentPart mainDocPart = wordDocument._document.MainDocumentPart;
AlternativeFormatImportPart chunk = mainDocPart.AddAlternativeFormatImportPart(partType, altChunk.Id);
var documentContent = File.ReadAllText(fileName, Encoding.ASCII);
using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(documentContent))) {
chunk.FeedData(ms);
}
_id = altChunk.Id;
_altChunk = altChunk;
_document = wordDocument;
mainDocPart.Document.Body.Append(altChunk);
mainDocPart.Document.Save();
}
}

from officeimo.

derek-price avatar derek-price commented on June 24, 2024

This is amazing - thanks for the fast reply! Let me give it a go and see how dangerous I can get. 😄

from officeimo.

derek-price avatar derek-price commented on June 24, 2024

Just wanted to let you know that your solution worked perfectly. Thanks again!

from officeimo.

PrzemyslawKlys avatar PrzemyslawKlys commented on June 24, 2024

I think i will add ability to add fragments directly, I guess you created file and added file which was then created as a fragment. We should have direct ability as well.

from officeimo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.