BoldDesk®Customer service software offering ticketing, live chat, and omnichannel support, starting at $49/mo. for 10 agents. Try it for free.
Hello,
I have the following requirement. I get an html string, which contains some tables and a style tag (this is a part of the html string):
<html><head><title>Report</title><style type="text/css">
.testo_nero{
font-family:Arial;
font-size:7pt;
}
.voce_bordo_tl{
font-family:Arial;
font-size:7pt;
border-top:1px #000000 solid;
border-left:1px #000000 solid;
}
.voce_bordo_tlr{
font-family:Arial;
font-size:7pt;
border-top:1px #000000 solid;
border-left:1px #000000 solid;
border-right:1px #000000 solid;
}
</style></head><body>
<center>
<table cellpadding="0" cellspacing="0" width="649px">
<tr>
<td valign="top">
<table cellpadding="0" cellspacing="0" width="322px">
<col width="152px">
<col width="50px" align="right">
<col width="35px" align="center">
<col width="50px" align="right">
<col width="35px" align="center">
<tr>
<td class="testo_bianco">Bilancio al</td>
<td align="right" class="testo_bianco" colspan="2" align="center">31/12/2006</td>
<td align="right" class="testo_bianco" colspan="2" align="center">31/12/2007</td>
</tr>
<tr>
<td class="voce_bordo_tl">Tipo Bilancio</td>
<td align="right" class="voce_bordo_tl" colspan="2" align="center">
Definitivo
</td>
<td align="right" class="voce_bordo_tlr" colspan="2" align="center">
Definitivo
</td>
</tr>
I take this html string, create a html file, where the styles will be applied ok and the tables looks good, then I want to insert this html file in my pdf generated file, at a bookmark location. I tried the approach from the documention page:
https://support.syncfusion.com/kb/article/17603/how-to-insert-html-within-bookmark-in-a-core-word-document
but the table doesn't appear in the generated file. This is my code:
public bool InsertHtmlInDoc(string htmlString, out string htmlFilePath)
{
string fileName = string.Empty;
try
{
string tempFolder = _appSettings.HtmlFilePath + @"Temp\";
if (!Directory.Exists(tempFolder))
Directory.CreateDirectory(tempFolder);
fileName = tempFolder + Guid.NewGuid().ToString() + ".html";
FileStream htmlFile = File.Open(fileName, FileMode.Create, FileAccess.Write);
byte[] info = new UTF8Encoding(true).GetBytes(htmlString);
htmlFile.Write(info, 0, info.Length);
htmlFile.Close();
htmlFilePath = fileName;
return true;
}
catch (Exception ex)
{
htmlFilePath = fileName;
return false;
}
}
private void InsertHtmlIntoBookmark2(BookmarksNavigator navigator, string htmlFilePath)
{
try
{
if (!File.Exists(htmlFilePath))
{
_logger.LogInfo($"HTML file not found: {htmlFilePath}");
return;
}
// Read the HTML content from the file
string htmlContent = File.ReadAllText(htmlFilePath);
// Ensure we are working within an existing bookmark
var bookmarkStart = navigator.CurrentBookmark?.BookmarkStart;
if (bookmarkStart == null)
{
_logger.LogInfo("No active bookmark found in the navigator.");
return;
}
// Move to the bookmark location
navigator.MoveToBookmark(bookmarkStart.Name);
// Ensure the bookmark was successfully moved to
if (navigator.CurrentBookmark == null || navigator.CurrentBookmark.BookmarkStart == null)
{
_logger.LogInfo($"Failed to move to bookmark '{bookmarkStart.Name}'.");
return;
}
_logger.LogInfo($"Bookmark '{bookmarkStart.Name}' found. Proceeding to insert content.");
// Convert the HTML content into a WordDocumentPart
WordDocumentPart htmlDocumentPart = ConvertHTMLToWordDocumentPartSync(htmlContent);
if (htmlDocumentPart == null)
{
_logger.LogInfo("Converted WordDocumentPart is null. HTML conversion failed.");
return;
}
// Ensure the bookmark content is cleared before inserting new content
navigator.DeleteBookmarkContent(true);
// Replace the content inside the existing bookmark
navigator.ReplaceContent(htmlDocumentPart);
//Close the WordDocumentPart
htmlDocumentPart.Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to insert HTML into bookmark.");
}
finally
{
// Cleanup: Delete the temp HTML file after reading
if (File.Exists(htmlFilePath))
File.Delete(htmlFilePath);
}
}
static WordDocumentPart ConvertHTMLToWordDocumentPartSync(string html)
{
//Create a new Word document.
WordDocument tempDocument = new WordDocument();
//Add minimal content to the document (ensures the document structure is valid).
tempDocument.EnsureMinimal();
//Append the HTML string to the last paragraph of the temporary Word document.
tempDocument.LastParagraph.AppendHTML(html);
//Create a new WordDocumentPart instance.
WordDocumentPart wordDocumentPart = new WordDocumentPart();
//Load the temporary document into the WordDocumentPart.
wordDocumentPart.Load(tempDocument);
//Return the WordDocumentPart containing the converted HTML content.
return wordDocumentPart;
}
The html file looks something like this:
What can cause the issue?
Hi Ciprian,
We have tried to reproduce the reported issue "Table not preserved
while inserting HTML at a bookmark location", but it is working
properly on our end. Kindly refer to the attached sample. If you still
encounter the issue, kindly modify the sample to make the issue reproducible
and also share the full HTML file used on your side.
Regards,
Sindhu Ramesh.
Thank you for your response,
It is ok, the content is placed in the generated doc, but the styles are not applied, the tables are created without borders. It is something additional that needs to be added in order for the styles to be applied?
Ciprian,
Upon further investigation, we noticed that you are using internal CSS to
define table border properties. Currently, DocIO doesn’t have complete support
for CSS styles in HTML to Word conversion. We have already logged these as a
feature request in our database. We don’t have any immediate plans to implement
these features.
You can track the status of this feature using the following feedback link:
https://www.syncfusion.com/feedback/11547/complete-css-support-in-html-to-word-conversion
As a workaround, to preserve border styles, we recommend specifying the
border properties using inline CSS within the elements. Kindly refer to the
modified HTML document.
Hello,
Based on your response, I've implemented a logic for extract the styles from the classes and replace the classes in the html string with the inline styles, but the result is the same, the borders and styles are not applied.
I attached my code and the template word file. What can be the issue?
Ciprian,
From the provided code snippet, we noticed that you are loading htmlString,
which contains internal CSS, instead of inlineHTML.
When using inlineHTML (which applies inline CSS), the output document
preserves the formatting correctly, matching the original HTML document.
It is ok, the styles are applied now.
But there is an issue related to the pdf conversion. If the generated file is word, the styles and the layout from the html file is kept, but after the conversion to pdf, the tables are different aligned. I attached the code.
Also, I added an autoshape in the template file, but in the generated pdf, it has different style, the correct style like in the template is kept only if the generated file is docx.
What causes these issues?
Attachment: bilanci_6479e5d5.zip
Ciprian,
Regarding “Table preservation issue”:
We have reproduced the reported issue “Table not preserved properly while
converting DOCX to PDF” on our end. We will validate this issue and update
you with more details within 2 days.
Regarding “Shape formatting issue”:
Currently DocIO doesn’t have support to preserve shape with gradient
fill in Word to PDF conversion. So that, shape is preserved with
improper color in the PDF document. Currently we do not have immediate plan to
implement this feature.
The status of this features can be tracked through the following feedback
links:
Preserve gradient fill for text box and shape in Word to PDF
conversion in WinForms | Feedback Portal
Regarding “Text inside shape not preserved”:
We have reproduced the reported issue, and we will validate and update you with more details
within 2 days.
Ciprian,
Regarding “Table preservation issue”:
Currently we are validating this issue with high priority on our end. We will
provide more details within two days.
Regarding “Text inside shape not preserved”:
Upon further investigation, we found that the autofit shape is present in the
provided Word document. Currently, we do not support autofit shapes in Word to
PDF conversion, which results in the contents of autofit shapes are not
preserved in the PDF document. We have already logged this issue as a feature
in our end. We don’t have any immediate plans to implement this feature. We
will let you know when this feature is implemented.
You can track the status of the feature request through the following feedback
link:
Provide
support for shape auto resize in Word to PDF conversion in WinForms | Feedback
Portal
As a workaround to resolve the issue, we suggest you use autofit text boxes
in the input Word document. Because autofit text boxes are supported in Word to
PDF conversion using DocIO library. For your reference, we have modified
the input Word document to include autofit text boxes, which can be downloaded
from the link below.
I replaced the shape with the textbox, now the issue related to "Text inside shape not preserved" was fixed, in the textbox the text is ok, but the issue with the style still standing, it looks different after the word to pdf conversion.
This is how the textbox looks in the word:
and this is how it looks in the converted PDF: