微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将图片从 docx word 转换为 html 文件中的 base64 编码图片

如何解决将图片从 docx word 转换为 html 文件中的 base64 编码图片

我使用 Open XML SDK 2.12.1 version 将 docx word 转换为 html 文件并使用 iText (Version 7.1.1) 使用 HtmlConverter (html2pdf version 2.0.1) 将 html 转换为 pdf

我的问题是将图像从 docx word 转换为 base64 编码的图像到 html 文件

要查看导出 pdf 文件中的图像,我需要这部分

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

改为

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

(请注意,base64 编码的图像被截断以适合此页面

带有 base64 编码图像的文件的浏览器视图和结果 PDF

Reference

我试过没有成功

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

这是docx word转html文件的结果

<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>

docx word转html文件代码

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

html文件转pdf文件代码

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。