Viewer preferences

This is a code example of iText PDF, discover more.

31st May 2016
iText PDF

Switch code for this example

ChangeViewerPreference.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. package com.itextpdf.samples.sandbox.stamper;
  9.  
  10. import com.itextpdf.kernel.pdf.PdfDocument;
  11. import com.itextpdf.kernel.pdf.PdfReader;
  12. import com.itextpdf.kernel.pdf.PdfViewerPreferences;
  13. import com.itextpdf.kernel.pdf.PdfWriter;
  14. import com.itextpdf.samples.GenericTest;
  15. import com.itextpdf.test.annotations.type.SampleTest;
  16.  
  17. import org.junit.experimental.categories.Category;
  18.  
  19. import java.io.File;
  20.  
  21. @Category(SampleTest.class)
  22. public class ChangeViewerPreference extends GenericTest {
  23.     public static final String DEST = "./target/test/resources/sandbox/stamper/change_viewer_preference.pdf";
  24.     public static final String SRC = "./src/test/resources/pdfs/OCR.pdf";
  25.  
  26.     public static void main(String[] args) throws Exception {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new ChangeViewerPreference().manipulatePdf(DEST);
  30.     }
  31.  
  32.     @Override
  33.     protected void manipulatePdf(String dest) throws Exception {
  34.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  35.         PdfViewerPreferences viewerPreferences = pdfDoc.getCatalog().getViewerPreferences();
  36.         if (viewerPreferences == null) {
  37.             viewerPreferences = new PdfViewerPreferences();
  38.             pdfDoc.getCatalog().setViewerPreferences(viewerPreferences);
  39.         }
  40.         viewerPreferences.setDuplex(PdfViewerPreferences.PdfViewerPreferencesConstants.DUPLEX_FLIP_LONG_EDGE);
  41.         pdfDoc.close();
  42.     }
  43. }
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