在 Java Android 中将图像转换为 PDF

Convert image to PDF in Java Android

我需要在我的 Android 应用程序中 convert an image to PDF file,但我发现了两个库 iTextGiText。 它们是开源的吗?

YEARS,iText 是一个开源库。信息形式 itext 官方网站: "iText is a free/open source software (F/OSS) project, giving you a lot of freedom and flexibility.... You have to respect the Affero General Public License (AGPL)."

你可以这样使用它:

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.Image;


public class ImageToPDF {
  public static void main(String ... args) {
    Document document = new Document();
    String input = "c:/temp/capture.png"; // .gif and .jpg are ok too!
    String output = "c:/temp/capture.pdf";
    try {
      FileOutputStream fos = new FileOutputStream(output);
      PdfWriter writer = PdfWriter.getInstance(document, fos);
      writer.open();
      document.open();
      document.add(Image.getInstance(input));
      document.close();
      writer.close();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}