public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("ImageRotation.pdf"));
      document.open();

      //
      // Rotate image in radian using the setRotation method.
      //
      String filename = "other-sample/src/main/resources/java.gif";
      Image image = Image.getInstance(filename);
      image.setRotation(90f);
      document.add(image);

      //
      // Rotate image in degree using the setRotationDegree method
      //
      String url = "http://localhost/xampp/img/xampp-logo-new.gif";
      image = Image.getInstance(url);
      image.setRotationDegrees(90);
      document.add(image);
    } catch (DocumentException | IOException e) {
      e.printStackTrace();
    } finally {
      document.close();
    }
  }
Exemple #2
0
  /**
   * @param imageUrllist
   * @param mOutputPdfFileName
   * @return
   * @throws FileNotFoundException
   * @throws BadElementException
   * @throws MalformedURLException
   * @throws DocumentException
   * @throws IOException
   */
  public static File convertPicToPdf(List<String> imageUrllist, String mOutputPdfFileName)
      throws FileNotFoundException, BadElementException, MalformedURLException, DocumentException,
          IOException {
    Document doc = new Document(PageSize.A4, 10, 10, 10, 10);
    FileOutputStream fileOutputStream = new FileOutputStream(mOutputPdfFileName);
    PdfWriter.getInstance(doc, fileOutputStream);
    float height, width;
    Image image;
    int percent;
    doc.open();
    for (String string : imageUrllist) {
      doc.newPage();
      image = Image.getInstance(string);
      image.setAlignment(Image.MIDDLE);
      height = image.getHeight();
      width = image.getWidth();
      if (readPictureDegree(string) == 90) {
        // 如果图片需要旋转,设置高度为宽度,宽度为高度
        height = image.getWidth();
        width = image.getHeight();
      }
      // 旋转图片
      image.setRotationDegrees(-readPictureDegree(string));
      System.out.println(height);
      System.out.println(width);
      percent = getPercent2(height, width);
      System.out.println(percent);

      image.scalePercent(percent); // 表示是原来图像的比例;
      doc.add(image);
      System.out.println("convert " + string + "successful");
    }
    doc.close();
    File mOutputPdfFile = new File(mOutputPdfFileName);
    if (!mOutputPdfFile.exists()) {
      mOutputPdfFile.deleteOnExit();
      return null;
    }
    return mOutputPdfFile;
  }