⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pdfoutputtarget.java

📁 Java的Web报表库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

    if (imageRef.getImage() != null)
    {
      // since version 0.99 iText supports Alpha-PNGs
      final WaitingImageObserver obs = new WaitingImageObserver(imageRef.getImage());
      obs.waitImageLoaded();

      final PngEncoder encoder = new PngEncoder(imageRef.getImage(), PngEncoder.ENCODE_ALPHA,
          PngEncoder.FILTER_NONE, 5);
      final byte[] data = encoder.pngEncode();
      return Image.getInstance(data);
    }

    throw new DocumentException("Neither an URL nor an Image was given to paint the graphics");
  }

  /**
   * Returns the corrected Y value.
   *
   * @param y  the y value.
   *
   * @return the corrected value.
   */
  private float getCorrectedY(final float y)
  {
    return getPageHeight() - y;
  }

  /**
   * Draws a shape at the specified location. The shape is drawn using a PathIterator. All
   * Shapes are supported. Set a stroke and a paint before drawing. The shape is not filled.
   *
   * @param shape  the shape to draw.
   */
  public void drawShape(final Shape shape)
  {
    final Rectangle2D bounds = getInternalOperationBounds();

    final float ycorr = (float) bounds.getY();
    final float xcorr = (float) bounds.getX();

    final PathIterator pit = shape.getPathIterator(null);
    final PdfContentByte cb = this.writer.getDirectContent();

    cb.newPath();
    cb.moveTo(xcorr, getCorrectedY(ycorr));

    final float[] params = new float[6];
    // How to apply this? This should be needed in fillShape
    while (pit.isDone() == false)
    {
      final int cmd = pit.currentSegment(params);
      params[1] = getCorrectedY(params[1] + ycorr);
      params[3] = getCorrectedY(params[3] + ycorr);
      params[5] = getCorrectedY(params[5] + ycorr);

      params[0] = params[0] + xcorr;
      params[2] = params[2] + xcorr;
      params[4] = params[4] + xcorr;

      switch (cmd)
      {
        case PathIterator.SEG_MOVETO:
          {
            cb.moveTo(params[0], params[1]);
            break;
          }
        case PathIterator.SEG_LINETO:
          {
            cb.lineTo(params[0], params[1]);
            break;
          }
        case PathIterator.SEG_CUBICTO:
          {
            cb.curveTo(params[0], params[1],
                params[2], params[3],
                params[4], params[5]);
            break;
          }
        case PathIterator.SEG_QUADTO:
          {
            cb.curveTo(params[0], params[1],
                params[2], params[3]);
            break;
          }
        case PathIterator.SEG_CLOSE:
          {
            cb.closePath();
          }
      }
      pit.next();
    }
    cb.stroke();
  }

  /**
   * Draws a shape at the specified location. The shape is drawn using a PathIterator. All
   * Shapes are supported. Set a stroke and a paint before drawing. The shape is filled using
   * the current paint and no outline is drawn.
   *
   * @param shape  the shape to fill.
   */
  public void fillShape(final Shape shape)
  {
    final Rectangle2D bounds = getInternalOperationBounds();

    final float ycorr = (float) bounds.getY();
    final float xcorr = (float) bounds.getX();

    final PathIterator pit = shape.getPathIterator(null);
    final PdfContentByte cb = this.writer.getDirectContent();

    cb.newPath();
    cb.moveTo(xcorr, getCorrectedY(ycorr));

    final int windingRule = pit.getWindingRule();

    final float[] params = new float[6];
    // How to apply this? This should be needed in fillShape
    while (pit.isDone() == false)
    {
      final int cmd = pit.currentSegment(params);
      params[1] = getCorrectedY(params[1] + ycorr);
      params[3] = getCorrectedY(params[3] + ycorr);
      params[5] = getCorrectedY(params[5] + ycorr);

      params[0] = params[0] + xcorr;
      params[2] = params[2] + xcorr;
      params[4] = params[4] + xcorr;

      switch (cmd)
      {
        case PathIterator.SEG_MOVETO:
          {
            cb.moveTo(params[0], params[1]);
            break;
          }
        case PathIterator.SEG_LINETO:
          {
            cb.lineTo(params[0], params[1]);
            break;
          }
        case PathIterator.SEG_CUBICTO:
          {
            cb.curveTo(params[0], params[1],
                params[2], params[3],
                params[4], params[5]);
            break;
          }
        case PathIterator.SEG_QUADTO:
          {
            cb.curveTo(params[0], params[1],
                params[2], params[3]);
            break;
          }
        case PathIterator.SEG_CLOSE:
          {
            cb.closePath();
          }
      }
      pit.next();
    }
    if (windingRule == PathIterator.WIND_EVEN_ODD)
    {
      cb.eoFill();
    }
    else
    {
      cb.fill();
    }
  }

  /**
   * This method is called when the page is ended.
   *
   * @throws OutputTargetException if there was a problem with the target.
   */
  public void endPage() throws OutputTargetException
  {
    try
    {
      this.writer.getDirectContent().restoreState();
      this.getDocument().newPage();
    }
    catch (Exception e)
    {
      throw new OutputTargetException("Failed to end page", e);
    }
  }

  /**
   * Opens the document.
   *
   * @throws OutputTargetException if there is a problem with the target.
   */
  public void open() throws OutputTargetException
  {

    final PageFormat pageFormat = getLogicalPage().getPhysicalPageFormat();
    final float urx = (float) pageFormat.getWidth();
    final float ury = (float) pageFormat.getHeight();

    final float marginLeft = (float) pageFormat.getImageableX();
    final float marginRight =
        (float) (pageFormat.getWidth()
        - pageFormat.getImageableWidth()
        - pageFormat.getImageableX());
    final float marginTop = (float) pageFormat.getImageableY();
    final float marginBottom =
        (float) (pageFormat.getHeight()
        - pageFormat.getImageableHeight()
        - pageFormat.getImageableY());
    final Rectangle pageSize = new Rectangle(urx, ury);

    try
    {
      if (pageFormat.getOrientation() != PageFormat.PORTRAIT)
      {
        pageSize.rotate();
      }

      setDocument(new Document(pageSize, marginLeft, marginRight, marginTop, marginBottom));

      writer = PdfWriter.getInstance(getDocument(), out);
      writer.setLinearPageMode();

      final String encrypt = getProperty(SECURITY_ENCRYPTION);

      if (encrypt != null)
      {
        if (encrypt.equals(SECURITY_ENCRYPTION_128BIT) == true
            || encrypt.equals(SECURITY_ENCRYPTION_40BIT) == true)
        {
          final String userpassword = getProperty(SECURITY_USERPASSWORD);
          final String ownerpassword = getProperty(SECURITY_OWNERPASSWORD);
          //Log.debug ("UserPassword: " + userpassword + " - OwnerPassword: " + ownerpassword);
          final byte[] userpasswordbytes = DocWriter.getISOBytes(userpassword);
          byte[] ownerpasswordbytes = DocWriter.getISOBytes(ownerpassword);
          if (ownerpasswordbytes == null)
          {
            ownerpasswordbytes = PDF_PASSWORD_PAD;
          }
          writer.setEncryption(userpasswordbytes, ownerpasswordbytes, getPermissions(),
              encrypt.equals(SECURITY_ENCRYPTION_128BIT));
        }
      }

      /**
       * MetaData can be set when the writer is registered to the document.
       */
      final String title = getProperty(TITLE);
      final String author = getProperty(AUTHOR);

      if (title != null)
      {
        getDocument().addTitle(title);
      }
      if (author != null)
      {
        getDocument().addAuthor(author);
      }
      getDocument().addCreator(CREATOR);
      getDocument().addCreationDate();

      getDocument().open();

      //writer.getDirectContent().saveState();
    }
    catch (Exception e)
    {
      throw new OutputTargetException("Opening Document failed.", e);
    }
  }


  /**
   * Signals that a page is being started. Stores the state of the target to
   * make it possible to restore the complete outputtarget.
   *
   * @param format  the physical page.
   */
  public void beginPage(final PhysicalPage format)
  {
    if (isOpen() == false)
    {
      throw new IllegalStateException("Target " + hashCode() + " is not open");
    }

    try
    {
      setPaint(ElementDefaultStyleSheet.DEFAULT_PAINT);
      setStroke(ShapeElement.DEFAULT_STROKE);
      setFont(ElementDefaultStyleSheet.DEFAULT_FONT_DEFINITION);
    }
    catch (OutputTargetException oe)
    {
      Log.error("Should not happen", oe);
    }

    this.writer.getDirectContent().saveState();
    this.currentPageFormat = format.getPageFormat();
  }

  /**
   * Reads a boolean property. If the property is not set, the given
   * default value is returned. This method returns true, if the property
   * is set to the value "true", false otherwise.
   *
   * @param key the key that should be queried.
   * @param value the defaultvalue.
   * @return the true, if the property has the value "true", false otherwise.
   */
  private boolean getBooleanProperty (final String key, final boolean value)
  {
    final String val = getProperty(key);
    if (val == null)
    {
      return value;
    }
    else
    {
      return val.equals("true");
    }
  }

  /**
   * Extracts the permissions for this PDF. The permissions are returned as flags in the integer
   * value. All permissions are defined as properties which have to be set before the target is
   * opened.
   *
   * @return the permissions.
   */
  private int getPermissions()
  {
    final boolean allowPrinting = getBooleanProperty(SECURITY_ALLOW_PRINTING, false);
    final boolean allowModifyContents = getBooleanProperty(SECURITY_ALLOW_MODIFY_CONTENTS, false);
    final boolean allowModifyAnnotations = getBooleanProperty(SECURITY_ALLOW_MODIFY_ANNOTATIONS, false);
    final boolean allowCopy = getBooleanProperty(SECURITY_ALLOW_COPY, false);
    final boolean allowFillIn = getBooleanProperty(SECURITY_ALLOW_FILLIN, false);
    final boolean allowScreenReaders = getBooleanProperty(SECURITY_ALLOW_SCREENREADERS, false);
    final boolean allowAssembly = getBooleanProperty(SECURITY_ALLOW_ASSEMBLY, false);
    final boolean allowDegradedPrinting = getBooleanProperty(SECURITY_ALLOW_DEGRADED_PRINTING, false);

    int permissions = 0;
    if (allowPrinting)
    {
      permissions |= PdfWriter.AllowPrinting;
    }
    if (allowModifyContents)
    {
      permissions |= PdfWriter.AllowModifyContents;
    }
    if (allowModifyAnnotations)
    {
      permissions |= PdfWriter.AllowModifyAnnotations;
    }
    if (allowCopy)
    {
      permissions |= PdfWriter.AllowCopy;
    }
    if (allowFillIn)
    {
      permissions |= PdfWriter.AllowFillIn;
    }
    if (allowScreenReaders)
    {
      permissions |= PdfWriter.AllowScreenReaders;
    }
    if (allowAssembly)
    {
      permissions |= PdfWriter.AllowAssembly;
    }
    if (allowDegradedPrinting)
    {
      permissions |= PdfWriter.AllowDegradedPrinting;
    }
    return permissions;
  }

  /**
   * Closes the document.
   */
  public void close()
  {
    this.getDocument().close();
    this.fontSupport.close();
    this.pdfDocument = null;
    this.writer = null;
  }

  /**
   * Draws the band onto the specified graphics device. The Text is printed on the
   * bottom of the elements bounds.
   *
   * @param text The text to be printed.
   */
  public void drawString(final String text)
  {
    final Rectangle2D bounds = getInternalOperationBounds();
    final int fontSize = getFont().getFontSize();

    final PdfContentByte cb = this.writer.getDirectContent();
    cb.beginText();
    cb.setFontAndSize(this.baseFont, fontSize);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -