View RSS Feed

richyrich

Set Initial PDF Document Zoom using iTextSharp

Rate this Entry
by on June 28th, 2010 at 10:15 AM (2272 Views)
Another iTextSharp blog post, this time on setting the initial zoom for a document.

Couldn't find an exact example of how to do this, but managed to cobble this together from a variety of code snippets I found.

You can set the initial zoom of a document using iTextSharp by adding a SetOpenAction method to the PdfWriter object. I found I could only add this once the document had been opened using doc.Open(), otherwise I got an "Object reference" error. This I presumed was referring to the writer object.

Code:
        PdfWriter writer = PdfWriter.GetInstance(doc, HttpContext.Current.Response.OutputStream);

        //this creates a new destination to send the action to when the document is opened. The zoom in this instance is set to 0.75f (75%). Again I use doc.PageSize.Height to set the y coordinate to the top of the page. PdfDestination.XYZ is a specific PdfDestination Type that allows us to set the location and zoom.
        PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 0.75f);

         //open our document
         doc.Open();

         //here you put all the information you want to write to your PDF

        //create a new action to send the document to our new destination.
        PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);

        //set the open action for our writer object
        writer.SetOpenAction(action);

        //finally, close our document
        doc.Close();
This should set the initial destination of our iTextSharp PDF document to page 1, location 0, page height, with a zoom of 0.75.

Hope that helps someone.

Submit "Set Initial PDF Document Zoom using iTextSharp" to Digg Submit "Set Initial PDF Document Zoom using iTextSharp" to del.icio.us Submit "Set Initial PDF Document Zoom using iTextSharp" to StumbleUpon Submit "Set Initial PDF Document Zoom using iTextSharp" to Google

Comments

  1. mikev2 -
    mikev2's Avatar
    This is just what I was looking for. Thanks for posting.
    • |
    • permalink

SEO by vBSEO