Commits

Andrea Vacondio committed 6e9a2fc

Close #72: upgraded PDFBox to 1.7.0. Added print scaling and duplex to the PDFbox task impl now that they are supported.

  • Participants
  • Parent commits 897bd53

Comments (0)

Files changed (5)

File sejda-model/src/main/java/org/sejda/model/input/PdfSource.java

      * @throws TaskIOException
      *             in case of error opening the source.
      */
-    public abstract <R> R open(PdfSourceOpener<R> opener) throws TaskIOException;
+    <R> R open(PdfSourceOpener<R> opener) throws TaskIOException;
 }

File sejda-pdfbox/pom.xml

 		<dependency>
 			<groupId>org.apache.pdfbox</groupId>
 			<artifactId>pdfbox</artifactId>
-			<version>1.6.0</version>
+			<version>1.7.0</version>
 		</dependency>
 		<dependency>
 			<groupId>org.bouncycastle</groupId>

File sejda-pdfbox/src/main/java/org/sejda/impl/pdfbox/ViewerPreferencesTask.java

 import static org.sejda.core.support.prefix.NameGenerator.nameGenerator;
 import static org.sejda.core.support.prefix.model.NameGenerationRequest.nameRequest;
 import static org.sejda.impl.pdfbox.util.ViewerPreferencesUtils.getDirection;
+import static org.sejda.impl.pdfbox.util.ViewerPreferencesUtils.getDuplex;
 import static org.sejda.impl.pdfbox.util.ViewerPreferencesUtils.getNFSMode;
+import static org.sejda.impl.pdfbox.util.ViewerPreferencesUtils.getPrintScaling;
 import static org.sejda.impl.pdfbox.util.ViewerPreferencesUtils.setBooleanPreferences;
 
 import java.io.File;
 
 import org.apache.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences;
+import org.apache.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences.DUPLEX;
+import org.apache.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences.NON_FULL_SCREEN_PAGE_MODE;
+import org.apache.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences.PRINT_SCALING;
+import org.apache.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences.READING_DIRECTION;
 import org.sejda.core.support.io.MultipleOutputWriter;
 import org.sejda.core.support.io.OutputWriters;
 import org.sejda.impl.pdfbox.component.DefaultPdfSourceOpener;
         PDViewerPreferences preferences = documentHandler.getViewerPreferences();
         setBooleanPreferences(preferences, parameters.getEnabledPreferences());
         if (parameters.getDirection() != null) {
-            String direction = getDirection(parameters.getDirection());
+            READING_DIRECTION direction = getDirection(parameters.getDirection());
             preferences.setReadingDirection(direction);
             LOG.trace("Direction set to '{}'.", direction);
         }
-        String nfsMode = getNFSMode(parameters.getNfsMode());
+        if (parameters.getDuplex() != null) {
+            DUPLEX duplex = getDuplex(parameters.getDuplex());
+            preferences.setDuplex(duplex);
+            LOG.trace("Duplex set to '{}'.", duplex);
+        }
+        if (parameters.getPrintScaling() != null) {
+            PRINT_SCALING printScaling = getPrintScaling(parameters.getPrintScaling());
+            preferences.setPrintScaling(printScaling);
+            LOG.trace("PrintScaling set to '{}'.", printScaling);
+        }
+        NON_FULL_SCREEN_PAGE_MODE nfsMode = getNFSMode(parameters.getNfsMode());
         preferences.setNonFullScreenPageMode(nfsMode);
         LOG.trace("Non full screen mode set to '{}'.", nfsMode);
         documentHandler.setViewerPreferences(preferences);

File sejda-pdfbox/src/main/java/org/sejda/impl/pdfbox/util/ViewerPreferencesUtils.java

 
 import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
 import org.apache.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences;
+import org.apache.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences.DUPLEX;
+import org.apache.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences.NON_FULL_SCREEN_PAGE_MODE;
+import org.apache.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences.PRINT_SCALING;
+import org.apache.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences.READING_DIRECTION;
 import org.sejda.model.exception.TaskException;
 import org.sejda.model.pdf.viewerpreference.PdfBooleanPreference;
 import org.sejda.model.pdf.viewerpreference.PdfDirection;
+import org.sejda.model.pdf.viewerpreference.PdfDuplex;
 import org.sejda.model.pdf.viewerpreference.PdfNonFullScreenPageMode;
 import org.sejda.model.pdf.viewerpreference.PdfPageLayout;
 import org.sejda.model.pdf.viewerpreference.PdfPageMode;
+import org.sejda.model.pdf.viewerpreference.PdfPrintScaling;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
         // hide utility constructor
     }
 
-    private static final Map<PdfNonFullScreenPageMode, String> NFS_MODE_CACHE;
+    private static final Map<PdfNonFullScreenPageMode, NON_FULL_SCREEN_PAGE_MODE> NFS_MODE_CACHE;
     static {
-        Map<PdfNonFullScreenPageMode, String> nfsModeCache = new HashMap<PdfNonFullScreenPageMode, String>();
-        nfsModeCache.put(PdfNonFullScreenPageMode.USE_NONE, PDViewerPreferences.NON_FULL_SCREEN_PAGE_MODE_USE_NONE);
-        nfsModeCache.put(PdfNonFullScreenPageMode.USE_OC,
-                PDViewerPreferences.NON_FULL_SCREEN_PAGE_MODE_USE_OPTIONAL_CONTENT);
+        Map<PdfNonFullScreenPageMode, NON_FULL_SCREEN_PAGE_MODE> nfsModeCache = new HashMap<PdfNonFullScreenPageMode, NON_FULL_SCREEN_PAGE_MODE>();
+        nfsModeCache.put(PdfNonFullScreenPageMode.USE_NONE, PDViewerPreferences.NON_FULL_SCREEN_PAGE_MODE.UseNone);
+        nfsModeCache.put(PdfNonFullScreenPageMode.USE_OC, PDViewerPreferences.NON_FULL_SCREEN_PAGE_MODE.UseOC);
         nfsModeCache.put(PdfNonFullScreenPageMode.USE_OUTLINES,
-                PDViewerPreferences.NON_FULL_SCREEN_PAGE_MODE_USE_OUTLINES);
-        nfsModeCache.put(PdfNonFullScreenPageMode.USE_THUMNS, PDViewerPreferences.NON_FULL_SCREEN_PAGE_MODE_USE_THUMBS);
+                PDViewerPreferences.NON_FULL_SCREEN_PAGE_MODE.UseOutlines);
+        nfsModeCache.put(PdfNonFullScreenPageMode.USE_THUMNS, PDViewerPreferences.NON_FULL_SCREEN_PAGE_MODE.UseThumbs);
         NFS_MODE_CACHE = Collections.unmodifiableMap(nfsModeCache);
     }
 
         PAGE_MODE_CACHE = Collections.unmodifiableMap(pageModeCache);
     }
 
+    private static final Map<PdfDuplex, DUPLEX> DUPLEX_CACHE;
+    static {
+        Map<PdfDuplex, DUPLEX> duplexCache = new HashMap<PdfDuplex, DUPLEX>();
+        duplexCache.put(PdfDuplex.SIMPLEX, PDViewerPreferences.DUPLEX.Simplex);
+        duplexCache.put(PdfDuplex.DUPLEX_FLIP_LONG_EDGE, PDViewerPreferences.DUPLEX.DuplexFlipLongEdge);
+        duplexCache.put(PdfDuplex.DUPLEX_FLIP_SHORT_EDGE, PDViewerPreferences.DUPLEX.DuplexFlipShortEdge);
+        DUPLEX_CACHE = Collections.unmodifiableMap(duplexCache);
+    }
+
     /**
      * Mapping between Sejda and PDFBox non full screen mode constants
      * 
      * @param nfsMode
      * @return the PDFBox non full screen mode constant.
      */
-    public static String getNFSMode(PdfNonFullScreenPageMode nfsMode) {
+    public static NON_FULL_SCREEN_PAGE_MODE getNFSMode(PdfNonFullScreenPageMode nfsMode) {
         return NFS_MODE_CACHE.get(nfsMode);
     }
 
      * @param direction
      * @return the PDFBox direction constant
      */
-    public static String getDirection(PdfDirection direction) {
+    public static READING_DIRECTION getDirection(PdfDirection direction) {
         if (PdfDirection.RIGHT_TO_LEFT.equals(direction)) {
-            return PDViewerPreferences.READING_DIRECTION_R2L;
+            return PDViewerPreferences.READING_DIRECTION.R2L;
         }
-        return PDViewerPreferences.READING_DIRECTION_L2R;
+        return PDViewerPreferences.READING_DIRECTION.L2R;
+    }
+
+    /**
+     * Mapping between Sejda and PDFBox duplex constants
+     * 
+     * @param duplex
+     * @return the PDFBox duplex constant
+     */
+    public static DUPLEX getDuplex(PdfDuplex duplex) {
+        return DUPLEX_CACHE.get(duplex);
+    }
+
+    /**
+     * Mapping between Sejda and PDFBox print scaling constants
+     * 
+     * @param scaling
+     * @return the PDFBox print scaling constant
+     */
+    public static PRINT_SCALING getPrintScaling(PdfPrintScaling scaling) {
+        if (PdfPrintScaling.NONE.equals(scaling)) {
+            return PDViewerPreferences.PRINT_SCALING.None;
+        }
+        return PDViewerPreferences.PRINT_SCALING.AppDefault;
     }
 
     /**

File sejda-pdfbox/src/test/java/org/sejda/impl/pdfbox/util/ViewerPreferencesUtilsTestTest.java

 public class ViewerPreferencesUtilsTestTest {
     @Test
     public void testGetDirection() {
-        assertEquals(PDViewerPreferences.READING_DIRECTION_L2R,
+        assertEquals(PDViewerPreferences.READING_DIRECTION.L2R,
                 ViewerPreferencesUtils.getDirection(PdfDirection.LEFT_TO_RIGHT));
-        assertEquals(PDViewerPreferences.READING_DIRECTION_R2L,
+        assertEquals(PDViewerPreferences.READING_DIRECTION.R2L,
                 ViewerPreferencesUtils.getDirection(PdfDirection.RIGHT_TO_LEFT));
     }
 
 
     @Test
     public void testGetNFSMode() {
-        assertEquals(PDViewerPreferences.NON_FULL_SCREEN_PAGE_MODE_USE_NONE,
+        assertEquals(PDViewerPreferences.NON_FULL_SCREEN_PAGE_MODE.UseNone,
                 ViewerPreferencesUtils.getNFSMode(PdfNonFullScreenPageMode.USE_NONE));
-        assertEquals(PDViewerPreferences.NON_FULL_SCREEN_PAGE_MODE_USE_OPTIONAL_CONTENT,
+        assertEquals(PDViewerPreferences.NON_FULL_SCREEN_PAGE_MODE.UseOC,
                 ViewerPreferencesUtils.getNFSMode(PdfNonFullScreenPageMode.USE_OC));
-        assertEquals(PDViewerPreferences.NON_FULL_SCREEN_PAGE_MODE_USE_OUTLINES,
+        assertEquals(PDViewerPreferences.NON_FULL_SCREEN_PAGE_MODE.UseOutlines,
                 ViewerPreferencesUtils.getNFSMode(PdfNonFullScreenPageMode.USE_OUTLINES));
-        assertEquals(PDViewerPreferences.NON_FULL_SCREEN_PAGE_MODE_USE_THUMBS,
+        assertEquals(PDViewerPreferences.NON_FULL_SCREEN_PAGE_MODE.UseThumbs,
                 ViewerPreferencesUtils.getNFSMode(PdfNonFullScreenPageMode.USE_THUMNS));
     }