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

Free Support Forum - aspose.com

Merge Fields turned into Plain Text by LibreOffice or OpenOffice - Convert Text into Real Merge Fields using C# .NET

I’m unable to use Merge Fields when document is saved from LibreOffice.

When document is saved from Microsoft Office it works but as soon as I open it under LibreOffice and save it as doc(x) The fields are not replaced.

I’m using the example from here: Mail Merge Form Fields

Please find attached the Files copied from example and file libreOfficeTemplate.doc saved using LibreOffice

My environment

OS: Ubuntu 14 x86_64

Java: 1.7.0

LibreOffice 4.2.6.3

Hi Jakub,


Thanks for your inquiry. This is not a bug in Aspose.Words for Java. When you ‘save as’ Microsoft Word generated ‘template.doc’ with LibreOffice as well as OpenOffice, they turn merge fields into plain text that is why Aspose.Words is unable to perform mail merge because there are no merge fields in ‘libreOfficeTemplate.doc’. I think, you should contact LibreOffice support, maybe they will be able to help to resolve this problem.

Best regards,

Hi,

I have a LibreOffice Template that I want to be filled with Aspose Words code.
Can I do that? Or because of what you said above, is not possible?

Thanks

Hi Rafael,

Thanks for your inquiry. Could you please attach your template Word document here for testing? We will investigate the issue on our end and provide you more information.

Best regards,

Sure, here it is.
It’s in portuguese, ok?
If you have any problems with that, let me know!

Thanks

Also, I’m having problem to understand how to use Fields (placeholder, mergefield) in my document template and populate a document using aspose! I have only see MailMerge examples and that’s not my case.

Thanks!

Hi Rafael,

Thanks for your inquiry. Well, there are no real merge fields in your sample ODT file. However, you can transform these place holders into real merge fields and then perform mail merge as normal. Here is code to transform these text place holders into real merge fields:
Document doc = new Document(getMyDir() + "oficio.odt");
bais.close();

doc.getRange().replace(Pattern.compile("[[(.*?)]]"), new ReplaceEvaluatorFindAndInsertMergefield(), false);

doc.save(“C:\temp\out.docx”);


static class ReplaceEvaluatorFindAndInsertMergefield implements IReplacingCallback {
<font color="RED"><b>public</b></font> <font color="RED"><b>int</b></font> replacing<font color="BLUE"><b>(</b></font>ReplacingArgs e<font color="BLUE"><b>)</b></font> <font color="RED"><b>throws</b></font> Exception <font color="BLUE"><b>{</b></font>
    Node currentNode <font color="BLUE">=</font> e<font color="BLUE"><b>.</b></font>getMatchNode<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

    <font color="GREEN"><i>// The first (and may be the only) run can contain text before the match,

// in this case it is necessary to split the run.
if (e.getMatchOffset() > 0)
currentNode = SplitRun((Run) currentNode, e.getMatchOffset());

    <font color="GREEN"><i>// This array is used to store all nodes of the match for further removing.

ArrayList runs = new ArrayList();

    <font color="GREEN"><i>// Find all runs that contain parts of the match string.

int remainingLength = e.getMatch().group(0).length();
while (
(remainingLength > 0) &&
(currentNode != null) &&
(currentNode.getText().length() <= remainingLength)) {
runs.add(currentNode);
remainingLength = remainingLength - currentNode.getText().length();

        <font color="GREEN"><i>// Select the next Run node.

// Have to loop because there could be other nodes such as BookmarkStart etc.
do {
currentNode = currentNode.getNextSibling();
}
while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));
}

    <font color="GREEN"><i>// Split the last run that contains the match if there is any text left.

if ((currentNode != null) && (remainingLength > 0)) {
SplitRun((Run) currentNode, remainingLength);
runs.add(currentNode);
}

    DocumentBuilder builder <font color="BLUE">=</font> <font color="RED"><b>new</b></font> DocumentBuilder<font color="BLUE"><b>(</b></font><font color="BLUE"><b>(</b></font>Document<font color="BLUE"><b>)</b></font> e<font color="BLUE"><b>.</b></font>getMatchNode<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>getDocument<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
    builder<font color="BLUE"><b>.</b></font>moveTo<font color="BLUE"><b>(</b></font><font color="BLUE"><b>(</b></font>Run<font color="BLUE"><b>)</b></font> runs<font color="BLUE"><b>.</b></font>get<font color="BLUE"><b>(</b></font>runs<font color="BLUE"><b>.</b></font>size<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font> <font color="BLUE">-</font> <font color="BROWN">1</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
    builder<font color="BLUE"><b>.</b></font>insertField<font color="BLUE"><b>(</b></font><font color="PURPLE">"MERGEFIELD \""</font> <font color="BLUE">+</font> e<font color="BLUE"><b>.</b></font>getMatch<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>group<font color="BLUE"><b>(</b></font><font color="BROWN">0</font><font color="BLUE"><b>) + </b></font><span style="color: purple;">"\""</span><font color="BLUE"><b>,</b></font> <font color="RED"><b>null</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font></font></pre><pre><font face="Courier New" size="2">
    <font color="GREEN"><i>//Now remove all runs in the sequence.

for (Run run : (Iterable<Run>) runs)
run.remove();

    <font color="RED"><b>return</b></font> ReplaceAction<font color="BLUE"><b>.</b></font>SKIP<font color="BLUE"><b>;</b></font>
<font color="BLUE"><b>}</b></font>

}

private static Run SplitRun(Run run, int position) throws Exception {
Run afterRun = (Run) run.deepClone(true);
afterRun.setText(run.getText().substring(position));
run.setText(run.getText().substring(0, position));
run.getParentNode().insertAfter(afterRun, run);
return afterRun;
}

I hope, this helps.

Best regards,

Hi Awais!

Thanks for your answer.
I was able to create MergeFields via MS Word and I saw that aspose has identified them.
I used this to check

How can I insert values into these fields via Aspose? I have only found via getMailMerge().execute, is this right? 'Cause it isn’t MailMergeFields…

Thanks!!

PS: I’m sending a new copy of my doc!

Hi Rafael,

Thanks for your inquiry. There are no real merge fields in your ‘Documento.odt’ document either. If there are real merge fields then yes you can use getMailMerge().execute method:

However, for static place holders, as mentioned in my previous post, please convert them to real merge fields before calling getMailMerge().execute method. There is another way you can replace these place holders with new values using ‘Find and Replace’ capabilities of Aspose.Words. Please check the following article:

Best regards,

Awais,

As I said, I was able to insert merge fields in my document (via MS Word) and use getMailMerge.execute() (even they are not actually mail merge fields, only merge fields).
But now my problem is: when I open this template in LibreOffice and save it, it loses the merge fields. They become (I think) placeholders instead.
I know it’s not what you are here for, but you know how I can actually create merge fields in a template via LibreOffice and use it with aspose? I only saw tutorial to Word in your documentation.

Another thing:
With both Execute (MailMerge) and with Replace, Aspose changes something in my document.
I will attach the template and the final document so you can see the changes:

1) It adds “1.” and “2.” to my labels “Do” (From) and “Ao” (To);
2) Align the text to right on values of these labels above;
3) Compress the label “ASSINADO DIGITALMENTE” (digitally signed); and
4) Enlarges foot syze (where there’s a field c_NUP).

Thanks a lot.

Hi Rafael,


Thanks for the additional information. Please do the following steps to insert a merge field using OpenOffice Writer:

  1. Select Insert > Fields > Other. The Fields dialog box opens.
  2. Click the Database tab.
  3. On the left hand side, select Mail merge fields.
  4. Under Database selection find your data source. Expand it to see the fields.
  5. Click the field you want to insert, then click Insert to insert the field. You can insert any number of fields any number of times into your mail merge document.
  6. Click Close when you are done.

After that you can use getMailMerge().execute method to perform mail merge.

Secondly, regarding the four points you mentioned, we tested the scenario and have managed to reproduce the same problems on our end. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-12024. Our product team will further look into the details of these problems and we will keep you updated on the status of correction. We apologize for your inconvenience.

Best regards,

Thanks Awais.

If possible, please send me an e-mail whenever you correct this issue.

About creating Merge Fields
MS Word gives 2 possiblities: creating just a merge field and creating a merge field from a database.
I don’t see this possibility on LibreOffice, that’s what I’ve asked.
In my case, these fields will be populated via LotusNotes script, they are not a Mail Field.

Regards,

Rafael

PS: I was able to identify some hidden problems with identation in the template (I simply deleted the entire line of problems 1 and 2, mentioned above, and redid the lines) and it worked well.
The problems 3 and 4, however, continues.

Here are the new examples of what I’ve said.

The field “ASSINADO DIGITALMENTE” continues without margin and the footer changed the position (up a few lines)

Hi Rafael,

Thanks for your inquiry. Unfortunately, this issue (WORDSNET-12024) is not resolved yet. Currently, this issue is pending for analysis and is in the queue. Please note that an issue first needs to be investigated to find the exact source of problem and then we investigate the best possible way to fix the issue. Once the issue is analyzed, we will then be able to provide you more information. Rest assured, we will inform you via this thread as soon as this issue is resolved. Sorry for any inconvenience.

Best regards,

Thanks, Awais.

Do you have a guess/prediction on how long it will take?
Just to know if it’s going to take long than 25 days and I won’t be able to use the trial anymore.

Hi Rafael,


Thanks for your inquiry. Unfortunately, at the moment we cannot provide you any reliable estimate. We will inform you as soon as this issue is resolved. We apologize for your inconvenience.

Best regards,

The issues you have found earlier (filed as WORDSNET-12024) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.