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.

WriteTitle.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.examples.hpsf;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import org.apache.poi.hpsf.Property;
  20. import org.apache.poi.hpsf.PropertySet;
  21. import org.apache.poi.hpsf.Section;
  22. import org.apache.poi.hpsf.SummaryInformation;
  23. import org.apache.poi.hpsf.Variant;
  24. import org.apache.poi.hpsf.WritingNotSupportedException;
  25. import org.apache.poi.hpsf.wellknown.PropertyIDMap;
  26. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  27. /**
  28. * <p>This class is a simple sample application showing how to create a property
  29. * set and write it to disk.</p>
  30. */
  31. @SuppressWarnings({"java:S106","java:S4823"})
  32. public final class WriteTitle {
  33. private WriteTitle() {}
  34. /**
  35. * <p>Runs the example program.</p>
  36. *
  37. * @param args Command-line arguments. The first and only command-line
  38. * argument is the name of the POI file system to create.
  39. * @throws IOException if any I/O exception occurs.
  40. * @throws WritingNotSupportedException if HPSF does not (yet) support
  41. * writing a certain property type.
  42. */
  43. public static void main(final String[] args)
  44. throws WritingNotSupportedException, IOException
  45. {
  46. /* Check whether we have exactly one command-line argument. */
  47. if (args.length != 1)
  48. {
  49. System.err.println("Usage: " + WriteTitle.class.getName() + "destinationPOIFS");
  50. System.exit(1);
  51. }
  52. final String fileName = args[0];
  53. /* Create a mutable property set. Initially it contains a single section
  54. * with no properties. */
  55. final PropertySet mps = new PropertySet();
  56. /* Retrieve the section the property set already contains. */
  57. final Section ms = mps.getSections().get(0);
  58. /* Turn the property set into a summary information property. This is
  59. * done by setting the format ID of its first section to
  60. * SectionIDMap.SUMMARY_INFORMATION_ID. */
  61. ms.setFormatID(SummaryInformation.FORMAT_ID);
  62. /* Create an empty property. */
  63. final Property p = new Property();
  64. /* Fill the property with appropriate settings so that it specifies the
  65. * document's title. */
  66. p.setID(PropertyIDMap.PID_TITLE);
  67. p.setType(Variant.VT_LPWSTR);
  68. p.setValue("Sample title");
  69. /* Place the property into the section. */
  70. ms.setProperty(p);
  71. /* Create the POI file system the property set is to be written to.
  72. * For writing the property set into a POI file system it has to be
  73. * handed over to the POIFS.createDocument() method as an input stream
  74. * which produces the bytes making out the property set stream. */
  75. try (final POIFSFileSystem poiFs = new POIFSFileSystem();
  76. final InputStream is = mps.toInputStream();
  77. final FileOutputStream fos = new FileOutputStream(fileName)) {
  78. /* Create the summary information property set in the POI file
  79. * system. It is given the default name most (if not all) summary
  80. * information property sets have. */
  81. poiFs.createDocument(is, SummaryInformation.DEFAULT_STREAM_NAME);
  82. /* Write the whole POI file system to a disk file. */
  83. poiFs.writeFilesystem(fos);
  84. }
  85. }
  86. }