How to add image with border into table cell in Word with Apache POI?

ML72 :

I am trying to insert a picture with a border into a table in Microsoft Word with Apache POI. I am able to add an image into a cell with the code below:

// table is a XWPFTable object instantiated earlier in the code
XWPFParagraph paragraph = table.getRow(0).getCell(0).addParagraph();
XWPFRun run = paragraph.createRun();
FileInputStream fis = new FileInputStream("C:\\ [filepath for the image]");
run.addPicture(fis, XWPFDocument.PICTURE_TYPE_PNG, "Name", 6217920, 3474720);

I tried looking for ways to add a border to the image, but I haven't been able to find any resources online. I came across this link: Format Picture with Fill and Line using apache poi in Java but it does not help in this case.

(To be specific, I want to add a solid black line which is 2 1/4 pt thick around the image)

Does anybody know how to do achieve this? Thanks in advance.

Axel Richter :

As always if the current high level apache poi classes not provide some Office Open XML features, do the following:

First do what is provided and have a look at the underlying XML you get created. In that case, do:

// table is a XWPFTable object instantiated earlier in the code
XWPFParagraph paragraph = table.getRow(0).getCell(0).addParagraph();
XWPFRun run = paragraph.createRun();
FileInputStream fis = new FileInputStream("C:\\ [filepath for the image]");
XWPFPicture picture = run.addPicture(fis, XWPFDocument.PICTURE_TYPE_PNG, "Name", Units.pixelToEMU(300), Units.pixelToEMU(150));
System.out.println(picture.getCTPicture());

You will get something like:

<xml-fragment xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:rel="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  <pic:nvPicPr>
    <pic:cNvPr id="0" name="Picture 0" descr="Name"/>
    <pic:cNvPicPr>
      <a:picLocks noChangeAspect="true"/>
    </pic:cNvPicPr>
  </pic:nvPicPr>
  <pic:blipFill>
    <a:blip rel:embed="rId2"/>
    <a:stretch>
      <a:fillRect/>
    </a:stretch>
  </pic:blipFill>
  <pic:spPr>
    <a:xfrm>
      <a:off x="0" y="0"/>
      <a:ext cx="2857500" cy="1428750"/>
    </a:xfrm>
    <a:prstGeom prst="rect">
      <a:avLst/>
    </a:prstGeom>
  </pic:spPr>
</xml-fragment>

Now open the result in Word and add what you want. In that case add the border around the picture. Then save the result, unzip the *.docx Zip archive and have a look at /word/document.xml to get what has changed.

You will find something like:

<a:ln w="28575">
  <a:solidFill>
    <a:srgbClr val="000000"/>
  </a:solidFill>
</a:ln>

added to <pic:spPr>.

Now try creating that using the low level ooxml-schema classes of apache poi:

...
XWPFPicture picture = run.addPicture(fis, XWPFDocument.PICTURE_TYPE_PNG, "Name", Units.pixelToEMU(300), Units.pixelToEMU(150));
System.out.println(picture.getCTPicture());

picture.getCTPicture().getSpPr().addNewLn().setW(Units.toEMU(2.25));
picture.getCTPicture().getSpPr().getLn().addNewSolidFill().addNewSrgbClr().setVal(new byte[]{0,0,0});
System.out.println(picture.getCTPicture());
...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to add comment to a table of word by apache poi

How to add a hyperlink to image in a Word document using Apache POI?

apache poi add table in word document

How to insert a table into a cell in poi word?

Apache POI Word Table Cell not updating color (XWPFTableCell)

Apache POI 5 : Set hyperlinks in word table cell

Apache POI word best way to add text after table

Apache POI set cell border is not working

How to prevent table cell border to be cut by cell image?

how to set specific cell width in different row in apache poi table?

how to add images side by side word document using apache poi

How to set table dimensions and spacing in word using apache poi in java

How to put a table inside header of word using java apache poi?

How to change table direction in Apache word poi (XWPF)?

table cell with border-image

java Apache POI Word existing table insert row with cell style and formatting

How to add border radius for a table cell, not working on all browsers

Word VBS script add border cell

How to add table heading drop-down with Apache Poi

Apache POI Word XWPF table direction and alignment

How to add a image in a border

how to set cell setVerticalAlignment in poi word?

How to add comment by apache poi

Apache Poi - Java-: How to add text containing blank lines as separate paragraphs to a Word document using Apache POI?

How to add bottom padding to a table cell conditionally on word wrap

How to format cell in XWPFTable in Apache POI

How to cache the formula cell by using Apache POI

How to format cell as currency using Apache POI

How to Highlight Replaced Word Using Apache POI

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive