0

I am creating a pdf file using iText. But its opening in different zoom percentage in each time. How to set the default zoom to 100% using java ? I am creating the PDF document using the below code :

        String fileName = "Dashboard.pdf";
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=\""
                + fileName + "\"");
        Document my_pdf_report = new Document();

        PdfWriter.getInstance(my_pdf_report, response.getOutputStream());
        my_pdf_report.open();

Because I need to generate a Save as Popup also. So is it possible to use

        PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ,
                0, reader.getPageSize(1).getHeight(), 1f);

in my code . If it is possible how it can be done ?

Mahesh Narayanan
  • 123
  • 5
  • 21
  • 1
    possible duplicate of [How to set zoom level to pdf using iTextSharp?](http://stackoverflow.com/questions/24087786/how-to-set-zoom-level-to-pdf-using-itextsharp) – Bruno Lowagie Jul 09 '15 at 11:03
  • Actually i am creating the file like this: `String fileName = "Dashboard.pdf"; response.setContentType("application/pdf"); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); Document my_pdf_report = new Document(); PdfWriter.getInstance(my_pdf_report, response.getOutputStream());` Because i need to generate Save as popup also. So is it possible to use `PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, reader.getPageSize(1).getHeight(), 1f);' in the above code sample ? – Mahesh Narayanan Jul 09 '15 at 11:55

1 Answers1

1

Please take a look at my answer to this question: How to set zoom level to pdf using iTextSharp?

In answer to this question, I have written the AddOpenAction example (which is using Java). This example opens a PDF with a zoom factor of 75%. You need a PDF that opens with a zoom factor of 100%. This means that you need to adapt the following line:

PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ,
    0, reader.getPageSize(1).getHeight(), 0.75f);

Like this:

PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ,
    0, reader.getPageSize(1).getHeight(), 1f);

With the 0.75 replaced by 1, the zoom factor is no longer 75% but 100%.

Another difference with your situation, is that you are creating a PDF from scratch (as you explain in a comment). You have:

PdfWriter.getInstance(my_pdf_report, response.getOutputStream());

You should replace this with:

PdfWriter writer = PdfWriter.getInstance(my_pdf_report, response.getOutputStream());

Now you can do:

writer.setOpenAction(PdfAction.gotoLocalPage(1, pdfDest, writer));

From another comment, we learn that you aren't aware of the page size of the PDFs you are creating. Please take a look at my answer to this question: How to set the page size to Envelope size with Landscape orientation?

You are creating a Document like this:

Document my_pdf_report = new Document();

This means that you are creating pages with the default size: A4.

In this case, the height of a page is 842, so you can use:

PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, 842, 1f);

Please read the documentation when in doubt. For a full, working example, see OpenAt100pct. The resulting PDF, open100pct.pdf opens at 100% in viewers that respect the /OpenAction functionality (not all of them do, but that's a viewer problem, not an iText or a PDF problem).

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Actually i am creating the file like this: `String fileName = "Dashboard.pdf"; response.setContentType("application/pdf"); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); Document my_pdf_report = new Document(); PdfWriter.getInstance(my_pdf_report, response.getOutputStream());` Because i need to generate Save as popup also. So is it possible to use ` PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, reader.getPageSize(1).getHeight(), 1f);` in the above code sample ? – Mahesh Narayanan Jul 09 '15 at 11:54
  • Yes, but you have to make a small change. I'll update my answer. – Bruno Lowagie Jul 09 '15 at 11:57
  • So I have to do it like this PdfReader reader = new PdfReader(fileName); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(fileName)); PdfWriter writer = PdfWriter.getInstance(my_pdf_report, response.getOutputStream()); PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, reader.getPageSize(1).getHeight(), 1f);PdfAction action = PdfAction.gotoLocalPage(1, pdfDest, stamper.getWriter()); writer.setOpenAction(PdfAction.gotoLocalPage(1, pdfDest, writer)); my_pdf_report.open(); Is it possible to provide a sample code ? Because for me its not working – Mahesh Narayanan Jul 09 '15 at 12:13
  • Remove the `PdfReader` and `PdfStamper`. You are creating a PDF from scratch. Why are you suddenly introducing `PdfReader` and `PdfStamper`? Please take a closer look at my update: `writer.setOpenAction(PdfAction.gotoLocalPage(1, pdfDest, writer));` There is no `stamper.getWriter()` there! You don't need `PdfStamper` in your case, you already have a `writer`. – Bruno Lowagie Jul 09 '15 at 12:19
  • But for this line 'PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, reader.getPageSize(1).getHeight(), 1f);' I need a reader instance right ? – Mahesh Narayanan Jul 09 '15 at 12:27
  • No you don't. I'll explain in another update to my question. – Bruno Lowagie Jul 09 '15 at 12:27
  • PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, 842, 1f); writer.setOpenAction(PdfAction.gotoLocalPage(1, pdfDest, writer)); So i have to put it before my_pdf_report.open(); Right ? – Mahesh Narayanan Jul 09 '15 at 12:43
  • Document my_pdf_report = new Document(); PdfWriter writer = PdfWriter.getInstance(my_pdf_report, response.getOutputStream()); PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, 842, 1f); writer.setOpenAction(PdfAction.gotoLocalPage(1, pdfDest, writer)); my_pdf_report.open(); This is my code . But iam getting an error – Mahesh Narayanan Jul 09 '15 at 12:44
  • @MaheshNarayanan I get the impression that you are intentionally making fun of me in an attempt to waste my time. I am going to pretend being naive and I am going to write you a full *working* example, but please be aware that patience is a scarce good. What does the error say? You should switch the line that opens the document with the open action declaration. – Bruno Lowagie Jul 09 '15 at 13:23
  • @ Bruno Lowagie I was not trying to make fun of you. java.lang.NullPointerException at com.itextpdf.text.pdf.PdfWriter.getPageReference(PdfWriter.java:1075) at com.itextpdf.text.pdf.PdfAction.gotoLocalPage(PdfAction.java:454) . I was getting this error. – Mahesh Narayanan Jul 10 '15 at 05:36
  • Anyway now its working, i need to open the file before PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, 842, 1f); I was opening the file after this line. So thanks for your help. – Mahesh Narayanan Jul 10 '15 at 05:38
  • @MaheshNarayanan OK, so if it works, why don't you accept the answer? – Bruno Lowagie Jul 10 '15 at 06:35