1

Im able to convert the image to pdf using itext library successfully but the problem is while converting.it is not converting properly (i.e.,) the position of the exact image is dragging down like this.

this is the pdf image....this is the pdfpage image what im getting after converting the pdf file

and after converting my image back to pdf this is what i get

this is the pdf page after converting from image to pdf

this is the code im using

for converting from pdf to image

File sourceFile = new File(sourceDir);
File destinationFile = new File(destinationDir);
String fileName = sourceFile.getName().replace(".pdf", "");
if (sourceFile.exists()) {
    if (!destinationFile.exists()) {
        destinationFile.mkdir();
        System.out.println("Folder created in: "+ destinationFile.getCanonicalPath());
    }

    RandomAccessFile raf = new RandomAccessFile(sourceFile, "r");
    FileChannel channel = raf.getChannel();
    ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
    PDFFile pdf = new PDFFile(buf);
    System.out.println("Total Pages: "+ pdf.getNumPages());
    int pageNumber = 1;
    for (int i = 0; i < pdf.getNumPages(); i++) {
        PDFPage page = pdf.getPage(i+1);
        // image dimensions 
        int width = 1200;
        int height = 1400;
        // create the image
        Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        // width & height, // clip rect, // null for the ImageObserver, // fill background with white, // block until drawing is done
        Image image = page.getImage(width, height, rect, null, true, true );
        Graphics2D bufImageGraphics = bufferedImage.createGraphics();
        bufImageGraphics.drawImage(image, 0, 0, null);

        File imageFile = new File( destinationDir + "pdfImg" + i +".png" );// change file format here. Ex: .png, .jpg, .jpeg, .gif, .bmp

        ImageIO.write(bufferedImage, "png", imageFile);
        pageNumber++;                
    }
}

this is the code im using to convert it back to pdf again

Rectangle pageSize = new Rectangle(2780, 2525);
Document pdfDocument = new Document(PageSize.A4);
String pdfFilePath = outputFile;
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 (isPictureFile)
    {
        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);
}

1 Answer 1

4

Change the image size here :

// image dimensions 
            int width = 700; // here
            int height = 600;//here

your image is out of pdf screenSize... reduce it to as it will accept in pdf..

Sign up to request clarification or add additional context in comments.

2 Comments

thanx allot dude such a simple answer and it worked for me thanx allot dude :)
If you got your solution from this answer means, accept this answer to keep it in solved questions..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.