Beruflich Dokumente
Kultur Dokumente
This technical tip shows how developers can create a PDF document from an image
using Aspose.Words inside their Android application. The Image class in Aspose.Pdf
makes it possible to convert an image from various sources into PDF format. This
may include an image from particular location over the hard-drive, an image from
MemoryStream or an image at a web location. The ImageInfo class has a method named
setMemoryData() which is used to specify MemoryStream as the image source. 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).
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
{
// Sample infrastructure.
/**
*/
// The file formats that can be loaded depends on the image readers
available on the machine.
reader.setInput(iis, false);
try
if (frameIdx != 0)
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
// We want the size of the page to be the same as the size of the
image.
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();
doc.save(outputFileName);
//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
// Sample infrastructure.
/**
* Converts an image to PDF using Aspose.Words for Java.
*/
// The file formats that can be loaded depends on the image readers
available on the machine.
reader.setInput(iis, false);
try
if (frameIdx != 0)
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
double currentImageHeight =
ConvertUtil.pixelToPoint(image.getHeight());
double currentImageWidth =
ConvertUtil.pixelToPoint(image.getWidth());
currentImageWidth = size[0];
currentImageHeight = size[1];
// We want the size of the page to be the same as the size of the
image.
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.
image,
RelativeHorizontalPosition.PAGE,
0,
RelativeVerticalPosition.PAGE,
0,
ps.getPageWidth(),
ps.getPageHeight(),
WrapType.NONE);
resizeLargeImage(shape);
finally {
if (iis != null) {
iis.close();
reader.dispose();
doc.save(outputFileName);
targetHeight = containerHeight;
targetWidth = containerWidth;
targetHeight = imgHeight;
targetWidth = imgWidth;
else
else
if (!image.hasImage())
return;
PageSetup ps =
image.getParentParagraph().getParentSection().getPageSetup();
// Is one of the sides of this image too big for the page?
if (exceedsMaxPageSize)
// Calculate the ratio to fit the page size based on which side is
longer.
image.setWidth(size.getWidthPoints() * ratio);
image.setHeight(size.getHeightPoints() * ratio);