We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Background color of text is lost after RTF to HTML conversion using Java

Issue) Rtf text having background color is not converted properly to Html / Pdf file. Its coming just as simple text without background color.
Below is the sample rtf text and its related image for reference.

Rtf text:
{\rtf1\ansi\deff0\uc1\ansicpg1252\deftab720{\fonttbl{\f0\fnil\fcharset1 Arial;}{\f1\fnil\fcharset1 Tahoma;}{\f2\fnil\fcharset1 Calibri;}{\f3\fnil\fcharset2 WingDings;}{\f4\fnil\fcharset2 Symbol;}}{\colortbl\red0\green0\blue0;\red255\green0\blue0;\red0\green128\blue0;\red0\green0\blue255;\red255\green255\blue0;\red255\green0\blue255;\red128\green0\blue128;\red128\green0\blue0;\red0\green255\blue0;\red0\green255\blue255;\red0\green128\blue128;\red0\green0\blue128;\red255\green255\blue255;\red192\green192\blue192;\red128\green128\blue128;\red0\green0\blue0;\red128\green128\blue0;\red0\green0\blue0;}\wpprheadfoot1\paperw12240\paperh15840\margl1880\margr1880\margt1440\margb1440\headery720\footery720\pgbrdrhead\pgbrdrfoot\pgbrdropt32\endnhere\sectdefaultcl{*\generator WPTools_7.000;}{\stylesheet
{\s1\li0\fi0\ri0\sb0\sa160\sl259\slmult1\ql\vertalt\f2\fs22\cf15 Normal;}
{\s2\li0\fi0\ri0\sb0\sa0\ql\vertalt\fs22 Default Paragraph Font;}}
{\sl250\slmult1\plain\f2\fs22\cf0\cb1 Text with red background\par
\pard\plain\sl250\slmult1\plain\f0\fs22\par
}}

Sample Image:
1_RtfImageTextWithRedBackground.jpg (24.8 KB)

Sample java code used for reference:
Added following dependency and repository in pom.xml

com.aspose aspose-words 20.3 jdk17



AsposeJavaAPI
Aspose Java API
https://repository.aspose.com/repo/

============= Java code with input and output option ============
public static void main(String[] args) throws Exception {
License license = new License();
license.setLicense(“C:\Users\pprajapati\Desktop\9122\Aspose.Words.lic”);
// with options
RtfLoadOptions rtfOptions = new RtfLoadOptions();
rtfOptions.setRecognizeUtf8Text(true);

PdfSaveOptions pdfOptions = new PdfSaveOptions();
pdfOptions.setCompliance(PdfCompliance.PDF_17);

HtmlSaveOptions htmlOptions = new HtmlSaveOptions(SaveFormat.HTML);
htmlOptions.setPrettyFormat(true);
htmlOptions.setExportListLabels(ExportListLabels.BY_HTML_TAGS);

Document doc = new Document("C:\\Users\\pprajapati\\Desktop\\9122\\program\\2_RedBackgroundDB.rtf", rtfOptions);
doc.save("C:\\Users\\pprajapati\\Desktop\\9122\\program\\outputAspose.pdf", pdfOptions);
doc.save("C:\\Users\\pprajapati\\Desktop\\9122\\program\\outputAspose.html", htmlOptions);
System.out.println("done");

}

@pprajapati

Please note that Aspose.Words mimics the behavior of MS Word. If you open the RTF in MS Word and convert it to HTML and PDF, you will get the same output.

We understand but is there anything wrong in our rtf that aspose not able show background color. Is RTF specification is old or something ? We are right now using temp license to evaluate aspose library fro rtf to html conversion

@jshahv

Please copy the RTF content shared in this thread, paste it in notepad and save the document as .rtf. Open the RTF in MS Word. You will notice that there is no background color. Aspose.Words does the same. So, it is expected behavior of Aspose.Words.

Can we know which rtf specification aspose word is using to convert rtf to any other doc ?

@jshahv

Aspose.Words follows the same specification of MS Word.