Quantcast
Channel: html2openxml Discussions Rss Feed
Viewing all 228 articles
Browse latest View live

New Post: Adding image as inline in word document

$
0
0
Dear All

I need an urgent information. I am trying to add the image in document using this tool. I'm using the same example on documentation by using .NET framwork 3.5.

I am unable to view the image; as there is only text.

Please help me.

Below is the code.

#region Open file and add Data
                            using (WordprocessingDocument doc = WordprocessingDocument.Open(mem, true))
                            {
                                MainDocumentPart mainPart = doc.MainDocumentPart;
                                if (mainPart == null)
                                {
                                    mainPart = doc.AddMainDocumentPart();
                                    new Document(new Body()).Save(mainPart);
                                }

                                HtmlConverter converter = new HtmlConverter(mainPart);


                                Body body = mainPart.Document.Body;


                                StringWriter sw = new StringWriter();

                                HtmlTextWriter hw = new HtmlTextWriter(sw);

                                //DivExport.RenderControl(hw);

                                string strHTML = string.Empty;
                                strHTML = strHTML + string.Format("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>");
                                strHTML = strHTML + string.Format("<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'><head><title></title></head><body>");

                                //strHTML = strHTML + sw.ToString();
                                strHTML = strHTML + string.Format("Looks how cool is <font size='x-large'><b>Open Xml</b></font>.");
                                strHTML = strHTML + string.Format(@"<img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' alt='Red dot' />");
                                strHTML = strHTML + string.Format(@"<img src='data:image/JPG;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMj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uDW9UFxFG9qgDfeNPk1XU0lZUtw2ehPSly1U+geziVdRtXuPD0cflyH6DmsGO31Kw0zzlRkKvhR3YV0baxrKJk2iM27oPSnQ6rezO6XlomzGYz70o06kI9GvUcopu5z13a6uJbfYJHDjc3HAzVZ7DU4YZ0iil3k5PFdZPqmpi3j2JFHJnnd0xSPq2ox20beWjSk8gdDTUallsDpo45rTVZIxmObAHHy02Kz1FXIeCVgR3Wuul1vWBKPLsk2nsetamk3l5ds4u4kTHTbUVY1FBt2sOMEncpWMUkGhwpKu1t3T8KK0tS+4v+9/SiuBmpPZ/wCqH+6Ks1Ws/wDVD/dFWaa2AKKKKYCUUtFAESxYlZ85yOlZF3oMlxdPKl0UVuwrcorSFSUHeIrI5s+GZvMLJfuM+9H/AAjEoiZVvnDHoc9K6SitfrdXuHKjn5/DstzFGJLxhIgxuXvTJPDUrxqn26Qbe4NdHRSWKqrZhyowotBniZj9tYhl2kGkl8PPLCkbXTYQ5BzW9RS+s1L3uHKjnh4aY2ywS3TuqsSDnmnv4fc26wpclQnQ963qKf1qr3DlRzMnheaRw51CQMPQ1p6XpR05mJnaTcO5rTopTxNSceVvQLIz9S+4v+9/SijUvuL/AL39KK5mMns/9UP90VZqtZ/6of7oqzTWwBRRRTAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAM7U/uL/vf0oo1P7i/wC9/Sipe4Fi0x5Y5/hFWMj1rmtSu7iGWMRyFRsHSqX9o3n/AD3ancDssj1oyPWuN/tG8/57tR/aN5/z3ai4HZZHrRketcb/AGjef892o/tG8/57tRcDssj1oyPWuN/tG8/57tR/aN5/z3ai4HZZHrRketcb/aN5/wA92o/tG8/57tRcDssj1oyPWuN/tG8/57tR/aN5/wA92ouB2WR60ZHrXG/2jef892o/tG8/57tRcDssj1oyPWuN/tG8/wCe7Uf2jef892ouB2WR60ZHrXG/2jef892o/tG8/wCe7UXA6LUuUXB/i/pRWdYzy3Fqxlcv83f6UUgP/9k=' alt='Red dot' ></img>");
                                strHTML = strHTML + string.Format("Now with <font color='red'><u>HtmlToOpenXml</u></font>, it nevers been so easy to convert html.");
                                //strHTML = strHTML + string.Format("<p>If you like it, add me a rating on <a href='http://notesforhtml2openxml.codeplex.com'>codeplex</a>");
                               // strHTML = strHTML + string.Format("</p>");
                                strHTML = strHTML + string.Format("</body></html>");


                                var paragraphs = converter.Parse(strHTML);
                                for (int i = 0; i < paragraphs.Count; i++)
                                {
                                    body.Append(paragraphs[i]);
                                }


                                Debug.Assert(IsDocumentValid(doc), "Invalid File!");
                                mainPart.Document.Save();





                                /*Allow File to Download*/
                                Response.Clear();

                                Response.Buffer = true;



                                Response.AddHeader("content-disposition", "attachment;filename=" + docName);

                                Response.Charset = "";

                               Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                                Response.ContentType = "application/ms-word";
                               byte[] mArray = mem.ToArray();
                                Response.BinaryWrite(mArray);

                                Response.Flush();

                                Response.End();

                            }
                            #endregion

New Post: Adding image as inline in word document

$
0
0
Hello,


I notice 2 things on your code:
1) the first image contains a break-line in your inline data. Perhaps it's due to Codeplex forum but as-is, it does not work.
2) image/jpg is not a standardized content type. It should be image/jp__e__g.

I just have committed a change to include also image/jpg (it doesn't hurt to be supported...) and fix an optimisation I found while debugging your code.
You can download the latest source code and compile the lib.

Thank you,

Olivier

New Post: Adding image as inline in word document

$
0
0

Olivier,

On another matter, have you been able to try google viewer with any document produced by the library? As I have noted, even the simplest document fails to be displayed until saved within word.

Chris

ps. êtes-vous français? A+

New Post: Adding image as inline in word document

$
0
0
I have check with image/jp_e_g. Its looking still the same.

Do you have any other solution?

BTW thanks both of you for replying fast :-)

New Post: Adding image as inline in word document

$
0
0
Hi Chris,

Yes I speak French ; I'm Belgian :-)


It's hard to explain why Google Viewer failed to load the docx.
Even when modifying an existing one (not creating from scratch) fails.

Do you dare asking the event log to Google? :-)

New Post: Adding image as inline in word document

$
0
0
sorry but it's "image/jpeg"

codeplex "BOLD" button replace the e with e but it's seem to not working...

edit: and now it works.. Damn!

New Post: Adding image as inline in word document

$
0
0

Excellent. Je suis canadien.

Ce qui est intéressant, c'est si le document est sauvegarde par Word après, tout fonctionne à merveille.

Auriez-vous essaye le 'validateur' d'openXml sur les fichiers crées par la librairie? Il me semble que le probleme est la.

De plus, crocodocs, un produit semblable à Google (mais qui n'est pas gratuit!), parvient à présenter les documents. Sans doute la validation y est différente .

Chris

New Post: Adding image as inline in word document

$
0
0
Oui j'ai ajouté l'étape de validation dans mon process de testing.
Tout n'est pas 100% parfait sur tout mais j'y travaille...

J'ai pris la validation Office 2010. Peut-être Google utilise la version 2013 ?
De version en version, cela devient plus restrictif.

J'ai ajouté ce bloc de code-ci:
staticvoid AssertThatOpenXmlDocumentIsValid(WordprocessingDocument wpDoc)
{
       var validator = new OpenXmlValidator(FileFormatVersions.Office2010);
       var errors = validator.Validate(wpDoc);

       if (!errors.GetEnumerator().MoveNext())
              return;

       Console.ForegroundColor = ConsoleColor.Red;
       Console.WriteLine("The document doesn't look 100% compatible with Office 2010.\n");

       Console.ForegroundColor = ConsoleColor.Gray;
       foreach (ValidationErrorInfo error in errors)
       {
              Console.Write("{0}\n\t{1}", error.Path.XPath, error.Description);
              Console.WriteLine();
       }

       Console.ReadLine();
}

New Post: Thread Safe

$
0
0
I have one question. This library HtmlToOpenXml is thread safe? i mean, if 3,4,5 users try to do press same button that convert html body to xml, it will handle this?

New Post: Thread Safe

$
0
0
Yes it is but each thread should use its own instance of HtmlConverter.
By the way, if you write the generated docx on file system, it will failed due to concurrency but if you work on document on-the-fly (= in memory) and returns them in the response stream, it will be okay.

New Post: Change Orientation to landscape

$
0
0
Hello,
First off, thanks for this wonderful library. It has helped me a bunch. I was curious if anything like the above has been implemented yet. I have a requirement that certain pages with large images need to be landscape oriented, while other pages with text need to be portrait. Does there yet exist a way to switch pages to different orientations (not the entire document). Many thanks!

New Post: Style not used to convert

$
0
0
Hi,
I am new to this library and OpenXML. I am using it to convert html document which defines the css styles within the html. During convertion to Word the styles are not applied. Is this possible with this library? I am using the demo program from documentation.

Thanks!

New Post: Style not used to convert

$
0
0
Hi,


CSS is partially supported. Only inline css (ie for example: <span style="font-family: Verdana; font-size: 12px"/>), not <style></style>
You can have a more detailled information on supported style in the documentation.

New Post: Change Orientation to landscape

$
0
0
Hello,


Sorry for late reply. Yes something has been implemented. You can declare your body as follow:
<bodystyle="page-orientation: landscape"></body>
to turn your page in landscape. Unlickily, currently it's available only on <body> and not yet on <div>.

New Post: converter parse img returns nothing

$
0
0
this is my code:

Document document = new Document() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } };
            DocumentNamespace(document);
            MainPart.Document = document;

            Body body = new Body();
            document.Append(body);
            HtmlConverter converter = new HtmlConverter(MainPart);
            //converter.ImageProcessing = ImageProcessing.ManualProvisioning;
            //converter.ProvisionImage += converter_ProvisionImage;
            foreach (ParagraphObject paragraphObject in paragraphs)
            {
                var htmlParagraphs = converter.Parse(paragraphObject.TitleAfterParsing);
                foreach (Paragraph paragraph in htmlParagraphs)
                    body.Append(GenerateCustomParagraph(paragraphObject, paragraph, true));

                switch (paragraphObject.Type)
                {
                    case ParagraphObject.ParagraphType.Text:
                        htmlParagraphs = converter.Parse(paragraphObject.ContentAfterParsing);
                        for (int i = 0; i < htmlParagraphs.Count; i++)
                        {
[...]
when i parse html tag img with a src being an valid url it return no htmlParagraps
i use htmlTOopenXML 1.4.1

New Post: converter parse img returns nothing

$
0
0
Can you show me what's in "paragraphObject" ?
In fact, what is the HTML you are parsing?

New Post: converter parse img returns nothing

New Post: converter parse img returns nothing

New Post: converter parse img returns nothing

$
0
0
Ok tried a different solution, becouse it turns out when i use a external web url it works fine. I decided to try with this solution:
private void converter_ProvisionImage(object sender, ProvisionImageEventArgs e)
{
   // Read the image from the file system:
    e.Data = File.ReadAllBytes(@"c:\inetpub\wwwroot\mysite\images\" + e.ImageUrl);
}
but even thoudh the e.Data fills with correct byte array the converter does not generate nothing.

New Post: Rowspan is not working as expected

$
0
0
I have a table in html page which has many rowspan , colspan in it. The table structure is give below:
<table align="center" border="1">
    <tbody>
        <tr>
            <td align="center" style="background-color: rgb(135,206,235);" colspan="2"><strong><span style="font-size: 13px; font-family: tahoma;">Change Type</span></strong></td>
            <td align="center" style="background-color: rgb(135,206,235);"><strong><span style="font-size: 13px; font-family: tahoma;">Change Risk</span></strong></td>
            <td align="center" style="background-color: rgb(135,206,235);"><strong><span style="font-size: 13px; font-family: tahoma;">Approval Body</span></strong></td>
        </tr>
        <tr>
            <td rowspan="4"><strong><span style="font-size: 13px; font-family: tahoma;">Emergency</span></strong></td>
            <td rowspan="4"><strong><span style="font-size: 13px; font-family: tahoma;">Normal</span></strong></td>
            <td align="center" style="background-color: rgb(255,0,0);"><strong><span style="font-size: 13px; font-family: tahoma;">Level 1</span></strong></td>
            <td align="center"><span style="font-size: 13px; font-family: tahoma;">MS COO</span></td>
        </tr>
        <tr>
            <td align="center" style="background-color: rgb(255,165,0);"><strong><span style="font-size: 13px; font-family: tahoma;">Level 2</span></strong></td>
            <td align="center"><span style="font-size: 13px; font-family: tahoma;">CAB or E-CAB</span></td>
        </tr>
        <tr>
            <td align="center" style="background-color: rgb(255,215,0);"><strong><span style="font-size: 13px; font-family: tahoma;">Level 3</span></strong></td>
            <td align="center"><span style="font-size: 13px; font-family: tahoma;">CAB or E-CAB</span></td>
        </tr>
        <tr>
            <td align="center" style="background-color: rgb(255,255,0);" rowspan="2"><strong><span style="font-size: 13px; font-family: tahoma;">Level 4</span></strong></td>
            <td align="center"><span style="font-size: 13px; font-family: tahoma;">Originator's Manager</span></td>
        </tr>
        <tr>
            <td align="center" colspan="2"><strong><span style="font-size: 13px; font-family: tahoma;">Standard</span></strong></td>
            <td align="center"><span style="font-size: 13px; font-family: tahoma;">Originator's Manager</span></td>
        </tr>
    </tbody>
</table>
When I am trying to convert this html code into Open xml , the table structure is totally messed up. On top of that the color from previous cell is overflowing to next cell.

Can you please help me on fixing it.
I am using htmltoopenxml 1.4 version.
Viewing all 228 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>