Mahesh Narayanan
Mahesh Narayanan

Reputation: 133

How to set a default zoom of 100% using itext java

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 ?

Upvotes: 0

Views: 3455

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

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).

Upvotes: 1

Related Questions