Search code examples
androiditext

Convert image to PDF in Java Android


I need to convert an image to PDF file in my Android application but i found two library iTextG and iText. Are they open source?


Solution

  • YES, iText is an open source library. Infomation from itext oficial site: "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)."

    and you can use it like that:

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