diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java index 016eb2c3b8..876339f2fa 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java @@ -25,11 +25,16 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import com.microsoft.schemas.vml.CTShape; import org.apache.poi.ooxml.POIXMLDocumentPart; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.ss.util.CellAddress; import org.apache.poi.util.Internal; +import org.apache.poi.util.Units; +import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFComment; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFVMLDrawing; import org.apache.xmlbeans.XmlException; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCommentList; @@ -168,6 +173,37 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments { return commentRefs.keySet().iterator(); } + /** + * Create a new comment and add to the CommentTable. + * @param sheet sheet to add comment to + * @param clientAnchor the anchor for this comment + * @return new XSSFComment + * @since POI 5.2.0 + */ + public XSSFComment createNewComment(XSSFSheet sheet, XSSFClientAnchor clientAnchor) { + XSSFVMLDrawing vml = sheet.getVMLDrawing(true); + com.microsoft.schemas.vml.CTShape vmlShape = vml.newCommentShape(); + if (clientAnchor.isSet()) { + // convert offsets from emus to pixels since we get a + // DrawingML-anchor + // but create a VML Drawing + int dx1Pixels = clientAnchor.getDx1() / Units.EMU_PER_PIXEL; + int dy1Pixels = clientAnchor.getDy1() / Units.EMU_PER_PIXEL; + int dx2Pixels = clientAnchor.getDx2() / Units.EMU_PER_PIXEL; + int dy2Pixels = clientAnchor.getDy2() / Units.EMU_PER_PIXEL; + String position = clientAnchor.getCol1() + ", " + dx1Pixels + ", " + clientAnchor.getRow1() + ", " + dy1Pixels + ", " + + clientAnchor.getCol2() + ", " + dx2Pixels + ", " + clientAnchor.getRow2() + ", " + dy2Pixels; + vmlShape.getClientDataArray(0).setAnchorArray(0, position); + } + CellAddress ref = new CellAddress(clientAnchor.getRow1(), clientAnchor.getCol1()); + + if (findCellComment(ref) != null) { + throw new IllegalArgumentException("Multiple cell comments in one cell are not allowed, cell: " + ref); + } + + return new XSSFComment(this, newComment(ref), vmlShape); + } + /** * Refresh Map commentRefs cache, * Calls that use the commentRefs cache will perform in O(1) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java index ee953ba50c..99a0bed281 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java @@ -42,7 +42,6 @@ import org.apache.poi.openxml4j.opc.PackagingURIHelper; import org.apache.poi.openxml4j.opc.TargetMode; import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.Drawing; -import org.apache.poi.ss.util.CellAddress; import org.apache.poi.ss.util.ImageUtils; import org.apache.poi.util.Internal; import org.apache.poi.util.Units; @@ -384,29 +383,8 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing