Free Support Forum - aspose.com

Multipage tiff image

Hi,

how can i insert a multframe tiff image into a word document.

DocumentBuilder builder = DocumentBuilder(doc);
builder.insertImage( “example.tif”);

inserts only the first page.

Best regards
Joachim

Hi Joachim,


Thanks for your inquiry. Please check following code snippet, it will help you to accomplish the task. However, if the issue persists then please share your sample image file here, we will test the scenario and will guide you accordingly.

public static void
convertImageToPdf(String inputFileName, String outputFileName) throws Exception<o:p></o:p>

{

// Create Aspose.Words.Document and DocumentBuilder.

// The builder makes it simple to add content to the document.

com.aspose.words.Document doc = new com.aspose.words.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);

}

}

finally {

if (iis != null) {

iis.close();

reader.dispose();

}

}

// Save the document

doc.save(outputFileName);

}

public static double[] CalculateImageSize(BufferedImage img, double containerHeight, double containerWidth, double targetHeight, double targetWidth) throws Exception {

// Calculate width and height

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);

}


Best Regards,

Thank you, it was exact what i need!