@tahir.manzoor
Bookmark from labelled image is overlapping bookmark from anchored images. How to resolve the issue.How about using this bookmark
((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, "<Fig>Numbered_Figure</Fig>");
run.getFont().setColor(Color.RED);
p.getRuns().add(run);
p.appendChild(new BookmarkEnd(interimdoc, "MyBookmark"));
The code I am using is as follow.
UseCase5(doc, builder);
ExtractImages5(doc, “uc5”, builder);
doc.save(interim);
pageproperty(interimdoc);
numberedImagesExtraction(interimdoc);
inlineImagesExtraction(interimdoc);
UseCase1(doc, builder);
ExtractImages(doc, "uc1", builder);
UseCase2(doc, builder);
ExtractImages(doc, "uc2", builder);
//tableImagesExtraction(interimdoc);
UseCase3(doc, builder);
ExtractImages(doc, "uc2", builder);
doc.save(interim);
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);
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) + "_anchored.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("<Anchored-Fig>"+figText.trim().substring(0, 7)+"</Anchored-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++;
}
}
}
}
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) + "_label_Image.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("<Label-Fig>"+figText.trim().substring(0, 7)+"</Label-Fig>" + ControlChar.PARAGRAPH_BREAK);
}
}
}
catch(Exception e)
{
}
}