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

📄 pdfoutputtarget.java

📁 Java的Web报表库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    final float y2 = (float) (bounds.getY() + baseFont.getFontDescriptor(BaseFont.ASCENT, fontSize));
    cb.showTextAligned(
        PdfContentByte.ALIGN_LEFT,
        text,
        (float) bounds.getX(),
        this.getPageHeight() - y2,
        0);
    cb.endText();
  }

  /**
   * Defines the stroke used to draw shapes. If the stroke is of an invalid type, an
   * OutputTargetException is thrown. Currently only BasicStroke is supported.
   *
   * @param stroke  the stroke.
   *
   * @throws OutputTargetException if there is a problem with the target.
   */
  public void setStroke(final Stroke stroke) throws OutputTargetException
  {
    if (stroke == null)
    {
      throw new NullPointerException();
    }
    if (stroke instanceof BasicStroke == false)
    {
      throw new OutputTargetException("Unable to handle this stroke type");
    }

    // If this stroke is already set, do nothing
    if (awtStroke != null && awtStroke.equals(stroke))
    {
      return;
    }

    this.awtStroke = stroke;
    final BasicStroke bstroke = (BasicStroke) stroke;
    final PdfContentByte cb = this.writer.getDirectContent();
    cb.setLineWidth(bstroke.getLineWidth());
  }

  /**
   * Returns the current stroke.
   *
   * @return the current stroke.
   */
  public Stroke getStroke()
  {
    return awtStroke;
  }

  /**
   * Sets the paint. If the paint could not be converted into a pdf object, an OutputTargetException
   * is thrown. This implementation currently supports java.awt.Color as the only valid paint.
   *
   * @param paint  the paint.
   *
   * @throws OutputTargetException if the paint is invalid.
   */
  public void setPaint(final Paint paint) throws OutputTargetException
  {
    if (paint == null)
    {
      throw new NullPointerException();
    }

    if (paint instanceof Color == false)
    {
      throw new OutputTargetException("Unsupported paint type, currently only color is supported");
    }

    // If this paint is already set, do nothing
    if (awtPaint != null && awtPaint.equals(paint))
    {
      return;
    }

    this.awtPaint = paint;
    final PdfContentByte cb = this.writer.getDirectContent();

    cb.setColorStroke((Color) paint);
    cb.setColorFill((Color) paint);
  }

  /**
   * Returns the currently set paint.
   *
   * @return the paint.
   */
  public Paint getPaint()
  {
    return awtPaint;
  }

  /**
   * returns the font encoding used in this output target.
   *
   * @return the font encoding.
   */
  private String getFontEncoding()
  {
    return getProperty(ENCODING, getDefaultFontEncoding());
  }

  /**
   * Defines the text encoding used in this output target.
   *
   * <ul>
   * <li>The Unicode encoding with horizontal writing is "Identity-H"
   * <li>The Unicode encoding with vertical writing is "Identity-V"
   * <li>"Cp1250"
   * <li>"Cp1252" is also known as WinAnsi
   * <li>"Cp1257"
   * <li>"MacRoman"
   * </ul>
   *
   * @param encoding  the font encoding.
   */
  public void setFontEncoding(final String encoding)
  {
    if (encoding == null)
    {
      throw new NullPointerException();
    }
    setProperty(ENCODING, encoding);
  }

  /**
   * Returns the 'embed fonts' flag.
   *
   * @return the 'embed fonts' flag.
   */
  private boolean isEmbedFonts()
  {
    return getProperty(EMBED_FONTS, "false").equals("true");
  }

  /**
   * Sets the 'embed fonts' flag.
   *
   * @param embedFonts  the new flag value.
   */
  private void setEmbedFonts(final boolean embedFonts)
  {
    setProperty(EMBED_FONTS, String.valueOf(embedFonts));
  }

  /**
   * Creates an output target that mimics a real output target, but produces no output.
   * This is used by the reporting engine when it makes its first pass through the report,
   * calculating page boundaries etc.  The second pass will use a real output target.
   *
   * @return a dummy output target.
   */
  public OutputTarget createDummyWriter()
  {
    /*
    PDFOutputTarget dummy = new PDFOutputTarget(new NullOutputStream(),
                                                getLogicalPage(), isEmbedFonts());
    Enumeration enum = getPropertyNames();
    while (enum.hasMoreElements())
    {
      String key = (String) enum.nextElement();
      dummy.setProperty(key, getProperty(key));
    }
    */
    return new DummyOutputTarget(this);
  }

  /**
   * Returns the document.
   *
   * @return the document.
   */
  private Document getDocument()
  {
    return pdfDocument;
  }

  /**
   * Sets the document.
   *
   * @param document  the document (null not permitted).
   */
  private void setDocument(final Document document)
  {
    if (document == null)
    {
      throw new NullPointerException();
    }
    this.pdfDocument = document;
  }

  /**
   * Configures the output target.
   *
   * @param config  the configuration.
   */
  public void configure(final ReportConfiguration config)
  {
    updateProperty(SECURITY_OWNERPASSWORD, config);
    updateProperty(SECURITY_USERPASSWORD, config);
    updateProperty(AUTHOR, config);
    updateProperty(ENCODING, config);
    updateBooleanProperty(EMBED_FONTS, config);
    updateBooleanProperty(SECURITY_ALLOW_ASSEMBLY, config);
    updateBooleanProperty(SECURITY_ALLOW_COPY, config);
    updateBooleanProperty(SECURITY_ALLOW_DEGRADED_PRINTING, config);
    updateBooleanProperty(SECURITY_ALLOW_FILLIN, config);
    updateBooleanProperty(SECURITY_ALLOW_MODIFY_ANNOTATIONS, config);
    updateBooleanProperty(SECURITY_ALLOW_MODIFY_CONTENTS, config);
    updateBooleanProperty(SECURITY_ALLOW_PRINTING, config);
    updateBooleanProperty(SECURITY_ALLOW_SCREENREADERS, config);
    updateBooleanProperty(SECURITY_OWNERPASSWORD, config);
    updateBooleanProperty(SECURITY_USERPASSWORD, config);

    // encryption needs more info: <undefined> <none> <40> <128>.
    updateProperty(SECURITY_ENCRYPTION, config);
  }

  /**
   * Updates a property.
   *
   * @param key  the key.
   * @param config  the config.
   */
  private void updateProperty(final String key, final ReportConfiguration config)
  {
    final String configValue = config.getConfigProperty(CONFIGURATION_PREFIX + key);
    final String propertyValue = getProperty(key, configValue);
    if (propertyValue != null)
    {
      setProperty(key, propertyValue);
    }
  }

  /**
   * Updates a boolean property.
   *
   * @param key  the key.
   * @param config  the config.
   */
  private void updateBooleanProperty(final String key, final ReportConfiguration config)
  {
    final String configValue = config.getConfigProperty(CONFIGURATION_PREFIX + key);
    final String propertyValue = getProperty(key, configValue);
    
    if (propertyValue.equalsIgnoreCase("true"))
    {
      setProperty(key, getProperty(key, "true"));
    }
    else
    {
      setProperty(key, getProperty(key, "false"));
    }
  }

  /**
   * Returns true if the output target is open, and false otherwise.
   *
   * @return true or false.
   */
  public boolean isOpen()
  {
    if (getDocument() == null)
    {
      //Log.debug ("Document is null, assuming that the document is closed ...");
      return false;
    }
    return getDocument().isOpen();
  }

  /**
   * A PDF size calculator.
   */
  private static class PDFSizeCalculator implements SizeCalculator
  {
    /** The base font. */
    private BaseFont baseFont;

    /** The font size. */
    private float fontSize;

    /**
     * Creates a new size calculator.
     *
     * @param font  the font.
     * @param fontSize  the font size.
     */
    private PDFSizeCalculator(final BaseFont font, final float fontSize)
    {
      this.baseFont = font;
      this.fontSize = fontSize;
    }

    /**
     * Calculates the width of the specified String in the current Graphics context.
     *
     * @param text the text to be weighted.
     * @param lineStartPos the start position of the substring to be weighted.
     * @param endPos the position of the last characterto be included in the weightening process.
     *
     * @return the width of the given string in 1/72" dpi.
     */
    public float getStringWidth(final String text, final int lineStartPos, final int endPos)
    {
      return baseFont.getWidthPoint(text.substring(lineStartPos, endPos), fontSize);
    }

    /**
     * Returns the height of the current font. The font height specifies the distance between
     * 2 base lines.
     *
     * @return  the font height.
     */
    public float getLineHeight()
    {
      return fontSize;
    }
  }

  /** The current page format. */
  private PageFormat currentPageFormat;

  /** The internal operation bounds. */
  private Rectangle2D internalOperationBounds;

  /**
   * Creates a 'size calculator' for the current state of the output target. The calculator
   * is used to calculate the string width and line height and later maybe more.
   *
   * @param font  the font.
   *
   * @return the size calculator.
   *
   * @throws OutputTargetException if there is a problem with the output target.
   */
  public SizeCalculator createTextSizeCalculator(final FontDefinition font) throws OutputTargetException
  {
    final BaseFontRecord record = fontSupport.createBaseFont(font,
        font.getFontEncoding(getFontEncoding()),
        false);
    return new PDFSizeCalculator(record.getBaseFont(), font.getFont().getSize2D());
  }

  /**
   * Sets the operation bounds.
   *
   * @param bounds  the bounds.
   */
  public void setOperationBounds(final Rectangle2D bounds)
  {
    super.setOperationBounds(bounds);
    internalOperationBounds
        = new Rectangle2D.Float((float) (bounds.getX() + currentPageFormat.getImageableX()),
            (float) (bounds.getY() + currentPageFormat.getImageableY()),
            (float) bounds.getWidth(), (float) bounds.getHeight());
  }

  /**
   * Returns the internal operation bounds.
   *
   * @return the internal operation bounds.
   */
  private Rectangle2D getInternalOperationBounds()
  {
    return internalOperationBounds;
  }

  /**
   * Draws a drawable relative to the current position.
   *
   * @param drawable the drawable to draw.
   */
  public void drawDrawable(final DrawableContainer drawable)
  {
    // cant be done using Wmf, as Wmf does not support Unicode and Bitmaps... damn

    // not yet implemented, needs WMF Converter ...
    // only the drawable clippingbounds region will be drawn.
    // the clipping is set to the clipping bounds of the drawable

    // the clipping bounds are relative to the drawable dimension,
    // they are not influenced by the drawables position on the page

    final Rectangle2D clipBounds = drawable.getClippingBounds();

    final Graphics2D target = writer.getDirectContent().createGraphics((float) clipBounds.getWidth(),
        (float) clipBounds.getHeight());
    target.translate(-clipBounds.getX(), -clipBounds.getY());
    target.clip(new Rectangle2D.Float(0, 0,
        (float) clipBounds.getWidth(),
        (float) clipBounds.getHeight()));

    final Dimension2D drawableSize = drawable.getDrawableSize();
    final Rectangle2D drawBounds = new Rectangle2D.Float(0, 0,
        (float) drawableSize.getWidth(),
        (float) drawableSize.getHeight());
    drawable.getDrawable().draw(target, drawBounds);
    target.dispose();
  }
}

⌨️ 快捷键说明

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