How to set the zoom level of a PDF using iTextSharp?

I need to set the zoom level to 75% in a PDF file using iTextSharp. But I am getting the error "the page 1 was request but the document has only 0 pages"

31st May 2016
admin-marketing

I need to set the zoom level to 75% in a PDF file using iTextSharp. I am using following code to set the zoom level.

 

PdfReader reader = new PdfReader("input.pdf".ToString());
Document doc = Document(reader.GetPageSize(1));
doc.OpenDocument();
PdfWriter writer = PdfWriter.GetInstance(doc,
    new FileStream("Zoom.pdf", FileMode.Create));
PdfDestination pdfDest = new PdfDestination(
    PdfDestination.XYZ, 0, doc.PageSize.Height, 0.75f);
doc.Open();
PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);
writer.SetOpenAction(action);
doc.Close();
But I am getting the error "the page 1 was request but the document has only 0 pages" in the doc.Close();

 

Posted on StackOverflow on Jun 6, 2014 by mail2vguna

Please take a look at the AddOpenAction example. This is how it’s done in iText 7:

PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
pdfDoc.getCatalog()
        .setOpenAction(PdfExplicitDestination.createXYZ(1, 0, pdfDoc.getPage(1).getPageSize().getHeight(), 0.75f));
pdfDoc.close();

The result is a PDF that opens with a zoom factor of 75%.

Click this link if you want to see how to answer this question in iText 5.


Share this article

Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now