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

📄 pdfoutputtarget.java

📁 swing编写的库存管理程序。毕业设计类
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
   * java.awt.Image object only, the image is recoded into an PNG-Image.
   *
   * @param imageRef  the image reference.
   *
   * @return an image.
   *
   * @throws DocumentException if no PDFImageElement could be created using the given
   *                           ImageReference.
   * @throws IOException if the image could not be read.
   */
  private Image getImage(final ImageReference imageRef) throws DocumentException, IOException
  {
    final Rectangle2D bounds = getInternalOperationBounds();
    final Rectangle2D imageBounds = imageRef.getBoundsScaled();

    try
    {
      final Rectangle2D drawArea = new Rectangle2D.Float(0, 0, (float) bounds.getWidth(),
          (float) bounds.getHeight());
      if ((imageRef.getSourceURL() != null) && (drawArea.contains(imageBounds)))
      {
        return Image.getInstance(imageRef.getSourceURL());
      }
    }
    catch (BadElementException be)
    {
      Log.info("Caught illegal Image, will recode to PNG instead", be);
    }
    catch (IOException ioe)
    {
      Log.info("Unable to read the raw-data, will try to recode image-data.", ioe);
    }

    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();
            break;
          }
        default:
          {
            Log.warn("Unexpected path iterator type: " + cmd);
          }
      }
      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();
            break;
          }
        default:
          {
            Log.warn("Unexpected path iterator type: " + cmd);
          }
      }
      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);
    }
  }

  /**
   * Extracts the to be generated PDF version as iText parameter from the
   * given property value. The value has the form "1.x" where x is the extracted
   * version.
   * 
   * @param version the version string.
   * @return the itext character defining the version.
   */
  private char getVersion(final String version)
  {
    if (version == null)
    {
      return '4';
    }
    if (version.length() < 3)
    {
      Log.warn("PDF version specification is invalid.");
      return '4';
    }
    final char retval = version.charAt(2);
    if (retval < '2' || retval > '5')
    {
      Log.warn("PDF version specification is invalid.");
      return '4';
    }
    return retval;
  }

  /**
   * 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.LANDSCAPE)
//      {
//        pageSize.rotate();
//      }
//      else if (pageFormat.getOrientation() == PageFormat.REVERSE_LANDSCAPE)
//      {
//        pageSize.rotate();
//        pageSize.rotate();
//        pageSize.rotate();
//      }
//
      setDocument(new Document(pageSize, marginLeft, marginRight, marginTop, marginBottom));

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

      final char version = getVersion(getProperty(PDF_VERSION, PDF_VERSION_DEFAULT));
      writer.setPdfVersion(version);

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

⌨️ 快捷键说明

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