Copy Content from Bookmark into same Document

I know there are a lot of threats in the forum for my problem, but I can’t find the right one for me

i have got a Bookmark with some text and two tables, i have to copy the whole content from bookmarkStart to bookmarkEnd and paste it after BookmarkEnd.

I need it for an Master Detail Report.
I can’t use section for it.

thx

Hi Michael,

Thanks for your inquiry. I think it is better to use sections instead of bookmarks, or section with bookmark as a marker. You can use code like the following to copy/paste the particular section:

// Open document.

Document doc = new Document(“C:\Temp\in.doc”);

// Get section, which should be copied.

Section sect = doc.getSections().get(0);

// Copy section few times

for (int i = 0; i < 5; i++)

{

// Clone section

Section clone = (Section)sect.deepClone(true);

// Insert cloned section after the original section.

sect.getParentNode().insertAfter(clone, sect);

}

// Save ouytput document

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

Hope this helps.

Best regards.

thx,

but if i use section in word documents i will lost my page header and footer with page counter …
so i can’t work with section to solve this problem?
or do I something mistakes with the sections in word

thx

Hi

Thanks for your inquiry. You will not lost headers/footers and page numbering if you use Sections. Please let me know if you need more assistance in achieving this, I will be glad to help you. Please attach sample template here and expected output, I will try modifying the template and provide you sample code.

Best regards.

Ok … with the section continuos it doesn’t lost my HEADER and FOOTER … if I make a section breake with page brake i will lost my footer and continous page count. But i work around with page prake into the section.

But there is now a other problem.

After the mergeField.exectue the srcSection.getParentNode is NULL …

without the mergeField.execute the copy and paste works without errors

i make something like that

String[] fields = new String[] { “DEQU_DENOMINAZIONE”, “COIN_DENOMINAZIONE”, “PALA_DENOMINAZIONE” };
Section srcSection = docDE.getSections().get(1);

Section clone = srcSection.deepClone();

for (int i = 0; i < allegato2_3List.size(); i++) {
if (i > 0) {
srcSection.getParentNode().insertAfter(clone, srcSection);
Section clone = srcSection.deepClone();
}

Object[] fieldValues = new Object[] { allegato2_3List.get(i).getDequ_Denominazione(), allegato2_3List.get (i).getCoin_Denominazione(), allegato2_3List.get(i).getPala_Denominazione() };
docDE.getMailMerge().execute(fields, fieldValues);
}

Hi Michael,

Thanks for your inquiry. You can try using the following code in your case:

// Open document.

Document doc = new Document(“C:\Temp\in.doc”);

String[] fields = new String[] { “DEQU_DENOMINAZIONE”, “COIN_DENOMINAZIONE”, “PALA_DENOMINAZIONE” };

Section srcSection = doc.getSections().get(1);

Section clone = (Section)srcSection.deepClone(true);

for (int i = 0; i < 10; i++)

{

if (i > 0)

doc.getSections().insert(2, clone.deepClone(true));

Object[] fieldValues = new Object[] { “DEQU_DENOMINAZIONE”, “COIN_DENOMINAZIONE”, “PALA_DENOMINAZIONE” };

doc.getMailMerge().execute(fields, fieldValues);

}

// Save ouytput document

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

Regarding headers/footers, I think, you can just link headers/footer to previous section. In this case the section will have the same header and footer as the previous one has.

Hope this helps.

Best regards.

thx, that works better then my workaround …

a question, why if i append a Node to a Table i can’t append an other Node, befor I have to get the “new table” for example

Table tableAllegato2_3 = docDE.getSections().get(i + 1).getBody().getTables().get(0);
tableAllegato2_3.appendChild(rowDetail.deepClone(true));

tableAllegato2_3 = docDE.getSections().get(i + 1).getBody().getTables().get(0);
tableAllegato2_3.appendChild(rowDetail.deepClone(true));

the same thing with the rowDetail, if I use your solution with the rowDetail.deepClone after then i can always use rowDetail. If I do not use deepClone but append the instance of rowDetail, I can’t append it a second time.

that’s not a problem but i’m wondering about it, why ASPOSE works so?

and a other question

is there a possibility to pass on MergeField a special character to make a new Line …

like that “First Line\nSecond Line”

thx

Hi Michael,

Thanks for your inquiry.

  1. When you insert some note in the document it is cut from its old location and inserted at new. That is why when you just call InsertBefore method several times with the same parameters you will not get several copies of the node.

  2. You can try using CobtrolChar.Line_Break.

Object[] fieldValues = new Object[] { “test” + ControlChar.LINE_BREAK + “test”, “COIN_DENOMINAZIONE”, “PALA_DENOMINAZIONE” };

Hope this helps.

Best regards.