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

Free Support Forum - aspose.com

Aspose Words

Hello everyone,

I would like to understand if it is possible getting a text with html tags inside and render it in a TextView as a doc file.
I’m creating an app with an editor inside made with an EditText in which the user can insert some text and see it in a preview in another section of the app in a TextView.
The problem is that appears in the TextView not well formatted and with the newline chars not inserted in the correct place.
With your library is it possible to handle text files as doc files as the official Word does?

Thanks
Giorgio

Hi Giorgio,


Thanks for your inquiry. Please note that Aspose.Words for Android is a class library and with it you can programmatically generate, modify, convert and render word processing documents. So, it does not offer any UI or control for performing these document processing tasks. However, if you’re intended to just preview your Word/Html document in your android application, we can offer you a simple way that will help you in achieving what you are looking for.

  1. Load your Word document into Aspose.Words for Android
  2. Generate images against each page in Word document and then view them in some android control.

Please see the following code that uses standard ImageView widget to show Aspose.Words generated images:
try
{
String licString = Environment.getExternalStorageDirectory().getAbsolutePath() + “/Aspose.Words.Android.lic”;
String inputPath = Environment.getExternalStorageDirectory().getAbsolutePath() + “/input.html”;
String outputPath = Environment.getExternalStorageDirectory().getAbsolutePath() + “/”;
com<font color="BLUE"><b>.</b></font>aspose<font color="BLUE"><b>.</b></font>words<font color="BLUE"><b>.</b></font>License lic <font color="BLUE">=</font> <font color="RED"><b>new</b></font> com<font color="BLUE"><b>.</b></font>aspose<font color="BLUE"><b>.</b></font>words<font color="BLUE"><b>.</b></font>License<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
lic<font color="BLUE"><b>.</b></font>setLicense<font color="BLUE"><b>(</b></font>licString<font color="BLUE"><b>,</b></font> <font color="RED"><b>this</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

com<font color="BLUE"><b>.</b></font>aspose<font color="BLUE"><b>.</b></font>words<font color="BLUE"><b>.</b></font>Document doc <font color="BLUE">=</font> <font color="RED"><b>new</b></font> com<font color="BLUE"><b>.</b></font>aspose<font color="BLUE"><b>.</b></font>words<font color="BLUE"><b>.</b></font>Document<font color="BLUE"><b>(</b></font>inputPath<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

com<font color="BLUE"><b>.</b></font>aspose<font color="BLUE"><b>.</b></font>words<font color="BLUE"><b>.</b></font>ImageSaveOptions options <font color="BLUE">=</font> <font color="RED"><b>new</b></font> com<font color="BLUE"><b>.</b></font>aspose<font color="BLUE"><b>.</b></font>words<font color="BLUE"><b>.</b></font>ImageSaveOptions<font color="BLUE"><b>(</b></font>SaveFormat<font color="BLUE"><b>.</b></font>PNG<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
options<font color="BLUE"><b>.</b></font>setPageCount<font color="BLUE"><b>(</b></font><font color="BROWN">1</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

<font color="RED"><b>for</b></font> <font color="BLUE"><b>(</b></font><font color="RED"><b>int</b></font> i <font color="BLUE">=</font> <font color="BROWN">0</font><font color="BLUE"><b>;</b></font> i <font color="BLUE"><</font> doc<font color="BLUE"><b>.</b></font>getPageCount<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font> i<font color="BLUE"><font color="BLUE">+</font><font color="BLUE">+</font></font><font color="BLUE"><b>)</b></font>
<font color="BLUE"><b>{</b></font>
    options<font color="BLUE"><b>.</b></font>setPageIndex<font color="BLUE"><b>(</b></font>i<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
    doc<font color="BLUE"><b>.</b></font>save<font color="BLUE"><b>(</b></font>outputPath <font color="BLUE">+</font> <font color="PURPLE">"out_"</font> <font color="BLUE">+</font> i <font color="BLUE">+</font> <font color="PURPLE">".png"</font><font color="BLUE"><b>,</b></font> options<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
<font color="BLUE"><b>}</b></font>

Bitmap bm <font color="BLUE">=</font> BitmapFactory<font color="BLUE"><b>.</b></font>decodeFile<font color="BLUE"><b>(</b></font>outputPath <font color="BLUE">+</font> <font color="PURPLE">"out_1.png"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

ImageView img <font color="BLUE">=</font> <font color="BLUE"><b>(</b></font>ImageView<font color="BLUE"><b>)</b></font>findViewById<font color="BLUE"><b>(</b></font>R<font color="BLUE"><b>.</b></font>id<font color="BLUE"><b>.</b></font>imageView1<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
img<font color="BLUE"><b>.</b></font>setImageBitmap<font color="BLUE"><b>(</b></font>bm<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

}
catch (Exception e)
{
e.printStackTrace();
}

Hope, this helps.

Dear Tahir,

how the library performs the doc.getPageCount() operation? Does it know the page number?
If I have a string populated with text e html tags such the only way to render the document is to create an image for every page?

Thanks for the support
Best regards
Giorgio


Hi Giorgio,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does.

When Document.getPageCount is called, it invokes page layout, document is formatted into pages and returned the number of pages in the document.

Hi Tahir,

I downloaded the AsposeWords for android demo and I’m trying to implement your example.
Is the license file necessary also for the demo?
Is it possible instead of passing an html file to this “com.aspose.words.Document doc = new com.aspose.words.Document(inputPath);” passing directly a string populated with html tag?

Thanks
Best regards
Giorgio


Hi Giorgio,

Thanks for your inquiry. We suggest you please request for 30-days temporary license from here:
Get temporary license

Please use DocumentBuilder.InsertHtml method to insert an HTML string into the document as shown in following code snippet.

Document document = new Document();
DocumentBuilder builder = new DocumentBuilder(document);
builder.insertHtml(
"

Paragraph right

"
+
"Implicit paragraph left" +
"
Div center
"
+
"

Heading 1 left.

"
);