0

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

PdfReader reader = new PdfReader("input.pdf".ToString());
iTextSharp.text.Document doc = new iTextSharp.text.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();

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
mail2vguna
  • 25
  • 2
  • 9
  • 1
    Line 1 is reading from an existing PDF. Line 2 is creating a **brand new empty** PDF with the same size as the first page of the existing PDF but with no other relation to the existing PDF. I'm surprised line 3 actually works. Line 4 is binding the **brand new empty** PDF to the **brand new empty** physical file "zoom.pdf". Line 5 is creating a reference to the first page of the **brand new empty** PDF however there are no pages so this would be invalid. – Chris Haas Jun 06 '14 at 18:43
  • @mail2vguna in other words, use a `PdfStamper` instead. – mkl Jun 07 '14 at 05:15
  • Hi mkl, instead of what?I din't get you.can you provide sample code?Thanks. – mail2vguna Jun 07 '14 at 06:58
  • possible duplicate of [Set inherit Zoom(action property) to bookmark in the pdf file](http://stackoverflow.com/questions/24217657/set-inherit-zoomaction-property-to-bookmark-in-the-pdf-file) – Jongware Nov 17 '14 at 13:54

1 Answers1

1

You need to use PdfStamper (as indicated by mkl) instead of PdfWriter (as made clear by Chris Haas). Please take a look at the AddOpenAction example:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, reader.getPageSize(1).getHeight(), 0.75f);
    PdfAction action = PdfAction.gotoLocalPage(1, pdfDest, stamper.getWriter());
    stamper.getWriter().setOpenAction(action);
    stamper.close();
    reader.close();
}

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

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165