Free Support Forum - aspose.com

Conflict between two types of bookmark

Hi Team,

There is a conflict between two types of bookmark code. If we use both the set of bookmark code the first set of bookmark overrides the second one.
The first type is
_((Paragraph) paragraph).getChildNodes(
** NodeType.SHAPE, true).clear();**
** Paragraph p = ((Paragraph) paragraph);**
** p.getChildNodes(NodeType.SHAPE, true).clear();**
** p.appendChild(new BookmarkStart(interimdoc,**
** “MyBookmark”));**
** Run run = new Run(interimdoc, “[” + name + “]”);**
** run.getFont().setSize(12);**
** run.getFont().setStrikeThrough(false);**
** run.getFont().setColor(Color.RED);**
** p.getRuns().add(run);**
** p.appendChild(new BookmarkEnd(interimdoc,**
** “MyBookmark”));**

and the second type is

_public static void ExtractImages(Document doc, String uc, DocumentBuilder builder) throws Exception
** {**


** int i = 1;**
** String bookmark = “bm_extract”;**
** for (Bookmark bm : doc.getRange().getBookmarks()) {**
** if (bm.getName().startsWith(“Bookmark”)) {**
** bm.getBookmarkEnd().getParentNode().insertBefore(new BookmarkEnd(doc, bm.getName()), bm.getBookmarkEnd().getParentNode().getFirstChild());**
** }**
** }**
** try{**
** doc.updatePageLayout();**
** for (Bookmark bm : doc.getRange().getBookmarks()) {**
** if (bm.getName().startsWith(“Bookmark”)) {**
** Node currentNode = bm.getBookmarkStart();**
** while (currentNode.getNodeType() != NodeType.SHAPE)**
** currentNode = currentNode.nextPreOrder(doc);**

** builder.moveTo(currentNode);**
** builder.startBookmark(bookmark + i);**
** builder.moveTo(bm.getBookmarkEnd());**
** builder.endBookmark(bookmark + i);**
** i++;**
** }**
** }**

** for (Bookmark bm : doc.getRange().getBookmarks()) {**
** if (bm.getName().startsWith(“Bookmark”)) {**
** bm.remove();**
** }**
** }**
** doc.updatePageLayout();**
** for (Bookmark bm : doc.getRange().getBookmarks())**
** {**
** if(bm.getName().startsWith(“bm_extract”))**
** {**
** ArrayList nodes = ExtractContentBetweenParagraphs((Paragraph)bm.getBookmarkStart().getParentNode(), (Paragraph) bm.getBookmarkEnd().getParentNode());**
** Document dstDoc = generateDocument(doc, nodes);**

** PageSetup sourcePageSetup = ((Paragraph)bm.getBookmarkStart().getParentNode()).getParentSection().getPageSetup();**
** dstDoc.getFirstSection().getPageSetup().setPaperSize(sourcePageSetup.getPaperSize());**
** dstDoc.getFirstSection().getPageSetup().setLeftMargin(sourcePageSetup.getLeftMargin());**
** dstDoc.getFirstSection().getPageSetup().setRightMargin(sourcePageSetup.getRightMargin());**

** dstDoc.updatePageLayout();**
** if(dstDoc.getLastSection().getBody().getLastParagraph().toString(SaveFormat.TEXT).trim().startsWith(“Fig”))**
** dstDoc.getLastSection().getBody().getLastParagraph().remove();**

** dstDoc.updatePageLayout();**
** while(dstDoc.getFirstSection().getBody().getFirstParagraph()!= null && dstDoc.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.SHAPE, true).getCount() == 0)**
** dstDoc.getFirstSection().getBody().getFirstParagraph().remove();**

** dstDoc.updatePageLayout();**
** if(dstDoc.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.SHAPE, true).getCount() > 0)**
** {**
** String filename = bm.getBookmarkEnd().getParentNode().toString(SaveFormat.TEXT);**
** if(filename.trim().length() > 0)**
** dstDoc.save(folderName + filename.substring(0, 7) + “_Other_Fig.docx”);**
** i++;**
** }**

** }**


** }**

** for (Bookmark bm : doc.getRange().getBookmarks()) {**
** if (bm.getName().startsWith(“bm_extract”)) {**
** String figText = bm.getBookmarkEnd().getParentNode().toString(SaveFormat.TEXT);**
** if(figText.trim().length() > 0)**
** bm.setText(""+figText.trim().substring(0, 7)+"" + ControlChar.PARAGRAPH_BREAK);**
** }**
** }**


** }**
** catch(Exception e)**
** {**


** }**
** }**

Kindly help please.

@Saranya_Sekar

Thanks for your inquiry. I am afraid I am not clear about your requirements; we will appreciate it if you please share some more details about your requirements. It will help us to understand your requirement and address it accordingly.

@tahir.manzoor
I am trying to have a single project with both these types of bookmark.The trouble is when extracting image with these type of bookmark the problem is only one bookmark is highlighted and the other is suppressed. I am eagerly waiting to get the output in both format. For current scenario the anchored image extraction bookmark is suppressed i.e. disappears when the other type of bookmark like Figcaptionabove code is present.I can’t be able to have two different kinds of bookmark in the output interim document generated after these two codes integrated.

Figcaptionbeside
private static void figureCaptionBeside(Document interimdoc) throws Exception
{
NodeCollection paragraphs = interimdoc.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph paragraph : (Iterable) paragraphs)
{
if(paragraph.toString(SaveFormat.TEXT).trim().startsWith(“Fig”) && paragraph.getParentNode() != null && paragraph.getParentNode().getNodeType() == NodeType.SHAPE) {
Document dstDoc = new Document();
NodeImporter importer = new NodeImporter(interimdoc, dstDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
Node importNode = importer.importNode(paragraph.getParentNode().getParentNode(), true);
dstDoc.getFirstSection().getBody().appendChild(importNode);

	        if (dstDoc.getChildNodes(NodeType.SHAPE, true).getCount() > 1) {
	            for (Shape shp : (Iterable<Shape>) dstDoc.getChildNodes(NodeType.SHAPE, true)) {
	                if (shp.toString(SaveFormat.TEXT).trim().contains("Fig"))
	                    shp.remove();
	            }
	            
	            String fname=folderName+"Fig"+i+".docx";
	            dstDoc.save(fname);
	            
	            String strval="SOSO_010_";      
                ((Paragraph) paragraph).getChildNodes(NodeType.SHAPE, true).clear();
                Paragraph p = ((Paragraph) paragraph);
                p.getChildNodes(NodeType.SHAPE, true).clear();
                p.appendChild(new BookmarkStart(interimdoc, "BesideBookMark"));
                Run run = new Run(interimdoc, "[" + strval+ "Fig_"+i + "]");
                run.getFont().setSize(14);
                run.getFont().setStrikeThrough(false);
                run.getFont().setColor(Color.RED);
                p.getRuns().add(run);
                p.appendChild(new BookmarkEnd(interimdoc,"BesideBookMark"));
                
	            importNode.remove();
				interimdoc.save(interim);
	            i++;
	        }
	    }
	}
}

AnchoredImage
public static void ExtractImages5(Document doc, String uc, DocumentBuilder builder) throws Exception
{
int i = 1;
String bookmark = “bm_extract”;
for (Bookmark bm : doc.getRange().getBookmarks()) {
if (bm.getName().startsWith(“Bookmark”)) {
bm.getBookmarkEnd().getParentNode().insertBefore(new BookmarkEnd(doc, bm.getName()), bm.getBookmarkEnd().getParentNode().getFirstChild());
}
}
doc.updatePageLayout();

    for (Bookmark bm : doc.getRange().getBookmarks()) {
        if (bm.getName().startsWith("Bookmark")) {
            Node currentNode = bm.getBookmarkStart();
            try{
            while (currentNode.getNodeType() != NodeType.SHAPE && currentNode.getNodeType() != NodeType.GROUP_SHAPE)
                currentNode = currentNode.nextPreOrder(doc);

            builder.moveTo(currentNode);
            builder.startBookmark(bookmark + i);
            builder.moveTo(bm.getBookmarkEnd());
            builder.endBookmark(bookmark + i);
            i++;
            }
            catch(Exception e){
            	
            }
        }
    }
    
    for (Bookmark bm : doc.getRange().getBookmarks()) {
        if (bm.getName().startsWith("Bookmark")) {
            bm.remove();
        }
    }
    doc.updatePageLayout();
    for (Bookmark bm : doc.getRange().getBookmarks())
    {
        if(bm.getName().startsWith("bm_extract"))
        {
           /* ArrayList nodes =  ExtractContents.extractContent(bm.getBookmarkStart(), bm.getBookmarkEnd(), true);
            Document dstDoc = ExtractContents.generateDocument(doc, nodes);*/
        	
       	 ArrayList nodes = ExtractContentBetweenParagraphs((Paragraph)bm.getBookmarkStart().getParentNode(), (Paragraph) bm.getBookmarkEnd().getParentNode());
            Document dstDoc = generateDocument(doc, nodes);


            PageSetup sourcePageSetup = ((Paragraph)bm.getBookmarkStart().getParentNode()).getParentSection().getPageSetup();
            dstDoc.getFirstSection().getPageSetup().setPaperSize(sourcePageSetup.getPaperSize());
            dstDoc.getFirstSection().getPageSetup().setLeftMargin(sourcePageSetup.getLeftMargin());
            dstDoc.getFirstSection().getPageSetup().setRightMargin(sourcePageSetup.getRightMargin());

            dstDoc.updatePageLayout();
            if(dstDoc.getLastSection().getBody().getLastParagraph().toString(SaveFormat.TEXT).trim().startsWith("Fig"))
                dstDoc.getLastSection().getBody().getLastParagraph().remove();

            dstDoc.updatePageLayout();
            while(dstDoc.getFirstSection().getBody().getFirstParagraph()!= null && dstDoc.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.SHAPE, true).getCount() == 0)
                dstDoc.getFirstSection().getBody().getFirstParagraph().remove();

            dstDoc.updatePageLayout();
            if(dstDoc.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.SHAPE, true).getCount() > 0)
            {
                String filename = bm.getBookmarkEnd().getParentNode().toString(SaveFormat.TEXT);
                if(filename.trim().length() > 0)
                    dstDoc.save(folderName + filename.substring(0, 7) + "_Other_Fig.docx");
                i++;
            }

        }
    }

    for (Bookmark bm : doc.getRange().getBookmarks()) {
        if (bm.getName().startsWith("bm_extract")) {
            String figText = bm.getBookmarkEnd().getParentNode().toString(SaveFormat.TEXT);
            if(figText.trim().length() > 0)
                bm.setText("<other-Fig>"+figText.trim().substring(0, 7)+"</other-Fig>" + ControlChar.PARAGRAPH_BREAK);
        }
    }
  }



public static void UseCase5(Document doc, DocumentBuilder builder) throws Exception
{
    int bookmark = 1;
    int i = 1;
    NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
    for (Paragraph  paragraph : (Iterable<Paragraph>) paragraphs)
    {
        if(paragraph.toString(SaveFormat.TEXT).trim().startsWith("Fig"))
        {
     //       System.out.println(paragraph.getText());
            Boolean bln = false;
            Node PreviousPara = paragraph.getPreviousSibling();
            while (PreviousPara != null && PreviousPara.getNodeType() == NodeType.PARAGRAPH
                    && (PreviousPara.toString(SaveFormat.TEXT).trim().length() == 0 ||
                       ((Paragraph)PreviousPara).getChildNodes(NodeType.SHAPE, true).getCount() > 0)
            )
            {
                PreviousPara = PreviousPara.getPreviousSibling();
            }

            if(PreviousPara == null)
            {
                builder.moveToDocumentStart();
                builder.insertParagraph();
                builder.startBookmark("Bookmark" + bookmark);
                //builder.moveToParagraph(paragraphs.indexOf(paragraph), 0);
                builder.moveTo(paragraph);
                builder.endBookmark("Bookmark" + bookmark);
                bookmark++;
            }
            else
            if(PreviousPara.getNodeType() == NodeType.PARAGRAPH)
            {
                Node node = ((Paragraph)PreviousPara).getParentNode().insertBefore(new Paragraph(doc), PreviousPara);
                builder.moveTo(node);
                builder.startBookmark("BookmarkUC1" + bookmark);
                builder.moveTo(paragraph);
                builder.endBookmark("BookmarkUC1" + bookmark);
                bookmark++;
            }
        }
    }
}

@Saranya_Sekar

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Please share some detail about your two use cases.
  • Please share your input Word document.
  • Please attach the output Word files that shows the undesired behavior.
  • Please attach the expected output Word files that shows the desired behavior.
  • Please share the screenshots of problematic sections of output document.
  • Please create a simple Java application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

@tahir.manzoor
Can I get bookmark changed like this for the anchored images extraction. Sample anchored image document is Anchored_Images (2).zip (2.6 MB)
Output to be extracted is Anchored_Images_output.zip (2.6 MB)
and the interim to be generated is Anchored_Images_Interim.zip (18.6 KB)

Sample input bookmark format:

String strval=“SOSO_010_”;
((Paragraph) scheduledpara).getChildNodes(NodeType.SHAPE, true).clear();
Paragraph p = ((Paragraph) scheduledpara);
p.getChildNodes(NodeType.SHAPE, true).clear();
p.appendChild(new BookmarkStart(interimdoc, “SchemaBookMark”));
Run run = new Run(interimdoc, “[” + strval+ name + “]”);
run.getFont().setSize(14);
run.getFont().setStrikeThrough(false);
run.getFont().setColor(Color.RED);
p.getRuns().add(run);
p.appendChild(new BookmarkEnd(interimdoc,“SchemaBookMark”));

I want the below Bookmark to be converted as above bookmark

public  static void ExtractImages5(Document doc, String uc, DocumentBuilder builder) throws Exception
{
    int i = 1;
    String bookmark = "bm_extract";
    for (Bookmark bm : doc.getRange().getBookmarks()) {
        if (bm.getName().startsWith("Bookmark")) {
            bm.getBookmarkEnd().getParentNode().insertBefore(new BookmarkEnd(doc, bm.getName()), bm.getBookmarkEnd().getParentNode().getFirstChild());
        }
    }
    doc.updatePageLayout();
   
    for (Bookmark bm : doc.getRange().getBookmarks()) {
        if (bm.getName().startsWith("Bookmark")) {
            Node currentNode = bm.getBookmarkStart();
            try{
            while (currentNode.getNodeType() != NodeType.SHAPE && currentNode.getNodeType() != NodeType.GROUP_SHAPE)
                currentNode = currentNode.nextPreOrder(doc);

            builder.moveTo(currentNode);
            builder.startBookmark(bookmark + i);
            builder.moveTo(bm.getBookmarkEnd());
            builder.endBookmark(bookmark + i);
            i++;
            }
            catch(Exception e){
            	
            }
        }
    }
    
    for (Bookmark bm : doc.getRange().getBookmarks()) {
        if (bm.getName().startsWith("Bookmark")) {
            bm.remove();
        }
    }
    doc.updatePageLayout();
    for (Bookmark bm : doc.getRange().getBookmarks())
    {
        if(bm.getName().startsWith("bm_extract"))
        {
           /* ArrayList nodes =  ExtractContents.extractContent(bm.getBookmarkStart(), bm.getBookmarkEnd(), true);
            Document dstDoc = ExtractContents.generateDocument(doc, nodes);*/
        	
       	 ArrayList nodes = ExtractContentBetweenParagraphs((Paragraph)bm.getBookmarkStart().getParentNode(), (Paragraph) bm.getBookmarkEnd().getParentNode());
            Document dstDoc = generateDocument(doc, nodes);


            PageSetup sourcePageSetup = ((Paragraph)bm.getBookmarkStart().getParentNode()).getParentSection().getPageSetup();
            dstDoc.getFirstSection().getPageSetup().setPaperSize(sourcePageSetup.getPaperSize());
            dstDoc.getFirstSection().getPageSetup().setLeftMargin(sourcePageSetup.getLeftMargin());
            dstDoc.getFirstSection().getPageSetup().setRightMargin(sourcePageSetup.getRightMargin());

            dstDoc.updatePageLayout();
            if(dstDoc.getLastSection().getBody().getLastParagraph().toString(SaveFormat.TEXT).trim().startsWith("Fig"))
                dstDoc.getLastSection().getBody().getLastParagraph().remove();

            dstDoc.updatePageLayout();
            while(dstDoc.getFirstSection().getBody().getFirstParagraph()!= null && dstDoc.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.SHAPE, true).getCount() == 0)
                dstDoc.getFirstSection().getBody().getFirstParagraph().remove();

            dstDoc.updatePageLayout();
            if(dstDoc.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.SHAPE, true).getCount() > 0)
            {
                String filename = bm.getBookmarkEnd().getParentNode().toString(SaveFormat.TEXT);
                if(filename.trim().length() > 0)
                    dstDoc.save(folderName + filename.substring(0, 7) + "_Other_Fig.docx");
                i++;
            }

        }
    }

    for (Bookmark bm : doc.getRange().getBookmarks()) {
        if (bm.getName().startsWith("bm_extract")) {
            String figText = bm.getBookmarkEnd().getParentNode().toString(SaveFormat.TEXT);
            if(figText.trim().length() > 0)
                bm.setText("<other-Fig>"+figText.trim().substring(0, 7)+"</other-Fig>" + ControlChar.PARAGRAPH_BREAK);
        }
    }
  }



public static void UseCase5(Document doc, DocumentBuilder builder) throws Exception
{
    int bookmark = 1;
    int i = 1;
    NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
    for (Paragraph  paragraph : (Iterable<Paragraph>) paragraphs)
    {
        if(paragraph.toString(SaveFormat.TEXT).trim().startsWith("Fig"))
        {
     //       System.out.println(paragraph.getText());
            Boolean bln = false;
            Node PreviousPara = paragraph.getPreviousSibling();
            while (PreviousPara != null && PreviousPara.getNodeType() == NodeType.PARAGRAPH
                    && (PreviousPara.toString(SaveFormat.TEXT).trim().length() == 0 ||
                       ((Paragraph)PreviousPara).getChildNodes(NodeType.SHAPE, true).getCount() > 0)
            )
            {
                PreviousPara = PreviousPara.getPreviousSibling();
            }

            if(PreviousPara == null)
            {
                builder.moveToDocumentStart();
                builder.insertParagraph();
                builder.startBookmark("Bookmark" + bookmark);
                //builder.moveToParagraph(paragraphs.indexOf(paragraph), 0);
                builder.moveTo(paragraph);
                builder.endBookmark("Bookmark" + bookmark);
                bookmark++;
            }
            else
            if(PreviousPara.getNodeType() == NodeType.PARAGRAPH)
            {
                Node node = ((Paragraph)PreviousPara).getParentNode().insertBefore(new Paragraph(doc), PreviousPara);
                builder.moveTo(node);
                builder.startBookmark("BookmarkUC1" + bookmark);
                builder.moveTo(paragraph);
                builder.endBookmark("BookmarkUC1" + bookmark);
                bookmark++;
            }
        }
    }
}

@Saranya_Sekar

Thanks for your inquiry. Please replace the following if block with second one in your code. Hope this helps you.

if(figText.trim().length() > 0)
    bm.setText("<Fig>"+figText.trim().substring(0, 7)+"</Fig>" + ControlChar.PARAGRAPH_BREAK);

    if(figText.trim().length() > 0)
    {
        bm.setText("<Fig>"+figText.trim().substring(0, 7)+"</Fig>" + ControlChar.PARAGRAPH_BREAK);
        Node scheduledpara = bm.getBookmarkStart().getParentNode();

        Paragraph p = ((Paragraph) scheduledpara);
        p.getChildNodes(NodeType.SHAPE, true).clear();
        p.appendChild(new BookmarkStart(interimdoc, "SchemaBookMark"));
        Run run = new Run(interimdoc, "[" + strval+ name + "]");
    run.getFont().setSize(14);
    run.getFont().setStrikeThrough(false);
    run.getFont().setColor(Color.RED);
    p.getRuns().add(run);
}

Can you please provide the complete set of working code with output generated.Input document is Article reviewed [13-05-2017]_test.zip (3.1 MB)

@Saranya_Sekar

Thanks for your inquiry. We already shared the working code example along with output documents with you. Could you please share what issue you are facing? Please also share your expected output documents.