Hi Team,
We have raised same issue earlier but didnt get proper solutions so raising same question again.
Scenario : While Generating document, inside document we may have some expressions combined with conditions which needs to be resolve before inserting into document and If condition is not satisfied then we have to remove that expression from that position from document.
For example, as shown in below screenshot,document can have expressions like below
and the expressions which are having # is a conditional expressions and after hash(#) whatever value is there that is condition and if that condition is true then only we need to replace before hash value or else we need to remove that expression.
So to remove the expression when condition is not satisfying we used below removeArgument method as suggested by you people[Related post will check and update].RemoveArgument method code please check in the attachment.
But removeargument method is causing issue when two expressions are coming in same paragraph which are not satisfying condition and when we try to remove them. So again we contacted you people and you said if data is coming like that this removeargs method wont work and as suggested by you people we used ReplaceArgs.setReplacement(“”) but it is leaving blank space like below.
Actual :
Actual1
Expected
RemoveArgs :
Can you please suggest us is there any way to fix this space issue.
Thanks,
Priyanka.
@priyanka9 Most likely the space is causes by an empty paragraph, but it is difficult to say for sure using screenshot. So please attach your source document, simple code that will allow us to reproduce the problem (in textual form). We will check the issue and provide you more information.
In general the following code should remove an empty paragraph:
// Remove current paragraph if it is empty.
if(!currentParagraph.hasChildNodes())
currentParagraph.remove();
Hi @alexey.noskov ,
Sorry for delay, Here are the inputs what you are expecting.
out .docx (31.4 KB)
MainLogic.docx (19.2 KB)
Can you please try with this input and let us know if u require any other details.
Thanks,
Priyanka.
@priyanka9 The provided code does not include all method used. Could you please create a simple application that will allow us to reproduce the problem on our side? Also, please try specifying backward direction in find/replace options:
options.setDirection(FindReplaceDirection.BACKWARD);
Hi @alexey.noskov ,
As you asked I have created sample project for both scenarios like setReplacement empty and removeArgs. Can you please check and help us here.
removeArgsError is coming from below args if data is like this so thats why we used setReplacement(“”) but it is leaving empty line in document which is breaking styling of the document.
removeArgsError:
removeArgsErrorMsg
Error
aspose_test.zip (249.0 KB)
Thanks,
Aruna.
@priyanka9 Unfortunately, I cannot reproduce the problem on my side using the attached project. Also, removeArgsInputData.docx
document used in your project does not exist. Please attach it here for testing.
Hi @alexey.noskov ,
Sorry for inconvenience can you please check latest attachment.
removeArgsInputData.docx (56.1 KB)
@priyanka9 Thank you for additional information. Please use FindReplaceDirection.BACKWARD
to avoid the problem:
options.setDirection(FindReplaceDirection.BACKWARD);
Ok let me check.Actually why we are using Forward direction is, Sometimes we need to get parent args styles and need to apply for child args. I will try to replace with BACKWARD direction will see if this styling scenario wont break we will use same.
Thanks,
Priyanka.
@priyanka9 The problem occurs because several occurrences are in the same Run and this causes the issue when process the document in forward direction, since processing of the first occurrence modifies the structure of the following content, this causes the problem upon processing the following occurrence.
Yeah @alexey.noskov I understood lets test once whether with this change i am able to achieve both styling and removing.
Thank You.
1 Like
Hi @alexey.noskov ,
Yeah BACKWARD direction fixed my removeArgs issue but i have observed below thing whilte testing this but anyway i fixed this by changing my code but just want to know why it is like this.
Scenario is I am using below methods to set my replacement value
setReplacement() and
ReplaceArgs.getMatchNode().getDocument().getRange().replace(arg,replacementText) but if i use both its throwing error while replacing value like no parent, .
I shouldnt use both ?
@priyanka9 You should not use both because ReplaceArgs.getMatchNode().getDocument().getRange().replace(arg,replacementText)
changes the structure of the document, that might cause problems in further processing of the current replacement process.
ho ok got it thank you @alexey.noskov.
1 Like
Hi @alexey.noskov ,
As i informed earlier now we are facing styling issue when we have data like below when we use BACKWARD
direction.
Scenario : When we are using find and replace BACKWARD
direction if we have data like below we are missing styling for child nodes.
Inputs :
input.docx (45.5 KB)
So when document have data like below (last section in input.docx)
attached some more samples for better understanding :
we are taking styling from {{ts.Annex1_TableStyle.docx}}
argument and storing it in Document object and applying that styling for all below arguments. This scenario is working fine for FARWARD
direction but when it comes to BACKWARD
direction since replacement will start from bottom and styling argument is there on top styling is not getting applied and styling completely gone when we tried to bring down that styling argument it worked as expected but that we cant do same in production data.
Actual output :
actual_output.docx (50.7 KB)
Expected output :
We are using insertDocument method to apply the style for child nodes.You can refer below attachment for that code.
aspose_test.zip (200.1 KB)
Can you please check and suggest us how we can fix this issue ?
Thanks,
Priyanka.
Hi,
Appreciate if we get quick response.
Thanks,
Priyanka.
@priyanka9 If it is critical to use forward direction, please try replacing all placeholder in your document so they were represented as a single Run node. Please try using the following code:
// Replace placeholders in the document tp make them to be represented as a single run.
FindReplaceOptions tmpOptions = new FindReplaceOptions();
tmpOptions.setUseSubstitutions(true);
baseAsposeDocument.getRange().replace(Pattern.compile("\\{\\{([^}]*)\\}\\}"), "$0", tmpOptions);
FindReplaceOptions options = new FindReplaceOptions();
options.setDirection(FindReplaceDirection.FORWARD);
// passing required args as per our project need u can ignore all these args
options.setReplacingCallback(new ReplaceEvaluatorTest());
baseAsposeDocument.getRange().replace(Pattern.compile("\\{\\{([^}]*)\\}\\}"), "", options);
FieldCollection fields = baseAsposeDocument.getRange().getFields();
baseAsposeDocument.save("C:\\Temp\\finalAsposeDoc.docx", SaveFormat.DOCX);
Hi @alexey.noskov ,
Thanks for your rly but as i said earlier we can not make any changes on the document data since that is coming from another system. Is there any other solution is there for this ?
@priyanka9 I do not propose to modify your documents. I propose a little preprocessing of the template before building the final report, i.e. the following three lines of code:
// Replace placeholders in the document tp make them to be represented as a single run.
FindReplaceOptions tmpOptions = new FindReplaceOptions();
tmpOptions.setUseSubstitutions(true);
baseAsposeDocument.getRange().replace(Pattern.compile("\\{\\{([^}]*)\\}\\}"), "$0", tmpOptions);
This code replaces the placeholders with themselves and after such preprocessing each placeholder will be represented as as single Run node.
ok thanks @alexey.noskov let me try and come back.
1 Like