I've been trying to create a method that converts an image such as a BMP into a PDF and have the created PDF maintain the same dimentions. For example If I look at the height and width of my BMP file in ms paint it is 0.87" Wide and 0.92" High. Unfortunately when looked at in adobe acrobat the height of the converted file is 1.15"Wide and 1.22" High.
I'm new to iText and have struggled with many of the examples being for older incompatible versions.
Here is the code that I used to convert the image to PDF:
public static byte[] convertImageBytesToPdfBytes(byte[] imgBytes) {
ByteArrayOutputStream returnSM = new ByteArrayOutputStream();
try {
PdfWriter writer = new PdfWriter(returnSM);
// Creating a PdfDocument
PdfDocument pdfDoc = new PdfDocument(writer);
//PdfDocument doc = new PdfDocument(new PdfReader(src), new PdfWriter(returnSM));
// Creating an ImageData object
ImageData data = ImageDataFactory.create(imgBytes);
// Creating an Image object
Image img = new Image(data);
img.setBorder(Border.NO_BORDER);
float imgWidth = img.getImageWidth();
float imgHeight = img.getImageHeight();
img.setFixedPosition(0, 0);
Rectangle rect = new Rectangle(imgWidth, imgHeight);
PageSize pagesize = new PageSize(rect);
pdfDoc.setDefaultPageSize(pagesize);
// Creating a Document Document
Document document = new Document(pdfDoc, pagesize);
document.setMargins(0, 0, 0, 0);
// Adding image to the document
document.add(img);
// Closing the document
document.close();
writer.close();
}
catch (Exception e) {
e.printStackTrace();
}
return returnSM.toByteArray();
}