Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

XSSFSignatureLine.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.xssf.usermodel;
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import javax.xml.namespace.QName;
  19. import com.microsoft.schemas.office.excel.CTClientData;
  20. import com.microsoft.schemas.office.excel.STObjectType;
  21. import com.microsoft.schemas.office.office.CTSignatureLine;
  22. import com.microsoft.schemas.vml.CTImageData;
  23. import com.microsoft.schemas.vml.CTShape;
  24. import org.apache.poi.common.usermodel.PictureType;
  25. import org.apache.poi.ooxml.POIXMLDocumentPart;
  26. import org.apache.poi.ooxml.POIXMLException;
  27. import org.apache.poi.ooxml.POIXMLRelation;
  28. import org.apache.poi.ooxml.util.XPathHelper;
  29. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  30. import org.apache.poi.poifs.crypt.dsig.SignatureLine;
  31. import org.apache.poi.schemas.vmldrawing.CTXML;
  32. import org.apache.xmlbeans.XmlException;
  33. import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STTrueFalseBlank;
  34. public class XSSFSignatureLine extends SignatureLine {
  35. private static final String MS_VML_URN = "urn:schemas-microsoft-com:vml";
  36. public void parse(XSSFSheet sheet) throws XmlException {
  37. XSSFVMLDrawing vml = sheet.getVMLDrawing(false);
  38. if (vml == null) {
  39. return;
  40. }
  41. CTSignatureLine line = XPathHelper.selectProperty(vml.getDocument(), CTSignatureLine.class, null,
  42. new QName[]{XSSFVMLDrawing.QNAME_VMLDRAWING},
  43. new QName[]{new QName(MS_VML_URN, "shape")},
  44. new QName[]{QNAME_SIGNATURE_LINE});
  45. if (line != null) {
  46. setSignatureShape(line);
  47. parse();
  48. }
  49. }
  50. public void add(XSSFSheet sheet, XSSFClientAnchor anchor) {
  51. XSSFVMLDrawing vml = sheet.getVMLDrawing(true);
  52. CTXML root = vml.getDocument().getXml();
  53. add(root, (image, type) -> addPicture(image,type,sheet));
  54. CTShape shape = getSignatureShape();
  55. CTClientData clientData = shape.addNewClientData();
  56. // LeftColumn, LeftOffset, TopRow, TopOffset, RightColumn, RightOffset, BottomRow, BottomOffset
  57. String anchorStr =
  58. anchor.getCol1()+", "+
  59. anchor.getDx1()+", "+
  60. anchor.getRow1()+", "+
  61. anchor.getDy1()+", "+
  62. anchor.getCol2()+", "+
  63. anchor.getDx2()+", "+
  64. anchor.getRow2()+", "+
  65. anchor.getDy2();
  66. // anchorStr = "2, 0, 3, 0, 5, 136, 9, 32";
  67. clientData.addAnchor(anchorStr);
  68. clientData.setObjectType(STObjectType.PICT);
  69. clientData.addSizeWithCells(STTrueFalseBlank.X);
  70. clientData.addCF("pict");
  71. clientData.addAutoPict(STTrueFalseBlank.X);
  72. }
  73. @Override
  74. protected void setRelationId(CTImageData imageData, String relId) {
  75. imageData.setRelid(relId);
  76. }
  77. private String addPicture(byte[] image, PictureType type, XSSFSheet sheet) throws InvalidFormatException {
  78. XSSFWorkbook wb = sheet.getWorkbook();
  79. XSSFVMLDrawing vml = sheet.getVMLDrawing(false);
  80. POIXMLRelation xtype = mapType(type);
  81. int idx = wb.getNextPartNumber(xtype, -1);
  82. POIXMLDocumentPart.RelationPart rp = vml.createRelationship(xtype, wb.getXssfFactory(), idx, false);
  83. POIXMLDocumentPart dp = rp.getDocumentPart();
  84. try (OutputStream out = dp.getPackagePart().getOutputStream()) {
  85. out.write(image);
  86. } catch (IOException e) {
  87. throw new POIXMLException(e);
  88. }
  89. return rp.getRelationship().getId();
  90. }
  91. private static POIXMLRelation mapType(PictureType type) throws InvalidFormatException {
  92. switch (type) {
  93. case BMP:
  94. return XSSFRelation.IMAGE_BMP;
  95. case DIB:
  96. return XSSFRelation.IMAGE_DIB;
  97. case EMF:
  98. return XSSFRelation.IMAGE_EMF;
  99. case EPS:
  100. return XSSFRelation.IMAGE_EPS;
  101. case GIF:
  102. return XSSFRelation.IMAGE_GIF;
  103. case JPEG:
  104. return XSSFRelation.IMAGE_JPEG;
  105. case PICT:
  106. return XSSFRelation.IMAGE_PICT;
  107. case PNG:
  108. return XSSFRelation.IMAGE_PNG;
  109. case TIFF:
  110. return XSSFRelation.IMAGE_TIFF;
  111. case WMF:
  112. return XSSFRelation.IMAGE_WMF;
  113. case WPG:
  114. return XSSFRelation.IMAGE_WPG;
  115. default:
  116. throw new InvalidFormatException("Unsupported picture format "+type);
  117. }
  118. }
  119. }