Reputation: 528
i am using the iText library to create a pdf by selecting images. Everything works great but i am facing an issue with this.
When the height of the image is greater than the size of the document then the images looks wierd in the pdf.
below is my code to create the pdf. And i am also attaching the snapshot of the pdf.
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDFfiles/");
Path path = path + filename +
".pdf";
File f = new File(file, filename +
".pdf");
Log.v("stage 1", "store the pdf in sd card");
//t.append("store the pdf in sd card\n");
Document document = new Document(PageSize.A5, 38, 38, 50, 38);
Log.v("stage 2", "Document Created");
//t.append("Document Created\n");
Rectangle documentRect = document.getPageSize();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
Log.v("Stage 3", "Pdf writer");
// t.append("Pdf writer\n");
document.open();
Log.v("Stage 4", "Document opened");
// t.append("Document opened\n");
for (int i = 0; i < imagesuri.size(); i++)
{
Bitmap bmp = BitmapFactory.decodeFile(imagesuri.get(i));
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 70, stream);
image = Image.getInstance(imagesuri.get(i));
if (bmp.getWidth() > documentRect.getWidth() || bmp.getHeight() > documentRect.getHeight()) {
//bitmap is larger than page,so set bitmap's size similar to the whole page
image.scaleAbsolute(documentRect.getWidth(), documentRect.getHeight());
} else {
//bitmap is smaller than page, so add bitmap simply.\[note: if you want to fill page by stretching image, you may set size similar to page as above\]
image.scaleAbsolute(bmp.getWidth(), bmp.getHeight());
}
Log.v("Stage 6", "Image path adding");
image.setAbsolutePosition((documentRect.getWidth() - image.getScaledWidth()) / 2, (documentRect.getHeight() - image.getScaledHeight()) / 2);
Log.v("Stage 7", "Image Alignments");
isPdfCreated = true;
image.setBorder(Image.BOX);
image.setBorderWidth(15);
document.add(image);
document.newPage();
}
Log.v("Stage 8", "Image adding");
// t.append("Image adding\n");
document.close();
Log.v("Stage 7", "Document Closed" + path);
// t.append("Document Closed\n");
} catch (Exception e) {
e.printStackTrace();
}
document.close();
imagesuri.clear()
Upvotes: 0
Views: 817
Reputation: 77528
When you scale absolute, you change the aspect ratio of the image.
For instance: if an image measures 600 by 800, then the width is 75% of the height. If you want to fit this image on a page that measures 400 by 600, you change this percentage to 66%. That will deform the image.
You act as if this surprises you (you say that the images look weird), but that's a normal phenomenon: changing the aspect ratio will always stretch the image one way or the other. For instance: if the image is the picture of someone's face, the face will become either wider (making the person look fatter) or higher (making the person look thinner).
You shouldn't use the scaleAbsolute()
if you want to avoid this. You should replace that method by the scaleToFit()
method. The scaleToFit()
method scales the image, but it preserves the aspect ratio.
For instance: if we have an image of 600 by 800, and you scale it to fit a 400 by 600 rectangle, the image will be scaled to 400 by 533. When you add this scaled image to the rectangle of 400 by 600, you will have 67 user units of extra white space, but that's not to be avoided if you want to preserve the aspect ration (and if you don't want your images to look weird).
It would be a much better idea to adapt the page size to the size of the images, but maybe your application requires a specific page size.
Upvotes: 1