Set zoom on a document in Aspose words

Hi,

I am following the below code to set zoom property on a document. However, I do not see any change in the way the output looks. Can you please let me know if I am missing something ?

com.aspose.words.Document document = wordDocument.getDocument();
com.aspose.words.Document doc1 =  document.extractPages(pageNum-1,1);
// doc1.getViewOptions().setViewType(ViewType.PAGE);
doc1.getViewOptions().setZoomPercent(10);
doc1.save(os,com.aspose.words.SaveFormat.PNG);
doc1.save("C:/temp/sample.png", SaveFormat.PNG);

@sushma1509 Document.ViewOptions property provides options to control how the document is displayed in Microsoft Word. In your case the document is saved to PNG, not in MS Word document format, so these options does not have any effect. This is expected.
If it is required so scale the image while saving, you can consider using Document.renderToSize or Document.renderToScale methods.

Hi @alexey.noskov
Thank you for the reply.
I followed the below code (exactly as the example), but however the converted png is not correct.
I am attaching both input and output file. Can you please let me know what am i missing ?

BufferedImage img = new BufferedImage(700, 700, BufferedImage.TYPE_INT_ARGB);
g2D = img.createGraphics();
g2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

// Offset the output 0.5" from the edge.
g2D.translate(ConvertUtil.inchToPoint(0.5f), ConvertUtil.inchToPoint(0.5f));

// Rotate the output by 10 degrees.
g2D.rotate(10.0 * Math.PI / 180.0, img.getWidth() / 2.0, img.getHeight() / 2.0);

g2D.setColor(Color.RED);

// Draw a 3"x3" rectangle.
g2D.drawRect(0, 0, (int)ConvertUtil.inchToPoint(3), (int)ConvertUtil.inchToPoint(3));

// Draw the first page of our document with the same dimensions and transformation as the rectangle.
// The rectangle will frame the first page.

float returnedScale = wordDocument.getDocument().renderToSize(pageNum - 1, g2D, 0f, 0f, (float)ConvertUtil.inchToPoint(3), (float)ConvertUtil.inchToPoint(3));
//wordDocument.getDocument().renderToSize(pageNum, gr, 0f, 0f, (float)ConvertUtil.inchToPoint(3), (float) ConvertUtil.inchToPoint(3));
ImageIO.write(img, "PNG", new File("C:/temp/converter.png"));

input.zip (16.5 KB)

output.zip (3.9 KB)

@sushma1509 The output is expected, the page is rendered to the specified rectangle. What is your expected output?

As I can see you are using Aspose.Words in evaluation mode and in your output there is an evaluation watermark. If you would like to test Aspose.Words without evaluation version limitations, you can request a free 30-days temporary license .
Please see our documentation to learn more about licensing:
https://docs.aspose.com/words/java/licensing/

@alexey.noskov

It is an empty page with only watermark . Am I right ? I do not see the content of the document.

@sushma1509 Here is output produced on my side using the latest 24.1 version of Aspose.Words for Java:
converter.png (4.7 KB)

Thanks. May be I need to try with temporary license.
Also, is the ViewOptions available only if doc is saved as DOC or is it possible even if saved as PDF ?
if not, any possibility of adding it in future?

@sushma1509 For PDF format you should use PdfSaveOptions.ZoomFactor and PdfSaveOptions.ZoomBehavior properties.

@alexey.noskov

Can I use ImageSaveOptions?

ImageSaveOptions options= new ImageSaveOptions(SaveFormat. PNG)
options.setScale()

Will this work as zoom factor?

https://reference.aspose.com/words/net/aspose.words.saving/imagesaveoptions/

Hi @alexey.noskov
Just to clarify:
My input type will be DOC/DOCX. Here are the questions:
If I convert to PDF, below are the two ways to set zoom factor and rotate factor:

//Option 1:
//Use pdf to set rotate and zoom -resultant type will be PDF
com.aspose.words.Document document = wordDocument.getDocument();
com.aspose.words.Document doc1 = document.extractPages(pageNum - 1, 1);
doc1.save("C:/temp/save.pdf", SaveFormat.PDF);
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("C:/temp/save.pdf");
GoToAction action = new GoToAction(new XYZExplicitDestination(1, 0, 0, .5));
pdfDocument.setOpenAction(action);
pdfDocument.save("C:/temp/zoomed.pdf", com.aspose.pdf.SaveFormat.Pdf);
//Option 2:
//Use ImageSaveOptions to set zoom , rotate yet to figure out : resultant mime type will be pdf
com.aspose.words.Document documentImage = wordDocument.getDocument();
com.aspose.words.Document documentImage1 = documentImage.extractPages(pageNum - 1, 1);
com.aspose.words.PdfSaveOptions pdfSaveOptions = new com.aspose.words.PdfSaveOptions();
pdfSaveOptions.setZoomBehavior(PdfZoomBehavior.ZOOM_FACTOR);
pdfSaveOptions.setZoomFactor(25);
documentImage1.save("C:/temp/zoomword.pdf", pdfSaveOptions);
  1. If I want the type of the output to be PNG , the only way to set zoom and rotate is as below:
com.aspose.words.Document pngDocument = wordDocument.getDocument();
com.aspose.words.Document pngDocument1 = pngDocument.extractPages(pageNum - 1, 1);


// Calculate the number of rows and columns that we will fill with thumbnails.
final int thumbColumns = 2;
int thumbRows = pngDocument.getPageCount() / thumbColumns;

int remainder = pngDocument.getPageCount() % thumbColumns;
if (remainder > 0) thumbRows++;

// Scale the thumbnails relative to the size of the first page.
float scale = 1.0f;
Dimension thumbSize = pngDocument.getPageInfo(0).getSizeInPixels(scale, 96);

// Calculate the size of the image that will contain all the thumbnails.
int imgWidth = (int)(thumbSize.getWidth() * thumbColumns);
int imgHeight = (int)(thumbSize.getHeight() * thumbRows);

BufferedImage img = new BufferedImage(700, 700, BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = img.createGraphics();

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

gr.setColor(Color.white);
// Fill the background, which is transparent by default, in white.
//   gr.fillRect(0, 0, imgWidth, imgHeight);

// for (int pageIndex = 0; pageIndex < document.getPageCount(); pageIndex++) {
int rowIdx = pageNum / thumbColumns;
int columnIdx = pageNum % thumbColumns;

// Specify where we want the thumbnail to appear.
float thumbLeft = (float)(columnIdx * thumbSize.getWidth());
float thumbTop = (float)(rowIdx * thumbSize.getHeight());

Point.Float size = pngDocument.renderToScale(pageNum - 1, gr, (float)ConvertUtil.inchToPoint(3), (float)ConvertUtil.inchToPoint(3), scale);

gr.setColor(Color.black);

// Render a page as a thumbnail, and then frame it in a rectangle of the same size.
gr.drawRect(0, 0, (int)ConvertUtil.inchToPoint(3), (int)ConvertUtil.inchToPoint(3));
//   }

//  ImageIO.write(img, "PNG", new File("C:/temp/Rendering.Thumbnails.png"));
ImageIO.write(img, "PNG", os);

If I want DOC to PNG conversion and then set zoom , I need to use renderToScale or renderToSize API of Aspose.Words. Can you please confirm if my understanding is correct ?

Also, what is the difference between using GoToAction(from aspose.pdf) to set zoom and PdfSaveOptions(aspose.words) to set zoom ?

@sushma1509

  1. In Option 1 Aspose.PDF is used to set zoom factor of PDF document, in the Option 2 Aspose.Words is used for the same. I would recommend to use Aspose.Words to avoid document postprocessing. This will reduce the document processing time. So the following code can be used while saving to PDF:
Document doc = new Document("C:\\Temp\\in.docx");
PdfSaveOptions opt = new PdfSaveOptions();
opt.setZoomBehavior(PdfZoomBehavior.ZOOM_FACTOR);
opt.setZoomFactor(25);
doc.save("C:\\Temp\\out.pdf", opt);
  1. If the document pages should be saved as images and scaled, you can use renderToSize and renderToSacale methods. For example see the following code:
Document doc = new Document("C:\\Temp\\in.doc");

// Define scale factor to scale the pages.
float scaleFactor = 0.25f; // 25%
for (int i = 0; i < doc.getPageCount(); i++)
{
    // Get the current page size.
    PageInfo pi = doc.getPageInfo(i);
    // get scaled page size in pixels. Use default 96dpi resolution.
    Dimension scaledPageSize = pi.getSizeInPixels(scaleFactor, 96);
    // Create an image of scaled page size.
    BufferedImage pageImg = new BufferedImage(scaledPageSize.width, scaledPageSize.height, BufferedImage.TYPE_INT_ARGB);
    // Render the page to the created image
    Graphics2D gr = pageImg.createGraphics();
    doc.renderToSize(i, gr, 0f, 0f, scaledPageSize.width, scaledPageSize.height);
    // Save the page image
    ImageIO.write(pageImg, "PNG", new File("C:\\Temp\\page_" + i + ".png"));
}