Hello there,
I am use Open XML SDK to convert docx word to html file and use iText (Version 7.1.1) to convert html to pdf using HtmlConverter (html2pdf version 2.0.1).
The PDF in result has no images at all !
But the result it's without image because itext7 support base64 image.
How to do edit my code for convert docx word to html file using support base64 image?
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);
}
}
}
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();
}