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.

XWPFSignatureLine.java 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 javax.xml.namespace.QName;
  17. import com.microsoft.schemas.office.office.CTSignatureLine;
  18. import com.microsoft.schemas.vml.CTImageData;
  19. import org.apache.poi.common.usermodel.PictureType;
  20. import org.apache.poi.ooxml.util.XPathHelper;
  21. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  22. import org.apache.poi.poifs.crypt.dsig.SignatureLine;
  23. import org.apache.xmlbeans.XmlException;
  24. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture;
  25. public class XWPFSignatureLine extends SignatureLine {
  26. static final String NS_OOXML_WP_MAIN = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
  27. private static final String MS_VML_URN = "urn:schemas-microsoft-com:vml";
  28. private CTSignatureLine line;
  29. public void parse(XWPFDocument doc) throws XmlException {
  30. line = XPathHelper.selectProperty(doc.getDocument(), CTSignatureLine.class, null,
  31. new QName[]{new QName(NS_OOXML_WP_MAIN, "body")},
  32. new QName[]{new QName(NS_OOXML_WP_MAIN, "p")},
  33. new QName[]{new QName(NS_OOXML_WP_MAIN, "r")},
  34. new QName[]{new QName(NS_OOXML_WP_MAIN, "pict")},
  35. new QName[]{new QName(MS_VML_URN, "shape")},
  36. new QName[]{QNAME_SIGNATURE_LINE});
  37. if (line != null) {
  38. setSignatureShape(line);
  39. parse();
  40. }
  41. }
  42. public void add(XWPFParagraph paragraph) {
  43. XWPFRun r = paragraph.createRun();
  44. CTPicture pict = r.getCTR().addNewPict();
  45. add(pict, (image, type) -> paragraph.getDocument().addPictureData(image, mapType(type)));
  46. }
  47. @Override
  48. protected void setRelationId(CTImageData imageData, String relId) {
  49. imageData.setId2(relId);
  50. }
  51. private static int mapType(PictureType type) throws InvalidFormatException {
  52. switch (type) {
  53. case BMP:
  54. return Document.PICTURE_TYPE_BMP;
  55. case DIB:
  56. return Document.PICTURE_TYPE_DIB;
  57. case EMF:
  58. return Document.PICTURE_TYPE_EMF;
  59. case EPS:
  60. return Document.PICTURE_TYPE_EPS;
  61. case GIF:
  62. return Document.PICTURE_TYPE_GIF;
  63. case JPEG:
  64. return Document.PICTURE_TYPE_JPEG;
  65. case PICT:
  66. return Document.PICTURE_TYPE_PICT;
  67. case PNG:
  68. return Document.PICTURE_TYPE_PNG;
  69. case TIFF:
  70. return Document.PICTURE_TYPE_TIFF;
  71. case WMF:
  72. return Document.PICTURE_TYPE_WMF;
  73. case WPG:
  74. return Document.PICTURE_TYPE_WPG;
  75. default:
  76. throw new InvalidFormatException("Unsupported picture format "+type);
  77. }
  78. }
  79. }