How do I insert RichText Content in an Aspose Document?

Hi,

I’m populating a OpenDocument Template with content via Aspose Words.
This content is storage at NotesDocuments and I use getRange().Replace to change some placeholders into values and generate a Document.
One of the content is RichText and I’m trying to get it like this:

// Gets the NotesDocument 
Document msg = db.getDocumentByUNID("xxxxxxxxxxxxxxxx");

// Gets the RichText Field of the NotesDocument
RichTextItem rtitem = (RichTextItem)msg.getFirstItem("cdTexto"); 

// There are 2 ways to get the RT content…FormattedText and Unformatted
// Formatted returns the text changing tabs for spaces
// Unformatted returns the text as it is
// I’ve tryied both methods
// String text = rtitem.getFormattedText(false, 0, 0);
String text = rtitem.getUnformattedText();
docAspose.getRange().replace("<<c_Texto>>", text , true, false);

When the Document is generated, it brings all of text in one line…without considering tabs, returns, etc.

I didn’t find a documentation about how to do this, forgive me if there is one…but how can I accomplish that?

Thanks!

Hi Tahir! Thanks for you help!

Just to be clear (to me), so I have to create an Aspose Document with my RTF String and then add it to the document I’m generating with a template?

Thanks fo your quick help!

Hi Rafael,

Thanks for your inquiry. Yes, your understanding is correct. Please let us know if you have any more queries.

Hi, Tahir

Just to help…There was an error in this code regarding the use of ITERATOR (I think is because of java version…this solution was ok til 1.5 but now it dosen’t, I believe).

I was able to fix it changing a few lines…
Instead of using:

for (Run run : (Iterable) runs){
    run.remove();
}

I changed to:

for (Iterator it = runs.iterator(); it.hasNext() {
    Run run = (Run) it.next();
    run.remove();
}

I’m leaving this here to help if someone gets the same problem.

Tahir,

Instead of leaving the original text format, it leaves a little bit messy.

Here are the original text:

  1. Transmito a V. Sa. a Nota Técnica anexa, em atendimento ao contido na

referência.

  1. Transmito a V. Sa. a Nota Técnica anexa, em atendimento ao contido na

referência.Transmito a V. Sa. a Nota Técnica anexa, em atendimento ao contido

na referência.Transmito a V. Sa. a Nota Técnica anexa, em atendimento ao

contido na referência.Transmito a V. Sa. a Nota Técnica anexa, em atendimento

ao contido na referência.Transmito a V. Sa. a Nota Técnica anexa, em

atendimento ao contido na referência.Transmito a V. Sa. a Nota Técnica anexa,

em atendimento ao contido na referência.Transmito a V. Sa. a Nota Técnica

anexa, em atendimento ao contido na referência.Transmito a V. Sa. a Nota

Técnica anexa, em atendimento ao contido na referência.Transmito a V. Sa. a

Nota Técnica anexa, em atendimento ao contido na referência.Transmito a V. Sa.

a Nota Técnica anexa, em atendimento ao contido na referência.Transmito a V.

Sa. a Nota Técnica anexa, em atendimento ao contido na referência.Transmito a

V. Sa. a Nota Técnica anexa, em atendimento ao contido na referência.Transmito

a V. Sa. a Nota Técnica anexa, em atendimento ao contido na

referência.Transmito a V. Sa. a Nota Técnica anexa, em atendimento ao contido

na referência.Transmito a V. Sa. a Nota Técnica anexa, em atendimento ao

contido na referência.Transmito a V. Sa. a Nota Técnica anexa, em atendimento

ao contido na referência.Transmito a V. Sa. a Nota Técnica anexa, em

atendimento ao contido na referência.

  1. Transmito a V. Sa. a Nota Técnica anexa, em atendimento ao contido na

referência.Transmito a V. Sa. a Nota Técnica anexa, em atendimento ao contido

na referência.Transmito a V. Sa. a Nota Técnica anexa, em atendimento ao

contido na referência.Transmito a V. Sa. a Nota Técnica anexa, em atendimento

ao contido na referência.Transmito a V. Sa. a Nota Técnica anexa, em

atendimento ao contido na referência.Transmito a V. Sa. a Nota Técnica anexa,

em atendimento ao contido na referência.Transmito a V. Sa. a Nota Técnica

anexa, em atendimento ao contido na referência.

And then, in the final document, attached.

Hi Rafael,

Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.

  • Please attach your input Word document.

  • Please create a standalone/runnable simple Java application that demonstrates the code (Aspose.Words code) you used to generate your output document

  • Please attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will investigate as to how you are expecting your final document be generated like.

As soon as you get these pieces of information to us we’ll start our investigation into your issue.

Hi Tahir,

I ran a few debugs over here and I’ve managed to figure it out. It is something that Lotus Notes do. He brakes the text diferentily that it’s original and that’s why Aspose couldn’t realize the formatting.

So, I changed a couple of things and create (via Notes) a html form with a text area and the formatting came almost exactly the same.

Didn’t use the builder’s font, size etc…and didn’t came with the correct position marked in the original template.

My text, template and final document and java code are attached to this message.

Thanks!

Hi Rafael,

Thanks for sharing the detail. You are getting the expected output document. It seems that you are inserting the simple text in the document instead of RTF. Please share the text value which you are using in following line of code.

String texto = rtitem.getUnformattedText();

Salomao:
Tahir,
Instead of leaving the original text format, it leaves a little bit messy.

Could you please share some more detail about this issue along with screen shots of problematic sections? The text.odt is ODT file format. It is not rtf file.

Tahir,

It’s not a RTF “Document”, it’s a richtext “text”.

Lotus Notes saves a text with formatting, paragraphs, tabs etc in a field of it’s database. Then, I get this field with that line (getUnformattedText) and use it.
This method (getUnformattedText) don’t bring simple text. It gives me the text as richtext (I’ve commented this in the code I’ve sent you).

In the result document, the problem is that this text did not come in the right position where the placeholder was, you see? And did not use the font defined to my document. Perhaps because of the documentBuilder instanciated inside that class FindAndInsertRTF?

Hi Rafael,

Thanks
for your inquiry.

*Salomao:

In the result document, the problem is that this text did not come in the right position where the placeholder was, you see?*

As you are replacing <<c_Texto>> with text, you can achieve your requirements using Range.Replace overloaded method. Please check the highlighted code snippet below.

String texto = "Transmito a V. Sa. a Nota Técnica anexa, em atendimento ao contido na referência. "
    + "Transmito a V. Sa. a Nota Técnica anexa, em atendimento ao contido na referência. Transmito a "
    + "V. Sa. a Nota Técnica anexa, em atendimento ao contido na referência. Transmito a V. Sa. a Nota Técnica anexa, "
    + "em atendimento ao contido na referência. Transmito a V. Sa. a Nota Técnica anexa, em atendimento ao contido na "
    + "referência. Transmito a V. Sa. a Nota Técnica anexa, em atendimento ao contido na referência. Transmito a V. Sa. "
    + "a Nota Técnica anexa, em atendimento ao contido na referência.";
// Open the stream. Read only access is enough for Aspose.Words to load a document.
InputStream stream = new FileInputStream(MyDir + "Template.odt");
// Load the entire document into memory.
com.aspose.words.Document docAspose = new com.aspose.words.Document(stream);
// You can close the stream now, it is no longer needed because the document is in memory.
stream.close();
// ############################## TESTE ##############################
// com.aspose.words.Document rtfdoc = RtfStringToDocument(texto);
// FindandInsertRTF replacertf = new FindandInsertRTF(rtfdoc);
// Pattern regex = Pattern.compile("<>", Pattern.CASE_INSENSITIVE);
// docAspose.getRange().replace(regex, replacertf, false);
docAspose.getRange().replace("<<c_Texto>>", texto, false, false);
// ############################## TESTE ##############################

*Salomao:

And did not use the font defined to my document.*

Please note that
Aspose.Words mimics the same behavior as MS Word does. If you open the
text document in MS Word, the font is Courier New (10.5). The same font
is available in output document.

If you still face problem, please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.

Tahir,

Thanks for answer but this is not a solution, just see my last post.
I’m replacing the placeholder with text but this text comes from the method rtitem.getUnformattedText()
If I use the replace() with string directly, my text loses all formating, including line breaks and paragraphs.
When
I use the replace() with IReplacingCallback (you previous solution) then
comes with line breaks, paragraphs etc but it dosen’t come with the FONT
used it the document (It loads a template, remember? so there is font
name, font size, already setted to the entire document) and the replace() ignores the left margin
that I’ve put to the placeholder.

I’m sending what Asposes generates, what should have generated and what generates if I use the replace method with String. I believe it gonna be easier to understand now what’s going on.

Hi Rafael,

Thanks
for your inquiry. You are facing the expected behavior of Aspose.Words. The string value returned by rtitem.getUnformattedText() method does not contain the Paragraph formatting as shown in Odt document.

Please save the string value returned by rtitem.getUnformattedText() method to disk and share it here for our reference. We will then provide you more information about your query along with code.

Tahir, I think it does.
I use it in every other place that I want to exhibit the text and exhibits with paragraphs, tabulation and line breaks as it should.

Next monday I’ll send what you’ve asked because I’m on a tiny break.

Thanks

Tahir

Here are the results.
I made 4 tests.

I’m picking up exactly this text above and inserting 4 different ways into 4 documents.

  1. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose.


  • Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose.

  • Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose. Testing how does it get in aspose.
    The results are:
    1. FindandInsertRTF with the text coming from getUnformattedText
    2. FindandInsertRTF with the text coming from getFormattedText
    3. Inserting directly the text using getUnformattedText
    4. Inserting directly the text using getFormattedText

    The only one that produces almost the ultimate result is option 1 but I have to change a few things:
    - Make the text use the font of the rest of the document (I put the variable ImportFormatMode.USE_DESTINATION_STYLES but it didn’t work I believe)
    - The text comes aligned with the left margin of the document (besides the first line of each item)

    Hope this helps you.

    PS: The getUnformattedText on result 3 comes all in one line in Aspose Document. but if I use this method in any other place to exhibt the text, it comes perfect. I’m attaching one image so you can see this.

  • Hi Tahir. Thanks for the solution, I’m gonna test it.

    But, one question, did you saw the 4 results that I’ve posted? The Result “1” comes almost perfect and I don’t have to add tabStops and anything.
    I get it that Aspose Words mimics the behavior of MS Word, but I’m using a Document Template with defined font in it. And when I put some values into the placeholders, they all use this exact template font EXCEPTS this rich text. As I’ve said in my post-script, this insertion don’t respect the previous margin configuration and it dosen’t use the template pre-defined font. Why is that happening?

    Tahir,

    I was testing a different approach for what I want.
    Instead of using a template document, I wrote all document itself using DocumentBuilder.
    Doing that along with builder.write to print the UnformattedText method I was able to achieve my goal. The text was printed perfectly.
    So I think I’m going to build the rest of the document using the DocumentBuilder class.

    If you discover the solution to my problem (using the template) please tell me because in the future, I may use templates like that with rich texts.
    And if you need anymore information, please ask me.
    I still using trial aspose and it will be a greate help.

    Thanks!

    Hi Rafael,

    Thanks
    for your inquiry. Yes, I checked your shared documents. Yes, you can achieve your requirements using DocumentBuilder. You can also achieve your requirements by inserting the paragraphs as shared in my previous post.

    Please check your document (WhatShouldGenerate.odt) shared here. This document contains tabstop. This is the reason I added TabStops in my code example.

    Please note that the text returned by rtitem.getUnformattedText() method is loaded into Aspose.Words DOM with font Courier New (10.5). This text does not contain any other formatting e.g TabStops, bold, Italic etc.

    As I shared earlier, Aspose.Words mimics the same behavior as MS Word does. If you insert such text into your template document, you will get the same output.

    The text returned by rtitem.getUnformattedText() is simple text. You can use DocumentBuilder to insert this text into your desired location with your desired font formatting. I suggest you please read the following documentation link for your kind reference.
    https://docs.aspose.com/words/java/using-documentbuilder-to-modify-a-document/

    As you are inserting text into your template document, you just need to move the cursor at desired location and insert the text using DocumentBuilder.Write or DocumentBuilder.Writeln mehod. Please read about specifying formatting from here:
    https://docs.aspose.com/words/java/programming-with-documents/

    If
    rtitem.getUnformattedText() returns multiple Paragraphs, you may use the approach share in my previous post. In my example, I am creating Paragraph and Run nodes. You may use DocumentBuilder.Writeln method at that place.

    Hope this answers your query. Please let us know if you have any more queries.