Valentin Grégoire
Valentin Grégoire

Reputation: 1150

Convert image to pdf with iText and Java

I succesfully converted image files (gif, png, jpg, bmp) to pdf's using iText 1.3. I can't change the version since we can't just change versions of a jar obviously in a professional environment.

The problem that I have is that the size of the image in the pdf is larger than the image itself. I am not talking about the file size but about the size of the image when the zoom is set to 100% on both the original image file and the pdf. The pdf shows the image about a 20% to 30% bigger than the original image.

What am I doing wrong?

    public void convertOtherImages2pdf(byte[] in, OutputStream out, String title, String author) throws IOException {
        Image image = Image.getInstance(in);
        Rectangle imageSize = new Rectangle(image.width() + 1f, image.height() + 1f);
        image.scaleAbsolute(image.width(), image.height());
        com.lowagie.text.Document document = new com.lowagie.text.Document(imageSize, 0, 0, 0, 0);
        PdfWriter writer = PdfWriter.getInstance(document, out);
        document.open();
        document.add(image);
        document.close();
        writer.close();
    }

Upvotes: 1

Views: 9177

Answers (2)

rashedmedisys
rashedmedisys

Reputation: 79

Make MultipleImagesToPdf Class

 public void imagesToPdf(String destination, String pdfName, String imagFileSource) throws IOException, DocumentException {

    Document document = new Document(PageSize.A4, 20.0f, 20.0f, 20.0f, 150.0f);
    String desPath = destination;

    File destinationDirectory = new File(desPath);
    if (!destinationDirectory.exists()){
        destinationDirectory.mkdir();
        System.out.println("DESTINATION FOLDER CREATED -> " + destinationDirectory.getAbsolutePath());
    }else if(destinationDirectory.exists()){
        System.out.println("DESTINATION FOLDER ALREADY CREATED!!!");
    }else{
        System.out.println("DESTINATION FOLDER NOT CREATED!!!");
    }

    File file = new File(destinationDirectory, pdfName + ".pdf");

    FileOutputStream fileOutputStream = new FileOutputStream(file);

    PdfWriter pdfWriter = PdfWriter.getInstance(document, fileOutputStream);
    document.open();

    System.out.println("CONVERTER START.....");

    String[] splitImagFiles = imagFileSource.split(",");

    for (String singleImage : splitImagFiles) {
    Image image = Image.getInstance(singleImage);
    document.setPageSize(image);
    document.newPage();
    image.setAbsolutePosition(0, 0);
        document.add(image);
    }

    document.close();
    System.out.println("CONVERTER STOPTED.....");
}



public static void main(String[] args) {

    try {
        MultipleImagesToPdf converter = new MultipleImagesToPdf();
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your destination folder where save PDF \n");
        // Destination = D:/Destination/;
        String destination = sc.nextLine();

        System.out.print("Enter your PDF File Name \n");
        // Name = test;
        String name = sc.nextLine();

        System.out.print("Enter your selected image files name with source folder \n");
        String sourcePath = sc.nextLine();
        // Source = D:/Source/a.jpg,D:/Source/b.jpg;
        if (sourcePath != null || sourcePath != "") {
            converter.imagesToPdf(destination, name, sourcePath);
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

=================================

Here i use itextpdf-5.4.0 Library

Happy Coding :)

Upvotes: 3

Paulo Soares
Paulo Soares

Reputation: 1916

You need to scale the image by the dpi.

float scale = 72 / dpi;

I don't know if such an ancient iText has that image information, more recent iText versions have it.

Upvotes: 2

Related Questions