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

📄 pdfoutputtarget.java

📁 swing编写的库存管理程序。毕业设计类
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    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);

    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()
  {
    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);
    updateProperty(PDF_VERSION, 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);
    // property is neither set in the configuration nor in the properties ...
    if (propertyValue == null)
    {
      return;
    }

    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)
    {
      return false;
    }
    return getDocument().isOpen();
  }

  /** 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
  {
    try
    {
      final BaseFontRecord record = fontSupport.createBaseFont(font,
          font.getFontEncoding(getFontEncoding()),
          false);
      return new PDFSizeCalculator(record.getBaseFont(), font.getFont().getSize2D());
    }
    catch (BaseFontCreateException bfce)
    {
      throw new OutputTargetException("The font definition was not successfull.", bfce);
    }
  }

  /**
   * 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

    // todo: Whats going on here?
//    final Rectangle2D operationBounds = getOperationBounds();
//    final Rectangle2D clipBounds = drawable.getClippingBounds();
//
//    final Graphics2D target =
//        writer.getDirectContent().createGraphics
//        ((float) clipBounds.getWidth(), (float) clipBounds.getHeight());
//    target.translate
//        (operationBounds.getX() + clipBounds.getX(),
//         operationBounds.getY() + 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();

    final Rectangle2D bounds = getInternalOperationBounds();
    final float x = (float) (bounds.getX());
    final float y = (float) (bounds.getY());

    final Rectangle2D clipBounds = drawable.getClippingBounds();

    final Graphics2D target = writer.getDirectContent().createGraphics(
        (float) clipBounds.getWidth() + x, (getPageHeight() - y));

    target.translate(x, 0);

    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();
  }

  /**
   * Returns the PDF encoding property value.
   *
   * @return the PDF encoding property value.
   */
  public static String getDefaultPDFEncoding()
  {
    return ReportConfiguration.getGlobalConfig().getConfigProperty
        (PDFTARGET_ENCODING, PDFTARGET_ENCODING_DEFAULT);
  }

  /**
   * Sets the PDF encoding property value.
   *
   * @param pdfTargetEncoding  the new encoding.
   */
  public static void setDefaultPDFEncoding(final String pdfTargetEncoding)
  {
    ReportConfiguration.getGlobalConfig().setConfigProperty
        (PDFTARGET_ENCODING, pdfTargetEncoding);
  }

  /**
   * Returns true, if the Graphics2D should use aliasing to render text. Defaults to false.
   *
   * @return true, if aliasing is enabled.
   */
  public static boolean isDefaultEmbedFonts()
  {
    return ReportConfiguration.getGlobalConfig().getConfigProperty
        (PDFTARGET_EMBED_FONTS, PDFTARGET_EMBED_FONTS_DEFAULT).equalsIgnoreCase("true");
  }

  /**
   * set to true, if the PDFOutputTarget should embed all fonts.
   *
   * @param embed set to true, if the PDFOutputTarget should use embedded fonts.
   */
  public static void setDefaultEmbedFonts(final boolean embed)
  {
    ReportConfiguration.getGlobalConfig().setConfigProperty
        (PDFTARGET_EMBED_FONTS, String.valueOf(embed));
  }


}

⌨️ 快捷键说明

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