How can I set the zoom level of a PDF using iText 7?
A simple iText 7 example showing how to set a specific magnification level for a PDF.
You can use setOpenAction
to open a document with a default display magnification level. The following code uses PdfDocument()
, which has been initialized with a PdfWriter()
to set a default magnification level of 75% when opening the document:
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
pdfDoc.getCatalog().setOpenAction(PdfExplicitDestination.createXYZ(pdfDoc.getPage(1), 0, pdfDoc.getPage(1).getPageSize().getHeight(), 0.75f));
pdfDoc.close();
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
pdfDoc.GetCatalog().SetOpenAction(PdfExplicitDestination.CreateXYZ(pdfDoc.GetPage(1), 0, pdfDoc.GetPage(1).GetPageSize().GetHeight(), 0.75f));
pdfDoc.Close();
The result is a PDF that opens with a zoom factor of 75%.
Alternatively, in iText 7 a similar method could be used with PdfLinkAnnotation
, PdfScreenAnnotation
or PdfWidgetAnnotation
if you prefer. This uses annotations to trigger the display magnification action for the PDF.
Click this link if you want to see how to achieve the same thing in iText 5.