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

Free Support Forum - aspose.com

Replace bookmarks add a several section break continous

Hi,
I try to replace some bookmark in a doc with others docs. So when the bookmark is replaced with a good document, a several (2 section continous) break section continous are added after the inserted document. And for each document inserted i will keep the header and footers for each document inseted.
After theses action, for each document inserted, the header for the first page is lost.

can you help me please.


Thanks for your responsiveness,

Best Regards,
Patrice ANTOINE

Hi Patrice,


Thanks for your inquiry. Please attach the following resources here for testing.

  1. Your main input document (that contains the Bookmarks)
  2. Your other document(s) which are being inserted at bookmarked places in the main document.
  3. Your output document that is generated by Aspose.Words which shows the undesired behaviour.
  4. Your target document showing the desired behaviour (You can use Microsoft Word to create this document)
  5. Your piece of code to reproduce the same problematic output document on my side (refer to point 3).

I will then investigate the issue on my side and provide you more information.

Best regards,

Hi Awais,
Thank you for your responsiveness,
I join you all files asked and the code used to replace the contents of a bookmark in a file :

private void mergePartInBookmark(Part part, Document document, boolean mergeStyles, boolean newBookmark)
throws Exception
{
// merge the part by replacing the corresponding bookmark
Document documentAdd = loadPart(part);
// Create Documentbuilder and move its cursor to the bookmark.
DocumentBuilder builder = new DocumentBuilder(document);
builder.moveToBookmark(part.getBookmarkID(), false, true);

// On verifie si on est dans le cas suivant (insertion de plusieurs parts dans le même bookmark) :
// Si on insere une part en paysage apres une part en portrait dans un
// template en portrait.
int partOrientation = documentAdd.getFirstSection().getPageSetup().getOrientation();
boolean breakPage = false;
if (!newBookmark)
{
int bkOrientation = builder.getPageSetup().getOrientation();
Node node = builder.getCurrentSection().getNextSibling();
if (node != null)
{
if (node instanceof Section)
{
Section nextSection = (Section)node;
int nextOrientation = nextSection.getPageSetup().getOrientation();
if (nextOrientation == bkOrientation)
{
if (partOrientation != nextOrientation)
{
breakPage = true;
}
}
}
}
}

// Get the current section, the source document will be insert after it.
Section insertAfter = builder.getCurrentSection();

// Insert a section break.
builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
// Insert the source document.
// Make the document appear straight after the destination documents content.
documentAdd.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);

if (partOrientation == builder.getCurrentSection().getPageSetup().getOrientation())
{
// Use for insert a document which has different page settings
documentAdd.getFirstSection().getPageSetup()
.setPageWidth(builder.getCurrentSection().getPageSetup().getPageWidth());
documentAdd.getFirstSection().getPageSetup()
.setPageHeight(builder.getCurrentSection().getPageSetup().getPageHeight());
}

documentAdd.getFirstSection().getHeadersFooters().linkToPrevious(true);

if (mergeStyles)
{
beforeAppend(document, documentAdd);
}

deleteBookmark(document, part.getBookmarkID(), true);
if (!part.getRemoveBookmark())
{
// Dans le cas ou on ne veut pas supprimer le bookmark, on l’insere
// dans la part qu’on insère pour qu’il soit ainsi dans la meme section.
DocumentBuilder builderAdd = new DocumentBuilder(documentAdd);
builderAdd.moveToDocumentEnd();
builderAdd.startBookmark(part.getBookmarkID());
builderAdd.endBookmark(part.getBookmarkID());
}
else
{
if (!newBookmark)
{
// Insertion de plusieurs parts dans le meme bookmark
// On doit changer l’orientation du bookmark avant d’inserer
// la 2eme part car sinon un saut de section (page blange)
// sera cree automatiquement. Ceci n’est pas fait dans le cas ou
// on insere une part paysage apres une part portrait dans un doc portrait
if (!breakPage)
{
builder.getPageSetup().setOrientation(partOrientation);
}
}
}
insertInDocument(insertAfter, documentAdd, mergeStyles);
}
private void insertInDocument(Section insertAfter, Document srcDoc, boolean mergeStyle)
{
NodeImporter importer = null;
if (mergeStyle)
{
// Create NodeImporter that will hep us to copy content from one document to another.
importer = new NodeImporter(srcDoc, insertAfter.getDocument(), ImportFormatMode.USE_DESTINATION_STYLES);
}
else
{
// Create NodeImporter that will hep us to copy content from one document to another.
importer = new NodeImporter(srcDoc, insertAfter.getDocument(), ImportFormatMode.KEEP_SOURCE_FORMATTING);
}
Node insertAfterInternal = insertAfter;

// Loop through all sections in the source document and insert them after the section in the destination document.
for (Section sect : srcDoc.getSections())
{
// Import section.
Node importedSection = importer.importNode(sect, true);

// Insert it into the document.
insertAfterInternal.getParentNode().insertAfter(importedSection, insertAfterInternal);

// Reset insertAfterInternal.
insertAfterInternal = importedSection;
}
}

Best regards,

Hi Patrice,


Thanks for the additional information. I am checking with this scenario and will get back to you soon.

Best regards,

Hi Patrice,


Thank you for being patient. Unfortunately, at the moment, we’re not able to reproduce this issue on our end because some bits of code are missing from your attached code e.g. beforeAppend and deleteBookmark methods are not available to us. Please create a standalone simple application (for example a Java Console Application Project) that helps us reproduce the same problem on our end and attach it here for testing. As soon as you get this Java Application ready for us, we’ll start our investigation into your issue. Thanks for your cooperation.

Best regards,