Thursday, 10 November 2016

Convert Image to PDF

package com.gb.samples;

import java.io.File;
import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;

public class Sample {

public static void main(String[] args) {
Rectangle pageSize = new Rectangle(2780, 2525);
Document pdfDocument = new Document(PageSize.A4);
String pdfFilePath = "C:/Users/vgrandhi/Desktop/sam/Converted_Image_to_PDF.pdf";
String inputFile = "C:/Users/vgrandhi/Desktop/sam/Converted_PdfFiles_to_Image/org_cropped.png";
try
{
    FileOutputStream fileOutputStream = new FileOutputStream(pdfFilePath);
    PdfWriter writer = null;
    writer = PdfWriter.getInstance(pdfDocument, fileOutputStream);
    writer.open();
    pdfDocument.open();
    /**
    * Proceed if the file given is a picture file
    */
    if (true)
    {
        pdfDocument.add(com.itextpdf.text.Image.getInstance(inputFile));
    }
    /**
    * Proceed if the file given is (.txt,.html,.doc etc)
    */
    else
    {
    File file = new File(inputFile);
    //pdfDocument.add(new Paragraph(org.apache.commons.io.FileUtils.readFileToString(file)));
    }

    pdfDocument.close();
    writer.close();
}
catch (Exception exception)
{
    System.out.println("Document Exception!" + exception);
}
}
}

No comments:

Post a Comment