You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XWPFPicture.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xwpf.usermodel;
  16. import org.apache.poi.ooxml.POIXMLDocumentPart;
  17. import org.apache.poi.openxml4j.opc.PackageRelationship;
  18. import org.apache.poi.util.Units;
  19. import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
  20. import org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture;
  21. public class XWPFPicture {
  22. private final CTPicture ctPic;
  23. private final String description;
  24. private final XWPFRun run;
  25. public XWPFPicture(CTPicture ctPic, XWPFRun run) {
  26. this.run = run;
  27. this.ctPic = ctPic;
  28. if (ctPic == null || ctPic.getNvPicPr() == null || ctPic.getNvPicPr().getCNvPr() == null) {
  29. throw new IllegalArgumentException("Found missing data while reading picture data from " + ctPic);
  30. }
  31. description = ctPic.getNvPicPr().getCNvPr().getDescr();
  32. }
  33. /**
  34. * Link Picture with PictureData
  35. */
  36. public void setPictureReference(PackageRelationship rel) {
  37. ctPic.getBlipFill().getBlip().setEmbed(rel.getId());
  38. }
  39. /**
  40. * Return the underlying CTPicture bean that holds all properties for this picture
  41. *
  42. * @return the underlying CTPicture bean
  43. */
  44. public CTPicture getCTPicture() {
  45. return ctPic;
  46. }
  47. /**
  48. * Get the PictureData of the Picture, if present.
  49. * Note - not all kinds of picture have data
  50. */
  51. public XWPFPictureData getPictureData() {
  52. CTBlipFillProperties blipProps = ctPic.getBlipFill();
  53. if (blipProps == null || !blipProps.isSetBlip()) {
  54. // return null if Blip data is missing
  55. return null;
  56. }
  57. String blipId = blipProps.getBlip().getEmbed();
  58. POIXMLDocumentPart part = run.getParent().getPart();
  59. if (part != null) {
  60. POIXMLDocumentPart relatedPart = part.getRelationById(blipId);
  61. if (relatedPart instanceof XWPFPictureData) {
  62. return (XWPFPictureData) relatedPart;
  63. }
  64. }
  65. return null;
  66. }
  67. /**
  68. * Returns the width of the picture (in points).
  69. *
  70. * @since POI 4.1.1
  71. */
  72. public double getWidth() {
  73. return Units.toPoints(ctPic.getSpPr().getXfrm().getExt().getCx());
  74. }
  75. /**
  76. * Returns the depth of the picture (in points).
  77. *
  78. * @since POI 4.1.1
  79. */
  80. public double getDepth() {
  81. return Units.toPoints(ctPic.getSpPr().getXfrm().getExt().getCy());
  82. }
  83. public String getDescription() {
  84. return description;
  85. }
  86. }