We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How I can improve my code on insertion of images to a word document

Hi All,
I have a function that takes the contents of a PDF file and converts to images, then takes these images and inserts a Word document, these Word document finally is converted to PDF again.

I wanted to know if I you could help me to improve the insertion of that images in the word file (see final file "Prueba doc.pdf").

Here I attached files via Dropbox:
  1. Prueba doc.pdf => final file
  2. ISO-9001 - 2008.pdf => source file where the images are obtained
  3. content.zip => the "ISO-9001 - 2008.pdf" file in images. Each image is a page.
LINK => https://www.dropbox.com/sh/u6n825nazxvqlkt/AACjWS06aMZOdhNLbKM-0FJWa?dl=0

Here I leave my code to improve:
Shape shape = builder.insertImage(file2.getAbsolutePath(),targetWidth,targetHeight);
shape.setWrapType(WrapType.TOP_BOTTOM);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PARAGRAPH);
shape.setVerticalAlignment(VerticalAlignment.TOP);
builder.insertParagraph();

My environment is WIN7 + Java6 + aspose.words-15.9.0

Best Regards
Martin Fernandez
Senior Java Developer

Hi Martin,


Thanks for your inquiry.

The Dropbox link does not work at our end. Could you please share the documents again? We will then provide you more information about your query.

PS: To attach input and output documents, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Hi Tahir, the following link has the zip with the files mentionated in the post.


https://www.dropbox.com/s/63xcx1814v7tfzs/info.zip?dl=0

Best Regards
Martin Fernandez

Hi Martin,


Thanks for sharing the detail. Please use following code example to insert images into Aspose.Words.Document and save it to Pdf.

Hope this helps you. Please let us know if you have any more queries.


Document doc = new Document();<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(doc);

// Max page size

double maxPageHeight = 1584;

double maxPageWidth = 1584;

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

for(int i = 0; i<5;i++) // loop through total number of images.

{

BufferedImage image = ImageIO.read(new File(MyDir + "info\\content\\page"+i+".jpeg"));

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

}

ps.setPageWidth(currentImageWidth);

ps.setPageHeight(currentImageHeight);

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

builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);

builder.insertParagraph();

}

doc.save(MyDir + "Out.pdf");

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

}


thanks a lot Tahir!!


Best Regards.

Martin Fernanez

Hi Martin,


Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.