I have used this code to display an image by placing bookmarks using `poi
3.8`. Its running fine and also adding a picture but First problem is that
if I try to place the image at the beginning of the line, it always put
image at the end of the sentence. Secondly if I try to put an image in a
table it gives the same image in each row. Can anyone please help me doing
that:</br>
public static void insertCheckboxAtBookmark(String bookmarkName,
XWPFDocument document, int width, int height, String imageFiles) {
List<XWPFParagraph> paraList = null;
List<XWPFTable> tableList = null;
Iterator<XWPFTable> tableIter = null;
List<XWPFTableRow> rowList = null;
Iterator<XWPFTableRow> rowIter = null;
List<XWPFTableCell> cellList = null;
Iterator<XWPFTableCell> cellIter = null;
XWPFTable table = null;
XWPFTableRow row = null;
XWPFTableCell cell = null;
tableList = document.getTables();
tableIter = tableList.iterator();
//Insert images in the paragraph
paraList = document.getParagraphs();
processCheckboxBookmarkParaList(document, paraList,
bookmarkName,
width,height,imageFiles);
// Insert images at Bookmark inside table
while(tableIter.hasNext()) {
table = tableIter.next();
rowList = table.getRows();
rowIter = rowList.iterator();
while(rowIter.hasNext()) {
row = rowIter.next();
cellList = row.getTableCells();
cellIter = cellList.iterator();
while(cellIter.hasNext()) {
cell = cellIter.next();
paraList =cell.getParagraphs();
processCheckboxBookmarkParaList(document,
paraList,
bookmarkName, width,height,imageFiles);
}
}
}
}
**And**
public static void processCheckboxBookmarkParaList(XWPFDocument
document,List<XWPFParagraph> paraList, String bookmarkName,int width, int
height, String imageFiles) {
Iterator<XWPFParagraph> paraIter = null;
XWPFParagraph para = null;
XWPFRun run = null;
List<CTBookmark> bookmarkList = null;
Iterator<CTBookmark> bookmarkIter = null;
CTBookmark bookmark = null;
Node nextNode = null;
paraIter = paraList.iterator();
while(paraIter.hasNext()) {
para = paraIter.next();
bookmarkList = para.getCTP().getBookmarkStartList();
bookmarkIter = bookmarkList.iterator();
System.out.println("the bookmark List is ; " +
bookmarkList);
while(bookmarkIter.hasNext()) {
bookmark = bookmarkIter.next();
System.out.println("the bookmark ; " + bookmark);
System.out.println("the bookmark get name ; " +
bookmark.getName());
if(bookmark.getName().equals(bookmarkName) &&
imageFiles.length() != 0) {
// run = para.createRun();
// run.setText(imageFiles);
/* nextNode =
bookmark.getDomNode().getNextSibling();
while(!(nextNode.getNodeName().contains("bookmarkEnd")))
{
para.getCTP().getDomNode().removeChild(nextNode);
nextNode =
bookmark.getDomNode().getNextSibling();
// run = para.createRun();
//
para.getCTP().getDomNode().insertBefore(run.getCTR().getDomNode(),nextNode);
*/
addPicture(width,height,document,imageFiles,"",para.createRun());
System.out.println("add picture
para create contains ;
" + para.createRun());
// }
}
}
}
}
**And**
public static void addPicture(int width, int height, XWPFDocument doc,
String imageFilePath, String imageDescription , XWPFRun run )
{
try
{
final int EMU = 9525;
width *= EMU;
height *= EMU;
doc.addPictureData(new FileInputStream(new
File(imageFilePath)), Document.PICTURE_TYPE_PNG);
int id =doc.getNextPicNameNumber(Document.PICTURE_TYPE_PNG)-2;
String blipId =
doc.getAllPictures().get(id).getPackageRelationship().getId();
// imageDescription = new File(imageFilePath).getName();
if(imageDescription.length() == 0){
imageDescription = new File(imageFilePath).getName();
if(imageDescription.equals("Checked.png") ||
imageDescription.equals("UnChecked.png"))
imageDescription = "";
else
imageDescription = new
File(imageFilePath).getName();
}
CTInline inline = run.getCTR().addNewDrawing().addNewInline();
String picXml = "" +
"<a:graphic
xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +
" <a:graphicData
uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
" <pic:pic
xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
" <pic:nvPicPr>" +
" <pic:cNvPr id=\"" + id +
"\" name=\"Generated\"/>" +
" <pic:cNvPicPr/>" +
" </pic:nvPicPr>" +
" <pic:blipFill>" +
" <a:blip r:embed=\"" + blipId +
"\"
xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>"
+
" <a:stretch>" +
" <a:fillRect/>" +
" </a:stretch>" +
" </pic:blipFill>" +
" <pic:spPr>" +
" <a:xfrm>" +
" <a:off x=\"0\" y=\"0\"/>" +
" <a:ext cx=\"" + width +
"\" cy=\"" + height + "\"/>" +
" </a:xfrm>" +
" <a:prstGeom prst=\"rect\">" +
" <a:avLst/>" +
" </a:prstGeom>" +
" </pic:spPr>" +
" </pic:pic>" +
" </a:graphicData>" +
"</a:graphic>";
// " </a:graphicFrameLocks>";
XmlToken xmlToken = null;
xmlToken = XmlToken.Factory.parse(picXml);
inline.set(xmlToken);
inline.setDistT(0);
inline.setDistB(0);
inline.setDistL(0);
inline.setDistR(0);
CTPositiveSize2D extent = inline.addNewExtent();
extent.setCx(width);
extent.setCy(height);
CTNonVisualDrawingProps docPr = inline.addNewDocPr();
docPr.setId(id);
docPr.setName("Picture " + id);
docPr.setDescr("Generated");
run.addBreak();
run.setText(imageDescription);
run.addBreak();
run.addBreak();
}
catch(Exception xe)
{
}
}
--
View this message in context:
http://apache-poi.1045710.n5.nabble.com/Need-Help-to-put-an-image-on-specific-bookmark-in-docx-tp5713997.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]