Пример #1
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);
  }
 public COSBase getCOSObject() {
   COSDictionary dict = new COSDictionary();
   COSArray arr = new COSArray();
   for (Entry<Integer, PDPageLabelRange> i : labels.entrySet()) {
     arr.add(COSInteger.get(i.getKey()));
     arr.add(i.getValue());
   }
   dict.setItem(COSName.NUMS, arr);
   return dict;
 }
 /**
  * Insert the given stream at the beginning of the existing stream array.
  *
  * @param streamToBeInserted
  */
 public void insertCOSStream(PDStream streamToBeInserted) {
   COSArray tmp = new COSArray();
   tmp.add(streamToBeInserted);
   tmp.addAll(streams);
   streams.clear();
   streams = tmp;
 }
 /**
  * Sets the optional Matrix entry for the Pattern.
  *
  * @param transform the transformation matrix
  */
 public void setMatrix(AffineTransform transform) {
   COSArray matrix = new COSArray();
   double[] values = new double[6];
   transform.getMatrix(values);
   for (double v : values) {
     matrix.add(new COSFloat((float) v));
   }
   getCOSDictionary().setItem(COSName.MATRIX, matrix);
 }
 /**
  * Set pattern instead of a color space for stroking operations.
  *
  * @param operator The operator that is being executed.
  * @param arguments List
  * @throws IOException If an error occurs while processing the pattern.
  */
 public void process(PDFOperator operator, List<COSBase> arguments) throws IOException {
   COSName selectedPattern;
   int numberOfArguments = arguments.size();
   COSArray colorValues;
   if (numberOfArguments == 1) {
     selectedPattern = (COSName) arguments.get(0);
   } else {
     // uncolored tiling patterns shall have some additional color values
     // TODO: pass these values to the colorstate
     colorValues = new COSArray();
     for (int i = 0; i < numberOfArguments - 1; i++) {
       colorValues.add(arguments.get(i));
     }
     selectedPattern = (COSName) arguments.get(numberOfArguments - 1);
   }
   Map<String, PDPatternResources> patterns = getContext().getResources().getPatterns();
   PDPatternResources pattern = patterns.get(selectedPattern.getName());
   getContext().getGraphicsState().getStrokingColor().setPattern(pattern);
 }
 /**
  * Appends a new stream to the array that represents this object's stream.
  *
  * @param streamToAppend The stream to append.
  */
 public void appendStream(COSStream streamToAppend) {
   streams.add(streamToAppend);
 }
Пример #7
0
 /** Constructor with an initial range of 0..1. */
 public PDRange() {
   rangeArray = new COSArray();
   rangeArray.add(new COSFloat(0.0f));
   rangeArray.add(new COSFloat(1.0f));
   startingIndex = 0;
 }
Пример #8
0
  private COSDictionary buildDSSDictionary(List<DSSDictionaryCallback> callbacks) throws Exception {
    COSDictionary dss = new COSDictionary();

    Map<String, COSStream> streams = new HashMap<String, COSStream>();

    Set<CRLToken> allCrls = new HashSet<CRLToken>();
    Set<OCSPToken> allOcsps = new HashSet<OCSPToken>();
    Set<CertificateToken> allCertificates = new HashSet<CertificateToken>();

    COSDictionary vriDictionary = new COSDictionary();
    for (DSSDictionaryCallback callback : callbacks) {
      COSDictionary sigVriDictionary = new COSDictionary();
      sigVriDictionary.setDirect(true);

      if (CollectionUtils.isNotEmpty(callback.getCertificates())) {
        COSArray vriCertArray = new COSArray();
        for (CertificateToken token : callback.getCertificates()) {
          vriCertArray.add(getStream(streams, token));
          allCertificates.add(token);
        }
        sigVriDictionary.setItem("Cert", vriCertArray);
      }

      if (CollectionUtils.isNotEmpty(callback.getOcsps())) {
        COSArray vriOcspArray = new COSArray();
        for (OCSPToken token : callback.getOcsps()) {
          vriOcspArray.add(getStream(streams, token));
          allOcsps.add(token);
        }
        sigVriDictionary.setItem("OCSP", vriOcspArray);
      }

      if (CollectionUtils.isNotEmpty(callback.getCrls())) {
        COSArray vriCrlArray = new COSArray();
        for (CRLToken token : callback.getCrls()) {
          vriCrlArray.add(getStream(streams, token));
          allCrls.add(token);
        }
        sigVriDictionary.setItem("CRL", vriCrlArray);
      }

      PAdESSignature signature = callback.getSignature();
      final byte[] digest =
          DSSUtils.digest(
              DigestAlgorithm.SHA1, signature.getCAdESSignature().getCmsSignedData().getEncoded());
      String hexHash = Hex.encodeHexString(digest).toUpperCase();

      vriDictionary.setItem(hexHash, sigVriDictionary);
    }
    dss.setItem("VRI", vriDictionary);

    if (CollectionUtils.isNotEmpty(allCertificates)) {
      COSArray arrayAllCerts = new COSArray();
      for (CertificateToken token : allCertificates) {
        arrayAllCerts.add(getStream(streams, token));
      }
      dss.setItem("Certs", arrayAllCerts);
    }

    if (CollectionUtils.isNotEmpty(allOcsps)) {
      COSArray arrayAllOcsps = new COSArray();
      for (OCSPToken token : allOcsps) {
        arrayAllOcsps.add(getStream(streams, token));
      }
      dss.setItem("OCSPs", arrayAllOcsps);
    }

    if (CollectionUtils.isNotEmpty(allCrls)) {
      COSArray arrayAllCrls = new COSArray();
      for (CRLToken token : allCrls) {
        arrayAllCrls.add(getStream(streams, token));
      }
      dss.setItem("CRLs", arrayAllCrls);
    }

    return dss;
  }