Create thumbnail image for Word document pages using Java

Hi,

Is there any API to create a thumbnail image for a Word Document formats using aspose java API. Please reply me.

Hi Vignesh,

Thanks for your query. Please check the RenderToSize method of Document Class. You can use the example mentioned in documentation of RenderToSize to generate thumbnails.

Please let us know, If you have any more queries.

Thanks for you reply. I tried the sample code of renderToSize and renderToScale API like


public void renderToSize(Document doc){
// Bitmap bmp = new Bitmap(700, 700);
BufferedImage img = new BufferedImage(700, 700, BufferedImage.TYPE_INT_ARGB);
// User has some sort of a Graphics object. In this case created from a bitmap.
Graphics2D gr = img.createGraphics();
try
{
// The user can specify any options on the Graphics object including
// transform, antialiasing, page units, etc.
gr.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

// The output should be offset 0.5" from the edge and rotated.
gr.translate(0.5f, 0.5f);
gr.rotate(10.0 * Math.PI / 180.0, img.getWidth() / 2.0, img.getHeight() / 2.0);

// This is our test rectangle.
gr.drawRect(0, 0, 3, 3);

// User specifies (in world coordinates) where on the Graphics to render and what size.
float returnedScale = doc.renderToSize(0, gr, 0f, 0f, 3f, 3f);

// This is the calculated scale factor to fit 297mm into 3".
System.out.println(MessageFormat.format(“The image was rendered at {0}% zoom.”, returnedScale * 100));

ImageIO.write(img, “PNG”, new File(“C:\Users\vignesh-1200\Desktop\renderToSize.PNG”));
}catch(Exception e){
}
finally { if (gr != null) gr.dispose(); }
}

and

public void renderToScale(Document doc){
try
{
PageInfo pageInfo = doc.getPageInfo(0);

// Let’s say we want the image at 50% zoom.
float MY_SCALE = 0.50f;

Dimension pageSize = pageInfo.getSizeInPixels(MY_SCALE, 96.0f);

BufferedImage img = new BufferedImage((int)pageSize.getWidth(), (int)pageSize.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = img.createGraphics();
// You can apply various settings to the Graphics object.
gr.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

// Fill the page background.
gr.setPaint(Color.black);

// Render the page using the zoom.
doc.renderToScale(0, gr, 0, 0, MY_SCALE);
ImageIO.write(img, “PNG”, new File(“C:\Users\vignesh-1200\Desktop\renderToScale.PNG”));

}catch(Exception e){
}
}


I have attached the input document and output image for both the API sample code. The output Thumbnail was not sharp. Please help me in working with these APIs.

Any Updates on this?

Hi Vignesh,

I have used the code mentioned at following documentation link under renderToScale method and thumbnail output look correct to me. Please use the following code snippet and let us know, If you have any more queries. I have attached the output thumbnail with this post.

// The user opens or builds a document.

Document doc = new Document("D:\\Thumbnail.docx");

// This defines the number of columns to display the thumbnails in.

final int THUMB_COLUMNS = 1;

// Calculate the required number of rows for thumbnails.

// We can now get the number of pages in the document.

int thumbRows = doc.getPageCount() / THUMB_COLUMNS;

int remainder = doc.getPageCount() % THUMB_COLUMNS;

if (remainder > 0)

thumbRows++;

// Lets say I want thumbnails to be of this zoom.

float SCALE = 0.25f;

// For simplicity lets pretend all pages in the document are of the same size,

// so we can use the size of the first page to calculate the size of the thumbnail.

Dimension thumbSize = doc.getPageInfo(0).getSizeInPixels(SCALE, 96);

// Calculate the size of the image that will contain all the thumbnails.

int imgWidth = (int)(thumbSize.getWidth() * THUMB_COLUMNS);

int imgHeight = (int)(thumbSize.getHeight() * thumbRows);

BufferedImage img = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);

// The user has to provides a Graphics object to draw on.

// The Graphics object can be created from a bitmap, from a metafile, printer or window.

Graphics2D gr = img.createGraphics();

try

{

gr.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

gr.setColor(Color.white);

// Fill the "paper" with white, otherwise it will be transparent.

gr.fillRect(0, 0, imgWidth, imgHeight);

for (int pageIndex = 0; pageIndex < doc.getPageCount(); pageIndex++)

{

int rowIdx = pageIndex / THUMB_COLUMNS;

int columnIdx = pageIndex % THUMB_COLUMNS;

// Specify where we want the thumbnail to appear.

float thumbLeft = (float)(columnIdx * thumbSize.getWidth());

float thumbTop = (float)(rowIdx * thumbSize.getHeight());

Point2D.Float size = doc.renderToScale(pageIndex, gr, thumbLeft, thumbTop, SCALE);

gr.setColor(Color.black);

// Draw the page rectangle.

gr.drawRect((int)thumbLeft, (int)thumbTop, (int)size.getX(), (int)size.getY());

}

ImageIO.write(img, "PNG", new File("D:\\Rendering.Thumbnails Out.png"));

}

finally { if (gr != null) gr.dispose(); }