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

Free Support Forum - aspose.com

Change in Bookmark format

Hi Team,

Kindly help me to convert bookmark of labelled image extraction.Sample input document is Multiple_Label.zip (2.3 MB) Interim to be generated is Multiple_Label_Interim.zip (13.8 KB)
and the output format is Multiple_Label _output.zip (1.9 MB)

Sample Input bookmark format

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”));

Labelled Images bookmark to be converted as above bookmark

public static void UseCase1(Document doc, DocumentBuilder builder) throws Exception
{
int bookmark = 1;
int i = 1;
NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph paragraph : (Iterable) paragraphs)
{
if(paragraph.toString(SaveFormat.TEXT).trim().startsWith(“Fig”))
{
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();
bln = true;
}

            if(!bln)
                continue;

            if(PreviousPara == null)
            {
                builder.moveToDocumentStart();
                builder.insertParagraph();
                builder.startBookmark("Bookmark" + bookmark);
                builder.moveToParagraph(paragraphs.indexOf(paragraph), 0);
                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++;
            }
        }
    }
}

//Use case 2
public static void UseCase2(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"))
        {
            Boolean bln = false;
            Node PreviousPara = paragraph.getPreviousSibling();
            while (PreviousPara != null &&
                    (PreviousPara.toString(SaveFormat.TEXT).trim().length() == 0 ||
                        (
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(a)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(b)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(c)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(d)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().startsWith("(Fig"))
                        )
                    )
            {
                PreviousPara = PreviousPara.getPreviousSibling();
                bln = true;
            }

            if(!bln)
                continue;

            if(PreviousPara == null)
            {
                builder.moveToDocumentStart();
                builder.insertParagraph();
                builder.startBookmark("Bookmark" + bookmark);
                builder.moveToParagraph(paragraphs.indexOf(paragraph), 0);
                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++;
            }
        }
    }
}
public static void UseCase3(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"))
        {
            Boolean bln = false;
            Node PreviousPara = paragraph.getPreviousSibling();
            while (PreviousPara != null &&
                    (PreviousPara.toString(SaveFormat.TEXT).trim().length() == 0 ||
                        (
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(a)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(b)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(c)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().contains("(d)") ||
                                PreviousPara.toString(SaveFormat.TEXT).trim().startsWith("(Fig"))
                        )
                    )
            {
                PreviousPara = PreviousPara.getPreviousSibling();
                bln = true;
            }

            if(!bln)
                continue;

            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("Bookmark" + bookmark);
                builder.moveTo(paragraph);
                builder.endBookmark("Bookmark" + bookmark);
                bookmark++;
            }
        }
    }
}
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("<other-Fig>"+figText.trim().substring(0, 7)+"</other-Fig>" + ControlChar.PARAGRAPH_BREAK);
        }
    }
    }
    catch(Exception e)
    {
    	
    }
}

@Saranya_Sekar

Thanks for your inquiry. Please check my reply in your other thread here:

@tahir.manzoor
Can you please provide the complete working set of code with output generated.input document is Multiple_Label.zip (2.3 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.