1

I am use Open XML SDK 2.12.1 version to convert docx word to html file

protected void btnDisplay_Click(object sender, EventArgs e)
{
    byte[] byteArray = (byte[])(Session["ByteArray"]);

    if (byteArray != null)
    {
        try
        {
            DirectoryInfo convertedDocsDirectory =
                new DirectoryInfo(Server.MapPath(DocxConvertedToHtmlDirectory));

            if (!convertedDocsDirectory.Exists)
                convertedDocsDirectory.Create();

            Guid g = Guid.NewGuid();

            var htmlFileName = g.ToString() + ".html";

            ConvertToHtml(byteArray, convertedDocsDirectory, htmlFileName);

            Response.Redirect(DocxConvertedToHtmlDirectory + htmlFileName);
        }
        catch (Exception ex)
        {
            lblMessage.Text = "ERROR: " + ex.Message.ToString();
        }
    }
    else
    {
        lblMessage.Text = "You have not specified a file.";
    }
}


public static void ConvertToHtml(byte[] byteArray, DirectoryInfo destDirectory, string htmlFileName)
{
    FileInfo fiHtml = new FileInfo(Path.Combine(destDirectory.FullName, htmlFileName));

    using (MemoryStream memoryStream = new MemoryStream())
    {
        memoryStream.Write(byteArray, 0, byteArray.Length);

        using (WordprocessingDocument wDoc = WordprocessingDocument.Open(memoryStream, true))
        {
            var imageDirectoryFullName =
                fiHtml.FullName.Substring(0, fiHtml.FullName.Length - fiHtml.Extension.Length) + "_files";

            var imageDirectoryRelativeName =
                fiHtml.Name.Substring(0, fiHtml.Name.Length - fiHtml.Extension.Length) + "_files";

            int imageCounter = 0;

            var pageTitle = (string)wDoc
                .CoreFilePropertiesPart
                .GetXDocument()
                .Descendants(DC.title)
                .FirstOrDefault();

            HtmlConverterSettings settings = new HtmlConverterSettings()
            {
                PageTitle = pageTitle,
                FabricateCssClasses = true,
                CssClassPrefix = "pt-",
                RestrictToSupportedLanguages = false,
                RestrictToSupportedNumberingFormats = false,

                ImageHandler = imageInfo =>
                {
                    DirectoryInfo localDirInfo = new DirectoryInfo(imageDirectoryFullName);
                    if (!localDirInfo.Exists)
                        localDirInfo.Create();
                    ++imageCounter;

                    string extension = imageInfo.ContentType.Split('/')[1].ToLower();

                    ImageFormat imageFormat = null;
                    if (extension == "png")
                    {
                        // Convert png to jpeg.
                        extension = "gif";
                        imageFormat = ImageFormat.Gif;
                    }
                    else if (extension == "gif")
                        imageFormat = ImageFormat.Gif;
                    else if (extension == "bmp")
                        imageFormat = ImageFormat.Bmp;
                    else if (extension == "jpeg")
                        imageFormat = ImageFormat.Jpeg;
                    else if (extension == "tiff")
                    {
                        // Convert tiff to gif.
                        extension = "gif";
                        imageFormat = ImageFormat.Gif;
                    }
                    else if (extension == "x-wmf")
                    {
                        extension = "wmf";
                        imageFormat = ImageFormat.Wmf;
                    }

                    // If the image format isn't one that we expect, ignore it,
                    // and don't return markup for the link.
                    if (imageFormat == null)
                        return null;

                    FileInfo imageFileName = new FileInfo(imageDirectoryFullName + "/image" +
                        imageCounter.ToString() + "." + extension);
                    try
                    {
                        imageInfo.Bitmap.Save(imageFileName.FullName, imageFormat);
                    }

                    catch (System.Runtime.InteropServices.ExternalException)
                    {
                        return null;
                    }

                    XElement img = new XElement(Xhtml.img,
                        new XAttribute(NoNamespace.src, imageDirectoryRelativeName + "/" + imageFileName.Name),
                        imageInfo.ImgStyleAttribute,
                        imageInfo.AltText != null ?
                            new XAttribute(NoNamespace.alt, imageInfo.AltText) : null);
                    return img;
                }
            };

            XElement html = HtmlConverter.ConvertToHtml(wDoc, settings);

            var body = html.Descendants(Xhtml.body).First();

            body.AddFirst(
                new XElement(Xhtml.p,
                    new XElement(Xhtml.a, new XAttribute("href", "/Default2.aspx"), "Go back to Upload Page")));

            var htmlString = html.ToString(SaveOptions.DisableFormatting);

            File.WriteAllText(fiHtml.FullName, htmlString, Encoding.UTF8);
        }
    }
}

and use iText (Version 7.1.14) to convert html to pdf using HtmlConverter (html2pdf version 2.0.1)

protected void Page_Load(object sender, EventArgs e)
{
    PdfWriter writer = new PdfWriter(Response.OutputStream);
    PdfDocument pdf = new PdfDocument(writer);
    Document document = new Document(pdf);

    using (MySqlConnection con =
        new MySqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString))
    {
        using (MySqlCommand cmd =
            new MySqlCommand("SELECT `HEAD` FROM `xmltable`;", myConnectionString))
        {
            cmd.Connection.Open();
            cmd.CommandType = CommandType.Text;

            MySqlDataReader reader = cmd.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    Paragraph contents = new Paragraph(reader.GetString("HEAD"))
                    .SetTextAlignment(TextAlignment.JUSTIFIED)
                    .SetFontSize(12);                        

                    nomefile = @"C:\\Desktop\Management_" + Guid.NewGuid() + ".pdf";
                    html = reader.GetString("HEAD").ToString();
                    dest = nomefile.ToString();

                    document.Add(contents);

                    HtmlConverter.ConvertToPdf(html, new FileStream(dest, FileMode.Create));
                }
            }

            reader.Close();
            cmd.Connection.Close();
        }
    }

    document.Close();

    Response.Clear();
    Response.ContentType = "application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + dest);
    Response.TransmitFile(dest);
    Response.End();
}

The PDF in result has no images at all !

The PDF file should be looks like

enter image description here

But the result it's without image because itext7 support base64 image.

I have tried this without success

try
{
    imageInfo.Bitmap.Save(imageFileName.FullName, imageFormat);
    byte[] imageBytes = memoryStream.ToArray();
    string base64String = Convert.ToBase64String(imageBytes);
}

How to do edit my code for convert docx word to html file using support base64 image?

HMTL

This is the HTML code.

To view the image in the pdf file i need this part

<img src="01a5094b-2d44-4399-93b2-2cc2db7ebfc2_files/image1.gif" style="width: 2.875in; height: 0.4583333in" alt="Immagine 9" />

change to

<img alt="Embedded Image" src="data:image/png;base64,iVBORw0...ErkJggg==" />

(note that the base64-encoded image was truncated to fit this page)

Browser view and resulting PDF of a file with a base64-encoded image

Reference

<html xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="UTF-8" /><title></title><meta name="Generator" content="PowerTools for Open XML" /><style>span { white-space: pre-wrap; }
p.pt-Normale {
    margin-bottom: 0;
    font-family: Calibri;
    font-size: 36pt;
    line-height: 108%;
    margin-top: 0;
    margin-left: 0;
    margin-right: 0;
}
span.pt-Carpredefinitoparagrafo {
    font-size: 36pt;
    font-style: normal;
    font-weight: normal;
    margin: 0;
    padding: 0;
}
p.pt-Normale-000000 {
    margin-bottom: 0;
    font-family: MyriadPro-Regular;
    font-size: 10pt;
    line-height: 108%;
    margin-top: 0;
    margin-left: 0;
    margin-right: 0;
}
span.pt-000001 {
    font-size: 10pt;
    font-style: normal;
    font-weight: normal;
    margin: 0;
    padding: 0;
}
span.pt-Carpredefinitoparagrafo-000002 {
    font-family: MyriadPro-Regular;
    font-size: 10pt;
    font-style: normal;
    font-weight: normal;
    margin: 0;
    padding: 0;
}
p.pt-Normale-000003 {
    margin-bottom: 0;
    font-family: 'Arial', 'sans-serif';
    font-size: 10pt;
    line-height: 108%;
    margin-top: 0;
    margin-left: 0;
    margin-right: 0;
}
p.pt-Normale-000004 {
    margin-bottom: 0;
    text-align: center;
    font-family: Tahoma-Bold;
    font-size: 9.5pt;
    line-height: 108%;
    margin-top: 0;
    margin-left: 0;
    margin-right: 0;
}
span.pt-000005 {
    color: #0B0B0B;
    font-size: 9.5pt;
    font-style: normal;
    font-weight: bold;
    margin: 0;
    padding: 0;
}
p.pt-Normale-000006 {
    margin-bottom: 0;
    text-align: center;
    font-family: 'Verdana', 'sans-serif';
    font-size: 16pt;
    line-height: 108%;
    margin-top: 0;
    margin-left: 0;
    margin-right: 0;
}
span.pt-Carpredefinitoparagrafo-000007 {
    color: #0B0B0B;
    font-family: 'Verdana', 'sans-serif';
    font-size: 16pt;
    font-style: normal;
    font-weight: bold;
    margin: 0;
    padding: 0;
}
span.pt-Carpredefinitoparagrafo-000008 {
    color: #0B0B0B;
    font-family: 'Verdana', 'sans-serif';
    font-size: 16pt;
    font-style: normal;
    font-weight: normal;
    margin: 0;
    padding: 0;
}
p.pt-Normale-000009 {
    margin-bottom: 0;
    font-family: 'Verdana', 'sans-serif';
    font-size: 12pt;
    line-height: 108%;
    margin-top: 0;
    margin-left: 0;
    margin-right: 0;
}
span.pt-000010 {
    font-size: 12pt;
    font-style: normal;
    font-weight: normal;
    margin: 0;
    padding: 0;
}
p.pt-Normale-000011 {
    margin-bottom: 0;
    text-align: justify;
    font-family: 'Verdana', 'sans-serif';
    font-size: 10pt;
    line-height: 108%;
    margin-top: 0;
    margin-left: 0;
    margin-right: 0;
}
span.pt-Carpredefinitoparagrafo-000012 {
    color: #0000FF;
    font-family: 'Verdana', 'sans-serif';
    font-size: 12pt;
    font-style: normal;
    font-weight: bold;
    margin: 0;
    padding: 0;
}
span.pt-Carpredefinitoparagrafo-000013 {
    font-family: 'Verdana', 'sans-serif';
    font-size: 10pt;
    font-style: normal;
    font-weight: normal;
    margin: 0;
    padding: 0;
}
span.pt-Carpredefinitoparagrafo-000014 {
    font-family: 'Verdana', 'sans-serif';
    font-size: 10pt;
    font-style: normal;
    font-weight: bold;
    margin: 0;
    padding: 0;
}
span.pt-Carpredefinitoparagrafo-000015 {
    font-family: 'Verdana', 'sans-serif';
    font-size: 10pt;
    text-decoration: underline;
    font-style: italic;
    font-weight: bold;
    margin: 0;
    padding: 0;
}
span.pt-Carpredefinitoparagrafo-000016 {
    color: #FF0000;
    font-family: 'Verdana', 'sans-serif';
    font-size: 10pt;
    text-decoration: underline;
    font-style: italic;
    font-weight: bold;
    margin: 0;
    padding: 0;
}
span.pt-Carpredefinitoparagrafo-000017 {
    font-family: 'Verdana', 'sans-serif';
    font-size: 10pt;
    font-style: italic;
    font-weight: normal;
    margin: 0;
    padding: 0;
}
span.pt-Carpredefinitoparagrafo-000018 {
    font-family: 'Verdana', 'sans-serif';
    font-size: 10pt;
    text-decoration: underline;
    font-style: normal;
    font-weight: normal;
    margin: 0;
    padding: 0;
}
span.pt-Carpredefinitoparagrafo-000019 {
    color: #FF0000;
    font-family: 'Verdana', 'sans-serif';
    font-size: 10pt;
    font-style: normal;
    font-weight: normal;
    margin: 0;
    padding: 0;
}
span.pt-000020 {
    color: #FF0000;
    font-size: 10pt;
    font-style: normal;
    font-weight: bold;
    margin: 0;
    padding: 0;
}
span.pt-000021 {
    font-size: 10pt;
    font-style: normal;
    font-weight: bold;
    margin: 0;
    padding: 0;
}
span.pt-Carpredefinitoparagrafo-000022 {
    font-size: 10pt;
    font-style: normal;
    font-weight: normal;
    margin: 0;
    padding: 0;
}
p.pt-000023 {
    margin-bottom: 0;
    margin-left: 0.50in;
    text-indent: -0.25in;
    text-align: justify;
    font-family: 'Verdana', 'sans-serif';
    font-size: 10pt;
    line-height: 108%;
    margin-top: 0;
    margin-right: 0;
}
span.pt-000024 {
    font-family: Wingdings;
    font-size: 10pt;
    font-style: normal;
    font-weight: normal;
    margin: 0;
    padding: 0;
    display: inline-block;
    text-indent: 0;
    width: 0.250in;
}
span.pt-000025 {
    font-family: 'Verdana', 'sans-serif';
    font-size: 10pt;
    font-style: normal;
    font-weight: normal;
    margin: 0;
    padding: 0;
    display: inline-block;
    text-indent: 0;
    width: 0.250in;
}
p.pt-Normale-000026 {
    margin-bottom: 0;
    margin-left: 0.50in;
    text-align: justify;
    font-family: 'Verdana', 'sans-serif';
    font-size: 10pt;
    line-height: 108%;
    margin-top: 0;
    margin-right: 0;
}
p.pt-Normale-000027 {
    margin-bottom: 0;
    margin-left: 3.00in;
    text-align: justify;
    font-family: 'Verdana', 'sans-serif';
    font-size: 10pt;
    line-height: 108%;
    margin-top: 0;
    margin-right: 0;
}
p.pt-Normale-000028 {
    margin-bottom: 0;
    font-family: Calibri;
    font-size: 11pt;
    line-height: 108%;
    margin-top: 0;
    margin-left: 0;
    margin-right: 0;
}
span.pt-Carpredefinitoparagrafo-000029 {
    font-family: Calibri;
    font-size: 11pt;
    font-style: normal;
    font-weight: normal;
    margin: 0;
    padding: 0;
}
span.pt-000030 {
    font-size: 11pt;
    font-style: normal;
    font-weight: normal;
    margin: 0;
    padding: 0;
}
p.pt-Normale-000031 {
    margin-bottom: 0;
    text-align: right;
    font-family: Calibri;
    font-size: 11pt;
    line-height: 108%;
    margin-top: 0;
    margin-left: 0;
    margin-right: 0;
}
p.pt-Normale-000032 {
    margin-bottom: 0;
    text-align: center;
    font-family: Calibri;
    font-size: 11pt;
    line-height: 108%;
    margin-top: 0;
    margin-left: 0;
    margin-right: 0;
}
p.pt-Normale-000033 {
    margin-bottom: 0;
    text-align: justify;
    font-family: 'Verdana', 'sans-serif';
    font-size: 11pt;
    line-height: 108%;
    margin-top: 0;
    margin-left: 0;
    margin-right: 0;
}
p.pt-Normale-000034 {
    margin-bottom: 0;
    text-align: center;
    font-family: 'Verdana', 'sans-serif';
    font-size: 10pt;
    line-height: 108%;
    margin-top: 0;
    margin-left: 0;
    margin-right: 0;
}
span.pt-Carpredefinitoparagrafo-000035 {
    color: #000000;
    font-family: 'Verdana', 'sans-serif';
    font-size: 10pt;
    font-style: normal;
    font-weight: normal;
    margin: 0;
    padding: 0;
}
span.pt-Carpredefinitoparagrafo-000036 {
    color: #000000;
    font-family: 'Verdana', 'sans-serif';
    font-size: 10pt;
    font-style: normal;
    font-weight: bold;
    margin: 0;
    padding: 0;
}
span.pt-Carpredefinitoparagrafo-000037 {
    color: #00B0F0;
    font-family: 'Verdana', 'sans-serif';
    font-size: 10pt;
    font-style: normal;
    font-weight: normal;
    margin: 0;
    padding: 0;
}
span.pt-Carpredefinitoparagrafo-000038 {
    color: #000000;
    font-family: 'Verdana', 'sans-serif';
    font-size: 10pt;
    font-style: normal;
    font-weight: normal;
    margin: 0;
    padding: 0;
    display: inline-block;
    text-indent: 0;
    width: 0.500in;
}
span.pt-000039 {
    color: #0000FF;
    font-size: 12pt;
    font-style: normal;
    font-weight: bold;
    margin: 0;
    padding: 0;
}
p.pt-Normale-000040 {
    margin-bottom: 0;
    font-family: 'Verdana', 'sans-serif';
    font-size: 10pt;
    line-height: 108%;
    margin-top: 0;
    margin-left: 0;
    margin-right: 0;
}
</style></head><body><p><a href="/Default2.aspx">Go back to Upload Page</a></p><div><p dir="ltr" class="pt-Normale"><span class="pt-Carpredefinitoparagrafo"><img src="01a5094b-2d44-4399-93b2-2cc2db7ebfc2_files/image1.gif" style="width: 2.875in; height: 0.4583333in" alt="Immagine 9" /></span></div></p></body></html>

0 Answers0