How to Create PDF Document from an Image inside Android Applications

This technical tip shows how Android developers can create a PDF document from an image using Aspose.Words inside their Android application. While converting images to PDF is not a main feature of Aspose.Words, this example shows how easy it is. To make this code work, add references to Aspose.Words, javax.imageio and java.awt.image to the project. The code below converts single frame images, such as JPEG, PNG or BMP, as well as multi-frame GIF images, to PDF.
//your code here...The following code snippet shows how to create a PDF document from an image package ImageToPdf; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import java.awt.image.BufferedImage; import java.io.File; import java.net.URI; import com.aspose.words.Document; import com.aspose.words.DocumentBuilder; import com.aspose.words.BreakType; import com.aspose.words.PageSetup; import com.aspose.words.ConvertUtil; import com.aspose.words.RelativeHorizontalPosition; import com.aspose.words.RelativeVerticalPosition; import com.aspose.words.WrapType; class Program { public static void main(String[] args) throws Exception { // Sample infrastructure. URI exeDir = Program.class.getResource("").toURI(); String dataDir = new File(exeDir.resolve("../../Data")) + File.separator; convertImageToPdf(dataDir + "Test.jpg", dataDir + "TestJpg Out.pdf"); convertImageToPdf(dataDir + "Test.png", dataDir + "TestPng Out.pdf"); convertImageToPdf(dataDir + "Test.bmp", dataDir + "TestBmp Out.pdf"); convertImageToPdf(dataDir + "Test.gif", dataDir + "TestGif Out.pdf"); } /** * Converts an image to PDF using Aspose.Words for Java. * * @param inputFileName File name of input image file. * @param outputFileName Output PDF file name. */ public static void convertImageToPdf(String inputFileName, String outputFileName) throws Exception { // Create Aspose.Words.Document and DocumentBuilder. // The builder makes it simple to add content to the document. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Load images from the disk using the approriate reader. // The file formats that can be loaded depends on the image readers available on the machine. ImageInputStream iis = ImageIO.createImageInputStream(new File(inputFileName)); ImageReader reader = ImageIO.getImageReaders(iis).next(); reader.setInput(iis, false); try { // Get the number of frames in the image. int framesCount = reader.getNumImages(true); // Loop through all frames. for (int frameIdx = 0; frameIdx < framesCount; frameIdx++) { // Insert a section break before each new page, in case of a multi-frame image. if (frameIdx != 0) builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); // Select active frame. BufferedImage image = reader.read(frameIdx); // We want the size of the page to be the same as the size of the image. // Convert pixels to points to size the page to the actual image size. PageSetup ps = builder.getPageSetup(); ps.setPageWidth(ConvertUtil.pixelToPoint(image.getWidth())); ps.setPageHeight(ConvertUtil.pixelToPoint(image.getHeight())); // Insert the image into the document and position it at the top left corner of the page. builder.insertImage( image, RelativeHorizontalPosition.PAGE, 0, RelativeVerticalPosition.PAGE, 0, ps.getPageWidth(), ps.getPageHeight(), WrapType.NONE); } } finally { if (iis != null) { iis.close(); reader.dispose(); } } // Save the document to PDF. doc.save(outputFileName); } } //Converts large size image into a PDF document //When the size of image is bigger than max allowed size of MS Word page, an exception is thrown or image is not visible in output document. The measurement for images must be between 0 and 55.88 cm (55.88 cm = 1584 points). In this case, use the following code example. package ImageToPdf; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import java.awt.image.BufferedImage; import java.io.File; import java.net.URI; import com.aspose.words.Document; import com.aspose.words.DocumentBuilder; import com.aspose.words.BreakType; import com.aspose.words.PageSetup; import com.aspose.words.ConvertUtil; import com.aspose.words.RelativeHorizontalPosition; import com.aspose.words.RelativeVerticalPosition; import com.aspose.words.WrapType; class Program { public static void main(String[] args) throws Exception { // Sample infrastructure. URI exeDir = Program.class.getResource("").toURI(); String dataDir = new File(exeDir.resolve("../../Data")) + File.separator; convertImageToPdf(dataDir + "Test.jpg", dataDir + "TestJpg Out.pdf"); convertImageToPdf(dataDir + "Test.png", dataDir + "TestPng Out.pdf"); convertImageToPdf(dataDir + "Test.bmp", dataDir + "TestBmp Out.pdf"); convertImageToPdf(dataDir + "Test.gif", dataDir + "TestGif Out.pdf"); } /** * Converts an image to PDF using Aspose.Words for Java. * * @param inputFileName File name of input image file. * @param outputFileName Output PDF file name. */ public static void convertImageToPdf(String inputFileName, String outputFileName) throws Exception { // Create Aspose.Words.Document and DocumentBuilder. // The builder makes it simple to add content to the document. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Load images from the disk using the approriate reader. // The file formats that can be loaded depends on the image readers available on the machine. ImageInputStream iis = ImageIO.createImageInputStream(new File(inputFileName)); ImageReader reader = ImageIO.getImageReaders(iis).next(); reader.setInput(iis, false); try { // Get the number of frames in the image. int framesCount = reader.getNumImages(true); // Loop through all frames. for (int frameIdx = 0; frameIdx < framesCount; frameIdx++) { // Insert a section break before each new page, in case of a multi-frame image. if (frameIdx != 0) builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); // Select active frame. BufferedImage image = reader.read(frameIdx); // Max page size double maxPageHeight = 1584; double maxPageWidth = 1584; double currentImageHeight = ConvertUtil.pixelToPoint(image.getHeight()); double currentImageWidth = ConvertUtil.pixelToPoint(image.getWidth()); if (currentImageWidth >= maxPageWidth || currentImageHeight >= maxPageHeight) { // Get max image size. double[] size = CalculateImageSize(image, maxPageHeight, maxPageWidth, currentImageHeight, currentImageWidth); currentImageWidth = size[0]; currentImageHeight = size[1]; } // We want the size of the page to be the same as the size of the image. // Convert pixels to points to size the page to the actual image size. PageSetup ps = builder.getPageSetup(); ps.setPageWidth(currentImageWidth); ps.setPageHeight(currentImageHeight); // Insert the image into the document and position it at the top left corner of the page. Shape shape = builder.insertImage( image, RelativeHorizontalPosition.PAGE, 0, RelativeVerticalPosition.PAGE, 0, ps.getPageWidth(), ps.getPageHeight(), WrapType.NONE); resizeLargeImage(shape); } } finally { if (iis != null) { iis.close(); reader.dispose(); } } // Save the document to PDF. doc.save(outputFileName); } public static double[] CalculateImageSize(BufferedImage img, double containerHeight, double containerWidth, double targetHeight, double targetWidth) throws Exception { targetHeight = containerHeight; targetWidth = containerWidth; //Get size of an image double imgHeight = ConvertUtil.pixelToPoint(img.getHeight()); double imgWidth = ConvertUtil.pixelToPoint(img.getWidth()); if (imgHeight < targetHeight && imgWidth < targetWidth) { targetHeight = imgHeight; targetWidth = imgWidth; } else { //Calculate size of an image in the document double ratioWidth = imgWidth / targetWidth; double ratioHeight = imgHeight / targetHeight; if (ratioWidth > ratioHeight) targetHeight = (targetHeight * (ratioHeight / ratioWidth)); else targetWidth = (targetWidth * (ratioWidth / ratioHeight)); } double[] size = new double[2]; size[0] = targetWidth; //width size[1] = targetHeight; //height return(size); } public static void resizeLargeImage(Shape image) throws Exception { // Return if this shape is not an image. if (!image.hasImage()) return; // Calculate the free space based on an inline or floating image. If inline we must take the page margins into account. PageSetup ps = image.getParentParagraph().getParentSection().getPageSetup(); double freePageWidth = image.isInline() ? ps.getPageWidth() - ps.g

Url: http://www.aspose.com/android/pdf-component.aspx

Language: Java | User: Sheraz Khan | Created: Nov 26, 2014 | Tags: Create PDF Document from Image convert image to PDF Android image to PDF Conversion Android PDF API converts single frame images to PDF JPEG to PDF in Android PNG to PDF in Android converting images to PDF