Example #1
0
 protected byte[] getStreamData(PDStream pdstream) throws IOException {
   List<COSName> filters = pdstream.getFilters();
   if (filters != null && filters.contains(COSName.DCT_DECODE)) {
     // JPG
     InputStream input =
         pdstream.getPartiallyFilteredStream(
             Arrays.asList(
                 new String[] {
                   COSName.DCT_DECODE.getName(), COSName.DCT_DECODE_ABBREVIATION.getName()
                 }));
     return readBytes(input);
   }
   if (filters != null && filters.contains(COSName.CCITTFAX_DECODE)) {
     // TIFF
     InputStream input =
         pdstream.getPartiallyFilteredStream(
             Arrays.asList(
                 new String[] {
                   COSName.CCITTFAX_DECODE.getName(),
                   COSName.CCITTFAX_DECODE_ABBREVIATION.getName()
                 }));
     return readBytes(input);
   }
   return pdstream.getByteArray();
 }
Example #2
0
  /**
   * Create a new PDPage content stream.
   *
   * @param document The document the page is part of.
   * @param sourcePage The page to write the contents to.
   * @param appendContent Indicates whether content will be overwritten. If false all previous
   *     content is deleted.
   * @param compress Tell if the content stream should compress the page contents.
   * @throws IOException If there is an error writing to the page contents.
   */
  public PDPageContentStream(
      PDDocument document, PDPage sourcePage, boolean appendContent, boolean compress)
      throws IOException {
    page = sourcePage;
    resources = page.getResources();
    if (resources == null) {
      resources = new PDResources();
      page.setResources(resources);
    }
    fonts = resources.getFonts();
    xobjects = resources.getImages();
    // If request specifies the need to append to the document
    if (appendContent) {
      // Get the pdstream from the source page instead of creating a new one
      PDStream contents = sourcePage.getContents();

      // Create a pdstream to append new content
      PDStream contentsToAppend = new PDStream(document);

      // This will be the resulting COSStreamArray after existing and new streams are merged
      COSStreamArray compoundStream = null;

      // If contents is already an array, a new stream is simply appended to it
      if (contents.getStream() instanceof COSStreamArray) {
        compoundStream = (COSStreamArray) contents.getStream();
        compoundStream.appendStream(contentsToAppend.getStream());
      } else {
        // Creates the COSStreamArray and adds the current stream plus a new one to it
        COSArray newArray = new COSArray();
        newArray.add(contents.getCOSObject());
        newArray.add(contentsToAppend.getCOSObject());
        compoundStream = new COSStreamArray(newArray);
      }

      if (compress) {
        List filters = new ArrayList();
        filters.add(COSName.FLATE_DECODE);
        contentsToAppend.setFilters(filters);
      }

      // Sets the compoundStream as page contents
      sourcePage.setContents(new PDStream(compoundStream));
      output = contentsToAppend.createOutputStream();
    } else {
      PDStream contents = new PDStream(document);
      if (compress) {
        List filters = new ArrayList();
        filters.add(COSName.FLATE_DECODE);
        contents.setFilters(filters);
      }
      sourcePage.setContents(contents);
      output = contents.createOutputStream();
    }
    formatDecimal.setMaximumFractionDigits(10);
    formatDecimal.setGroupingUsed(false);
  }