Skip to content

Commit 790f34f

Browse files
Merge pull request #1829 from syncfusion-content/ES-865560-HtmlBodyContentAlone
ES-865560- Added content for HTMLExportBodyContentAlone API
2 parents e644a8b + f7957ab commit 790f34f

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed

File-Formats/DocIO/html.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,58 @@ document.Close()
470470

471471
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/HTML-conversions/Customize-Word-to-HTML-conversion).
472472

473+
474+
### Export HTML with body content alone
475+
476+
While saving a Word document as a HTML file using .NET Word Library, there is an option to save the HTML file with only the content within the <body> tags, excluding other elements through HtmlExportBodyContentAlone API.
477+
478+
The following code example illustrates how to export the HTML file with only the body content.
479+
480+
{% tabs %}
481+
482+
{% highlight c# tabtitle="C# [Cross-Platform]" %}
483+
//Load an existing Word document.
484+
using (FileStream fileStreamPath = new FileStream("Input.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
485+
{
486+
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
487+
{
488+
//Enable the flag, to save HTML with elements inside body tags alone.
489+
document.SaveOptions.HtmlExportBodyContentAlone = true;
490+
491+
using (FileStream outputFileStream = new FileStream("WordToHTML.html", FileMode.Create, FileAccess.ReadWrite))
492+
{
493+
//Save Word document as HTML.
494+
document.Save(outputFileStream, FormatType.Html);
495+
}
496+
}
497+
}
498+
{% endhighlight %}
499+
500+
{% highlight c# tabtitle="C# [Windows-specific]" %}
501+
//Loads an existing document
502+
//Load an existing Word document.
503+
WordDocument document = new WordDocument("Input.docx", FormatType.Docx);
504+
//Enable the flag, to save HTML with elements inside body tags alone.
505+
document.SaveOptions.HtmlExportBodyContentAlone = true;
506+
//Saves the document as html file
507+
document.Save(document, "WordtoHtml.html");
508+
document.Close();
509+
{% endhighlight %}
510+
511+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
512+
'Loads an existing document
513+
Dim document As New WordDocument("Input.docx")
514+
'Enable the flag, to save HTML with elements inside body tags alone.
515+
document.SaveOptions.HtmlExportBodyContentAlone = true
516+
'Saves the document as html file
517+
document.Save(document, "WordtoHtml.html")
518+
document.Close()
519+
{% endhighlight %}
520+
521+
{% endtabs %}
522+
523+
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/HTML-conversions/Export-HTML-with-body-content)
524+
473525
## Supported and unsupported items
474526

475527
The following document elements and attributes are supported by DocIO in Word to HTML and HTML to Word conversions.

File-Formats/DocIO/word-to-pdf.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,170 @@ In .NET Core and latest target, we have limitation in metafile. Refer {{'[here](
23672367
</tr>
23682368
</table>
23692369

2370+
## Show Warning for Unsupported Elements
2371+
2372+
When converting a Word document to a PDF, the presence of unsupported elements in the input Word document can lead to preservation issues in the converted PDF. The .NET Word library (DocIO) contains [Warning](https://help.syncfusion.com/cr/file-formats/Syncfusion.DocToPDFConverter.DocToPDFConverterSettings.html#Syncfusion_DocToPDFConverter_DocToPDFConverterSettings_Warning) API, which helps to detect and handle these unsupported elements during the conversion process. This API holds the information of unsupported elements once found in the input Word document.
2373+
2374+
Users can display warning messages for the unsupported elements using the [WarningType](https://help.syncfusion.com/cr/file-formats/Syncfusion.DocIO.DLS.WarningInfo.html#Syncfusion_DocIO_DLS_WarningInfo_WarningType) during Word to PDF conversion. Users can set a flag to stop the conversion process based on the warning.
2375+
2376+
The following code demonstrates how to stop conversion if the input Word document has an unsupported element like SmartArt during Word to PDF conversion.
2377+
2378+
2379+
{% tabs %}
2380+
2381+
{% highlight c# tabtitle="C# [Cross-platform]" %}
2382+
using (FileStream fileStream = new FileStream("Input.docx", FileMode.Open))
2383+
{
2384+
//Loads an existing Word document.
2385+
using (WordDocument wordDocument = new WordDocument(fileStream, Syncfusion.DocIO.FormatType.Automatic))
2386+
{
2387+
//Creates an instance of DocIORenderer.
2388+
using (DocIORenderer renderer = new DocIORenderer())
2389+
{
2390+
renderer.Settings.Warning = new DocumentWarning();
2391+
//Converts Word document into a PDF document.
2392+
using (PdfDocument pdfDocument = renderer.ConvertToPDF(wordDocument))
2393+
{
2394+
//If the IsCanceled boolean is enabled, the input document will contain an unsupported element.
2395+
if (renderer.IsCanceled)
2396+
{
2397+
Console.WriteLine("The execution stopped due to unsupported element.");
2398+
Console.ReadKey();
2399+
}
2400+
else
2401+
{
2402+
//Saves the PDF file.
2403+
FileStream outputFile = new FileStream("Output.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
2404+
pdfDocument.Save(outputFile);
2405+
outputFile.Dispose();
2406+
Console.WriteLine("Success");
2407+
}
2408+
}
2409+
}
2410+
}
2411+
}
2412+
{% endhighlight %}
2413+
2414+
{% highlight c# tabtitle="C# [Windows-specific]" %}
2415+
WordDocument wordDocument = new WordDocument("Input.docx");
2416+
DocToPDFConverter converter = new DocToPDFConverter();
2417+
converter.Settings.Warning = new DocumentWarning();
2418+
PdfDocument pdfDocument = converter.ConvertToPDF(document);
2419+
//If the IsCanceled boolean is enabled, the input document will contain an unsupported element.
2420+
if (converter.IsCanceled)
2421+
{
2422+
Console.WriteLine("The execution stopped due to unsupported element.");
2423+
Console.ReadKey();
2424+
}
2425+
else
2426+
{
2427+
//Saves the PDF file.
2428+
FileStream outputFile = new FileStream("Output.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
2429+
pdfDocument.Save(outputFile);
2430+
outputFile.Dispose();
2431+
Console.WriteLine("Success");
2432+
}
2433+
{% endhighlight %}
2434+
2435+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
2436+
Dim wordDocument As New WordDocument("Input.docx")
2437+
Dim converter As New DocToPDFConverter()
2438+
converter.Settings.Warning = New DocumentWarning()
2439+
Dim pdfDocument As PdfDocument = converter.ConvertToPDF(document)
2440+
2441+
' If the IsCanceled boolean is enabled, the input document will contain an unsupported element.
2442+
If converter.IsCanceled Then
2443+
Console.WriteLine("The execution stopped due to unsupported element.")
2444+
Console.ReadKey()
2445+
Else
2446+
' Saves the PDF file.
2447+
Using outputFile As New FileStream("Output.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite)
2448+
pdfDocument.Save(outputFile)
2449+
Console.WriteLine("Success")
2450+
End Using
2451+
End If
2452+
{% endhighlight %}
2453+
2454+
{% endtabs %}
2455+
2456+
The following code demonstrates how to initialize the [Warning](https://help.syncfusion.com/cr/file-formats/Syncfusion.DocToPDFConverter.DocToPDFConverterSettings.html#Syncfusion_DocToPDFConverter_DocToPDFConverterSettings_Warning) API and display warning messages for all unsupported elements in the input document. Additionally, this code shows how to set a flag to stop Word to PDF conversion if an unsupported element is identified.
2457+
2458+
{% tabs %}
2459+
2460+
{% highlight c# tabtitle="C# [Cross-platform]" %}
2461+
public class DocumentWarning : IWarning
2462+
{
2463+
public bool ShowWarnings(List<WarningInfo> warningInfo)
2464+
{
2465+
bool isContinueConversion = true;
2466+
foreach (WarningInfo warning in warningInfo)
2467+
{
2468+
//Based on the WarningType enumeration, you can do your manipulation.
2469+
//Skip the Word to PDF conversion by setting the isContinueConversion value to false.
2470+
//To stop execution if the input document has a SmartArt.
2471+
if (warning.WarningType == WarningType.SmartArt)
2472+
isContinueConversion = false;
2473+
2474+
//Warning messages for unsupported elements in the input document.
2475+
Console.WriteLine("The input document contains " + warning.WarningType + " unsupported element.");
2476+
}
2477+
return isContinueConversion;
2478+
}
2479+
}
2480+
{% endhighlight %}
2481+
2482+
{% highlight c# tabtitle="C# [Windows-specific]" %}
2483+
public class DocumentWarning : IWarning
2484+
{
2485+
public bool ShowWarnings(List<WarningInfo> warningInfo)
2486+
{
2487+
bool isContinueConversion = true;
2488+
foreach (WarningInfo warning in warningInfo)
2489+
{
2490+
//Based on the WarningType enumeration, you can do your manipulation.
2491+
//Skip the Word to PDF conversion by setting the isContinueConversion value to false.
2492+
//To stop execution if the input document has a SmartArt.
2493+
if (warning.WarningType == WarningType.SmartArt)
2494+
isContinueConversion = false;
2495+
2496+
//Warning messages for unsupported elements in the input document.
2497+
Console.WriteLine("The input document contains " + warning.WarningType + " unsupported element.");
2498+
}
2499+
return isContinueConversion;
2500+
}
2501+
}
2502+
{% endhighlight %}
2503+
2504+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
2505+
Public Class DocumentWarning
2506+
Implements IWarning
2507+
2508+
Public Function ShowWarnings(warningInfo As List(Of WarningInfo)) As Boolean Implements IWarning.ShowWarnings
2509+
Dim isContinueConversion As Boolean = True
2510+
2511+
For Each warning As WarningInfo In warningInfo
2512+
' Based on the WarningType enumeration, you can perform your manipulation.
2513+
' Skip the Word to PDF conversion by setting the isContinueConversion value to false.
2514+
' To stop execution if the input document has a SmartArt.
2515+
If warning.WarningType = WarningType.SmartArt Then
2516+
isContinueConversion = False
2517+
End If
2518+
2519+
' Warning messages for unsupported elements in the input document.
2520+
Console.WriteLine("The input document contains " & warning.WarningType & " unsupported element.")
2521+
Next
2522+
2523+
Return isContinueConversion
2524+
End Function
2525+
End Class
2526+
{% endhighlight %}
2527+
2528+
{% endtabs %}
2529+
2530+
T> Using the above Warning API, handle logic to identify the documents with unsupported elements and notify the end users to use supported elements for good preservation in the output PDF.
2531+
2532+
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Word-to-PDF-Conversion/Show-Warning-for-unsupported-elements)
2533+
23702534
## See Also
23712535

23722536
* [How to perform font substitution in Word to PDF conversion](https://support.syncfusion.com/kb/article/7499/how-to-perform-font-substitution-in-word-to-pdf-conversion)

0 commit comments

Comments
 (0)