Hi,
Is there any API to create a thumbnail image for a Word Document formats using aspose java API. Please reply me.
Hi,
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
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(); }