How to replace the text in clonedSection

Cloning of the section of the document is working fine, but I want to replace some text in the cloned section everytime before I added to the document. How to do that?

--------------------

Document doc =

new Document(“C:\Templates\template.docx”);

Section cloneSection = doc.getSections().get(1).deepClone();

doc.getSections().add(cloneSection);

Section cloneSection1 = doc.getSections().get(1).deepClone();

doc.getSections().add(cloneSection1);

Section cloneSection2 = doc.getSections().get(1).deepClone();

doc.getSections().add(cloneSection2);

Section cloneSection3 = doc.getSections().get(1).deepClone();

doc.getSections().add(cloneSection3);

Section cloneSection4 = doc.getSections().get(1).deepClone();

doc.getSections().add(cloneSection4);

Section cloneSection5 = doc.getSections().get(1).deepClone();

doc.getSections().add(cloneSection5);

Hi Koteswara,


Thanks for your inquiry. Please try run the following code to be able to achieve what you’re looking for:
Section cloneSection = doc.getSections().get(1).deepClone();
cloneSection.getRange().replace(“old text”, “new text”, false, true);
doc.getSections().add(cloneSection);

Section cloneSection1 = doc.getSections().get(1).deepClone();
cloneSection1.getRange().replace(“old text”, “new text”, false, true);
doc.getSections().add(cloneSection1);

Section cloneSection2 = doc.getSections().get(1).deepClone();
.
.

I hope, this helps.

Moreover, I would suggest you please read the following article to learn “Find and Replace” functionality of Aspose.Words:
http://www.aspose.com/docs/display/wordsjava/Find+and+Replace+Overview

Best regards,

I am not able to replace the following

or [Group Name]

I tried doing this, it's not working

cloneSection.getRange().replace("", "HylandCroy", false, true);

cloneSection.getRange().replace("[Group Name]","PagodaWay" false, true);

If I try following way, it is working. Basically letters like '[' or ']' or '<' or '>' are not working with this replacement function. WHY?

cloneSection.getRange().replace("Group Name", "HylandCroy", false, true);

cloneSection.getRange().replace("Group Name","PagodaWay" false, true);

Hi Koteswara,


Thanks for the additional information. In this case, please pass a regular expression pattern which will be used to find matches as follows:

cloneSection.getRange().replace(Pattern.compile("\<Group Name\>"), “HylandCroy”);
cloneSection.getRange().replace(Pattern.compile("\[Group Name\]"), “HylandCroy”);

I hope, this helps.

Best regards,

My document is having 10 sections, If I clone the section2; It is appending the cloning section at the end of the document that is after the section 10. How to avoid this type of issue? I want the cloning follows immediately after the section2.

Hi Koteswara,


Thanks for your inquiry. You can insert a section in document into the sections collection at the specified index using Sections.Insert method. Moreover, please attach the following resources here for testing:

  1. Your input Word document you’re getting this problem with.
  2. The Aspose.Words generated output document that shows the undesired behavior.
  3. Your expected document. You can use Microsoft Word to generate this document which shows the correct formatting.
  4. Please create a standalone/runnable simple application (for example a Console Application project) that helps me reproduce your problem on my end and attach it here for testing

As soon as you get these pieces of information ready for me, I’ll start investigation into your issue and provide you more information.

Best regards,

My document:

Section1: [Facility Name][Facility Number]

Section2:[Facility Address1][Facility Address2]

Section3:$$[Emission unit1][Release Point1]

section4:[Permit Number][Application Number]



To know which section to clone I will have symbol before the section starts. I want to remove that symbol before cloning. Here I am using $$ as a symbol for cloning.



Here I want to repeat section three with different emission units and release points. Section3 cloning should follow section3 not at the end of section4.



Kindly help me here

Hi Koteswara,


Thanks for the additional information and sorry for the delayed response. Please find attached sample input/output documents and use the following code to achieve what you’re looking for.
Document doc = new Document(“C:\Temp\input.docx”);

NodeCollection runs = doc.getChildNodes(NodeType.RUN, true);
for (Run run : (Iterable<Run>)runs){
if (run.toString(SaveFormat.TEXT).contains("$$")){
run.setText(run.getText().replace("$$",""));
Section srcSection = (Section)run.getAncestor(NodeType.SECTION);
if (srcSection != null){
srcSection.getParentNode().insertAfter((Section)srcSection.deepClone(true), srcSection);
}
}
}

doc.save(“C:\Temp\output.docx”);

I hope, this helps.

Best regards,

I want to Open the doc file without saving in a particular location. How to do that?

Instead of doc.save, do you have something like doc.open()?

Thanks,
Koti

Hi Koteswara,


Thanks for your inquiry. Sure, you can get the InputStream from Document object, please try using the following code snippet:
Document doc = new Document(“c:\temp\some-document.docx”);

ByteArrayOutputStream baos

= new ByteArrayOutputStream();
doc.save(baos, SaveFormat.DOCX);

InputStream inStream

= new ByteArrayInputStream(baos.toByteArray());
Document doc1
= new Document(inStream);

I hope, this helps.

Best regards,

Hi I want to generate and open the document file on fly by clicking on one button. Can you please send me the code for that?

Hi Koteswara,

Thanks for your inquiry. Please refer to the following articles:

Best regards,