Change bookmark format

Hi Team,

I want bookmark to be converted in the following format for inline images.Input document is inline_Input.zip (5.1 MB) and the output generated is inline_Input_output.zip (179.1 KB)
and the interim is inline_Input_Interim.zip (1.5 MB)

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

Inline image to be converted as above bookmark

private static void inlineImagesExtraction(Document interimdoc) throws Exception
{
Document doc = interimdoc;
DocumentBuilder builder = new DocumentBuilder(doc);
try{
int i = 1;
for (Shape shape : (Iterable) doc.getChildNodes(NodeType.SHAPE, true)) {

	    if (shape.getParentParagraph() == null ||
	            shape.getParentParagraph().getChildNodes().getCount() < 9 ||
	            shape.getAncestor(NodeType.TABLE) != null ||
	            shape.getShapeType() == ShapeType.TEXT_BOX) {

	    } else {
	        Document newDoc = (Document) doc.deepClone(false);
	        newDoc.removeAllChildren();
	        newDoc.ensureMinimum();

	        Shape importedShape = (Shape) newDoc.importNode(shape, true);
	        importedShape.setWrapType(WrapType.NONE);
	        importedShape.setHorizontalAlignment(HorizontalAlignment.CENTER);
	        importedShape.setVerticalAlignment(VerticalAlignment.CENTER);
	        importedShape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
	        importedShape.setRelativeHorizontalPosition(RelativeVerticalPosition.PAGE);

	        newDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);

	        builder.moveTo(shape);
	        builder.getFont().setColor(Color.red);
	        builder.write(" <other-Fig>Other_Fig" + i + "</other-Fig> ");
	        
	        shape.remove();

	        newDoc.save(folderName+"Other_Fig" + i + ".docx");
	    }
	    i++;
	}

	doc.save(interim);
	}
	catch(Exception e){
	}
}

@Saranya_Sekar

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

Can I yo please provide the complete set of working code with output generated.inline_Input.zip (5.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.

@tahir.manzoor
Thanks for the reply. For inline image there is no if(figText.trim().length() > 0) bm.setText("<Fig>"+figText.trim().substring(0, 7)+"</Fig>" + ControlChar.PARAGRAPH_BREAK);
statement how to place the bookmark in this case.

@Saranya_Sekar

Thanks for your inquiry. Please ZIP and attach your expected output documents. We will then provide you more information about your query.

@tahir.manzoor
Input sample is inline_Input.zip (5.1 MB) and the interim is inline_Input_Interim.zip (1.5 MB) and the expected output is inline_Input_output.zip (179.1 KB) For inline image there is no if(figText.trim().length() > 0) bm.setText("<Fig>"+figText.trim().substring(0, 7)+"</Fig>" + ControlChar.PARAGRAPH_BREAK);
statement how to place the bookmark in this case.

private static void inlineImagesExtraction(Document interimdoc) throws Exception
{
	Document doc = interimdoc;
	DocumentBuilder builder = new DocumentBuilder(doc);

try{
int i = 1;
for (Shape shape : (Iterable) doc.getChildNodes(NodeType.SHAPE, true)) {

	    if (shape.getParentParagraph() == null ||
	            shape.getParentParagraph().getChildNodes().getCount() < 9 ||
	            shape.getAncestor(NodeType.TABLE) != null ||
	            shape.getShapeType() == ShapeType.TEXT_BOX) {

	    } else {
	        Document newDoc = (Document) doc.deepClone(false);
	        newDoc.removeAllChildren();
	        newDoc.ensureMinimum();

	        Shape importedShape = (Shape) newDoc.importNode(shape, true);
	        importedShape.setWrapType(WrapType.NONE);
	        importedShape.setHorizontalAlignment(HorizontalAlignment.CENTER);
	        importedShape.setVerticalAlignment(VerticalAlignment.CENTER);
	        importedShape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
	        importedShape.setRelativeHorizontalPosition(RelativeVerticalPosition.PAGE);

	        newDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);

	        builder.moveTo(shape);
	        builder.getFont().setColor(Color.red);
	        builder.write(" <other-Fig>Other_Fig" + i + "</other-Fig> ");

	        shape.remove();

	        newDoc.save(folderName+"Other_Fig" + i + ".docx");
	    }
	    i++;
	}

	doc.save(interim);
	}
	catch(Exception e){
	}
}

@Saranya_Sekar

Thanks for sharing the detail. Please use the following method to get the desired output.

In your input document, there are two shapes that are not inline. So, they will not be extracted in output document. Please set them as inline shape to extract them.

private static void inlineImagesExtraction(Document interimdoc) throws Exception
{
    Document doc = interimdoc;
    DocumentBuilder builder = new DocumentBuilder(doc);
    try{
        int i = 1;
        for (Shape shape : (Iterable<Shape>) doc.getChildNodes(NodeType.SHAPE, true)) {

            if (shape.getWrapType() == WrapType.INLINE && shape.getParentParagraph().getRuns().getCount() > 0
                    && shape.getParentParagraph().toString(SaveFormat.TEXT).trim().length() > 0 && shape.getOleFormat() == null
                    && shape.getParentParagraph().toString(SaveFormat.TEXT).contains("(a)") == false
                    && shape.getParentParagraph().toString(SaveFormat.TEXT).contains("(b)") == false
                    && shape.getParentParagraph().toString(SaveFormat.TEXT).contains("(c)") == false
                    && shape.getParentParagraph().getNextSibling() != null && shape.getParentParagraph().getNextSibling().toString(SaveFormat.TEXT).contains("(a)") == false
                    && shape.getParentParagraph().getNextSibling() != null && shape.getParentParagraph().getNextSibling().toString(SaveFormat.TEXT).contains("(b)") == false
                    && shape.getParentParagraph().getAncestor(NodeType.TABLE) == null
                    )
            {
                Document newDoc = (Document) doc.deepClone(false);
                newDoc.removeAllChildren();
                newDoc.ensureMinimum();

                Shape importedShape = (Shape) newDoc.importNode(shape, true);
                importedShape.setWrapType(WrapType.NONE);
                importedShape.setHorizontalAlignment(HorizontalAlignment.CENTER);
                importedShape.setVerticalAlignment(VerticalAlignment.CENTER);
                importedShape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
                importedShape.setRelativeHorizontalPosition(RelativeVerticalPosition.PAGE);

                newDoc.getFirstSection().getBody().getFirstParagraph().appendChild(importedShape);

                builder.moveTo(shape);
                builder.getFont().setColor(Color.red);
                builder.write(" <other-Fig>Other_Fig" + i + "</other-Fig> ");

                shape.remove();

                newDoc.save(MyDir+"Other_Fig" + i + ".docx");
            }
            i++;
        }

        doc.save(MyDir + "18.11-interim.docx");
    }
    catch(Exception e){
        e.printStackTrace();
    }
}

@tahir.manzoor
Can I get the bookmark modified like this

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

@Saranya_Sekar

Thanks for your inquiry. Yes, you can achieve this requirement using following code snippet.

builder.moveTo(shape);
builder.getFont().setColor(Color.red);
builder.getFont().setSize(14);
builder.getFont().setStrikeThrough(false);
builder.startBookmark("SchemaBookMark");
builder.write(" <other-Fig>Other_Fig" + i + "</other-Fig> ");
builder.endBookmark("SchemaBookMark");

shape.remove();

newDoc.save(MyDir+"Other_Fig" + i + ".docx");

We suggest you please read the following article.
Use DocumentBuilder to Insert Document Elements
Using DocumentBuilder to Modify a Document