I am looking to add bookmark to this block of working code.The bookmark should be placed in the location of image removed and the bookmark placed in that location. Input document is Numbered_Image_Input.zip (1.0 MB)
and the expected interim document to be generated is Numbered_Image_interim.zip (15.1 KB)
The code is attached here.
private static void numberedImagesExtraction(Document interimdoc) {
try {
ArrayList nodes = new ArrayList();
for (Paragraph paragraph : (Iterable) interimdoc.getChildNodes(NodeType.PARAGRAPH, true)) {
nodes = new ArrayList();
if (paragraph.toString(SaveFormat.TEXT).trim() .startsWith(“Fig”))
{
nodes.add(paragraph);
Node previousPara = paragraph.getPreviousSibling();
while (previousPara != null && previousPara.getNodeType() == NodeType.PARAGRAPH
&& ((Paragraph) previousPara).getChildNodes(NodeType.SHAPE, true).getCount() > 0) {
if (previousPara != null) nodes.add(previousPara);
previousPara = previousPara.getPreviousSibling();
}
// Remove text only paragraph
if (nodes.size() == 1 && ((Paragraph) nodes.get(0)).getChildNodes( NodeType.SHAPE, true).getCount() == 0)
nodes.clear();
if (nodes.size() > 0) {
// Reverse the node collection.
Collections.reverse(nodes);
// Extract the consecutive shapes and export them into
// new document
Document dstDoc = new Document();
dstDoc.removeAllChildren();
dstDoc.ensureMinimum();
dstDoc.getChildNodes(NodeType.RUN, true).clear();
for (Paragraph para : (Iterable) nodes)
{
NodeImporter importer = new NodeImporter(interimdoc, dstDoc,
ImportFormatMode.KEEP_SOURCE_FORMATTING);
if (dstDoc.getFirstSection().getBody().getFirstParagraph()
.toString(SaveFormat.TEXT).trim()
.startsWith(“Figure”)) {
dstDoc.getFirstSection().getBody().getFirstParagraph().remove();
}
//** OUTPUT FILENAME START //
String Imgcaption = paragraph.toString(SaveFormat.TEXT);
int k = 0;
while (k < Imgcaption.length()
&& !Character.isDigit(Imgcaption.charAt(k)))
k++;
int j = k;
while (j < Imgcaption.length()
&& Character.isDigit(Imgcaption.charAt(j)))
j++;
int l = Integer.parseInt(Imgcaption.substring(k, j));
String strI = Integer.toString(l);
Pattern pattern = Pattern.compile(strI);
Matcher matcher = pattern.matcher(Imgcaption);
String name = null;
while (matcher.find()) {
name = Imgcaption.substring(0, matcher.end());
name = name.replace(".", “");
}
if (name.startsWith(“Fig”)) {
name = “Fig” + "” + l;
}
//* OUTPUT FILENAME END *//
Node newNode = importer.importNode(para, true);
dstDoc.getFirstSection().getBody().appendChild(newNode);
String filename = folderName + name +"_ToCheck"+ DOCX;
dstDoc.save(filename);
previousPara.remove();
interimdoc.save(interim);
}
i++;
nodes.clear();
}
}
Thanks for your inquiry. Please use the following code example to get the desired output. We have attached the output documents and interim document with this post for your kind reference. Docs.zip (1.1 MB)
Document doc = new Document(MyDir + "Numbered_Image_Input.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
UseCase(doc, builder);
ExtractImages(doc, "uc", builder);
doc.save(MyDir + "interim_out.docx");