You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ja...@apache.org on 2014/10/22 02:48:01 UTC

svn commit: r1633501 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: pdmodel/PDDocumentCatalog.java pdmodel/PageLayout.java pdmodel/PageMode.java util/PDFMergerUtility.java

Author: jahewson
Date: Wed Oct 22 00:48:01 2014
New Revision: 1633501

URL: http://svn.apache.org/r1633501
Log:
PDFBOX-2423: Made page mode and layout constants in PDDocumentCatalog into enums

Added:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageLayout.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageMode.java
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocumentCatalog.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocumentCatalog.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocumentCatalog.java?rev=1633501&r1=1633500&r2=1633501&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocumentCatalog.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocumentCatalog.java Wed Oct 22 00:48:01 2014
@@ -50,56 +50,6 @@ import org.apache.pdfbox.pdmodel.interac
  */
 public class PDDocumentCatalog implements COSObjectable
 {
-    /**
-     * Page mode where neither the outline nor the thumbnails are displayed.
-     */
-    public static final String PAGE_MODE_USE_NONE = "UseNone";
-    /**
-     * Show bookmarks when pdf is opened.
-     */
-    public static final String PAGE_MODE_USE_OUTLINES = "UseOutlines";
-    /**
-     * Show thumbnails when pdf is opened.
-     */
-    public static final String PAGE_MODE_USE_THUMBS = "UseThumbs";
-    /**
-     * Full screen mode with no menu bar, window controls.
-     */
-    public static final String PAGE_MODE_FULL_SCREEN = "FullScreen";
-    /**
-     * Optional content group panel is visible when opened.
-     */
-    public static final String PAGE_MODE_USE_OPTIONAL_CONTENT = "UseOC";
-    /**
-     * Attachments panel is visible.
-     */
-    public static final String PAGE_MODE_USE_ATTACHMENTS = "UseAttachments";
-
-    /**
-     * Display one page at a time.
-     */
-    public static final String PAGE_LAYOUT_SINGLE_PAGE = "SinglePage";
-    /**
-     * Display the pages in one column.
-     */
-    public static final String PAGE_LAYOUT_ONE_COLUMN = "OneColumn";
-    /**
-     * Display the pages in two columns, with odd numbered pagse on the left.
-     */
-    public static final String PAGE_LAYOUT_TWO_COLUMN_LEFT = "TwoColumnLeft";
-    /**
-     * Display the pages in two columns, with odd numbered pagse on the right.
-     */
-    public static final String PAGE_LAYOUT_TWO_COLUMN_RIGHT ="TwoColumnRight";
-    /**
-     * Display the pages two at a time, with odd-numbered pages on the left.
-     */
-    public static final String PAGE_LAYOUT_TWO_PAGE_LEFT = "TwoPageLeft";
-    /**
-     * Display the pages two at a time, with odd-numbered pages on the right.
-     */
-    public static final String PAGE_LAYOUT_TWO_PAGE_RIGHT = "TwoPageRight";
-
     private final COSDictionary root;
     private final PDDocument document;
     private PDAcroForm cachedAcroForm;
@@ -483,43 +433,59 @@ public class PDDocumentCatalog implement
     }
 
     /**
-     * Sets the page display mode, see the PAGE_MODE_XXX constants.
+     * Returns the page display mode.
      *
-     * @return A string representing the page mode.
+     * @return the new page mode.
      */
-    public String getPageMode()
+    public PageMode getPageMode()
     {
-        return root.getNameAsString(COSName.PAGE_MODE, PAGE_MODE_USE_NONE);
+        String mode = root.getNameAsString(COSName.PAGE_MODE);
+        if (mode != null)
+        {
+            return PageMode.fromString(mode);
+        }
+        else
+        {
+            return PageMode.USE_NONE;
+        }
     }
 
     /**
-     * Sets the page mode. See the PAGE_MODE_XXX constants for valid values.
+     * Sets the page mode.
      *
      * @param mode The new page mode.
      */
-    public void setPageMode(String mode)
+    public void setPageMode(PageMode mode)
     {
-        root.setName(COSName.PAGE_MODE, mode);
+        root.setName(COSName.PAGE_MODE, mode.stringValue());
     }
 
     /**
-     * Sets the page layout, see the PAGE_LAYOUT_XXX constants.
+     * Gets the page layout.
      *
-     * @return A string representing the page layout.
+     * @return the page layout.
      */
-    public String getPageLayout()
+    public PageLayout getPageLayout()
     {
-        return root.getNameAsString(COSName.PAGE_LAYOUT, PAGE_LAYOUT_SINGLE_PAGE);
+        String mode = root.getNameAsString(COSName.PAGE_LAYOUT);
+        if (mode != null)
+        {
+            return PageLayout.fromString(mode);
+        }
+        else
+        {
+            return PageLayout.SINGLE_PAGE;
+        }
     }
 
     /**
-     * Sets the page layout. See the PAGE_LAYOUT_XXX constants for valid values.
+     * Sets the page layout.
      *
      * @param layout The new page layout.
      */
-    public void setPageLayout(String layout)
+    public void setPageLayout(PageLayout layout)
     {
-        root.setName(COSName.PAGE_LAYOUT, layout);
+        root.setName(COSName.PAGE_LAYOUT, layout.stringValue());
     }
 
     /**

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageLayout.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageLayout.java?rev=1633501&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageLayout.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageLayout.java Wed Oct 22 00:48:01 2014
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License), Version 2.0
+ * (the "License")), you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing), software
+ * distributed under the License is distributed on an "AS IS" BASIS),
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND), either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.pdfbox.pdmodel;
+
+/**
+ * A name object specifying the page layout shall be used when the document is opened.
+ *
+ * @author John Hewson
+ */
+public enum PageLayout
+{
+    /** Display one page at a time. */
+    SINGLE_PAGE("SinglePage"),
+
+    /**  Display the pages in one column. */
+    ONE_COLUMN("OneColumn"),
+
+    /** Display the pages in two columns), with odd numbered pages on the left. */
+    TWO_COLUMN_LEFT("TwoColumnLeft"),
+
+    /** Display the pages in two columns), with odd numbered pages on the right.  */
+    TWO_COLUMN_RIGHT("TwoColumnRight"),
+
+    /** Display the pages two at a time), with odd-numbered pages on the left. */
+    TWO_PAGE_LEFT("TwoPageLeft"),
+
+    /** Display the pages two at a time), with odd-numbered pages on the right. */
+    TWO_PAGE_RIGHT("TwoPageRight");
+
+    public static PageLayout fromString(String value)
+    {
+        if (value.equals("SinglePage"))
+        {
+            return SINGLE_PAGE;
+        }
+        else if (value.equals("OneColumn"))
+        {
+            return ONE_COLUMN;
+        }
+        else if (value.equals("TwoColumnLeft"))
+        {
+            return TWO_COLUMN_LEFT;
+        }
+        else if (value.equals("TwoPageLeft"))
+        {
+            return TWO_PAGE_LEFT;
+        }
+        else if (value.equals("TwoPageRight"))
+        {
+            return TWO_PAGE_RIGHT;
+        }
+        throw new IllegalArgumentException(value);
+    }
+
+    private final String value;
+
+    PageLayout(String value)
+    {
+        this.value = value;
+    }
+
+    /**
+     * Returns the string value, as used in a PDF file.
+     */
+    public String stringValue()
+    {
+        return value;
+    }
+}

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageMode.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageMode.java?rev=1633501&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageMode.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageMode.java Wed Oct 22 00:48:01 2014
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.pdfbox.pdmodel;
+
+/**
+ * A name object specifying how the document shall be displayed when opened.
+ *
+ * @author John Hewson
+ */
+public enum PageMode
+{
+    /** Neither the outline nor the thumbnails are displayed. */
+    USE_NONE("UseNone"),
+
+    /** Show bookmarks when pdf is opened. */
+    USE_OUTLINES("UseOutlines"),
+
+    /** Show thumbnails when pdf is opened. */
+    USE_THUMBS("UseThumbs"),
+
+    /** Full screen mode with no menu bar, window controls. */
+    FULL_SCREEN("FullScreen"),
+
+    /** Optional content group panel is visible when opened. */
+    USE_OPTIONAL_CONTENT("UseOC"),
+
+    /** Attachments panel is visible. */
+    USE_ATTACHMENTS("UseAttachments");
+
+    public static PageMode fromString(String value)
+    {
+        if (value.equals("UseNone"))
+        {
+            return USE_NONE;
+        }
+        else if (value.equals("UseOutlines"))
+        {
+            return USE_OUTLINES;
+        }
+        else if (value.equals("UseThumbs"))
+        {
+            return USE_THUMBS;
+        }
+        else if (value.equals("FullScreen"))
+        {
+            return FULL_SCREEN;
+        }
+        else if (value.equals("UseOC"))
+        {
+            return USE_OPTIONAL_CONTENT;
+        }
+        else if (value.equals("UseAttachments"))
+        {
+            return USE_ATTACHMENTS;
+        }
+        throw new IllegalArgumentException(value);
+    }
+
+    private final String value;
+
+    PageMode(String value)
+    {
+        this.value = value;
+    }
+
+    /**
+     * Returns the string value, as used in a PDF file.
+     */
+    public String stringValue()
+    {
+        return value;
+    }
+}

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java?rev=1633501&r1=1633500&r2=1633501&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java Wed Oct 22 00:48:01 2014
@@ -42,6 +42,7 @@ import org.apache.pdfbox.pdmodel.PDDocum
 import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary;
 import org.apache.pdfbox.pdmodel.PDPage;
 import org.apache.pdfbox.pdmodel.PDResources;
+import org.apache.pdfbox.pdmodel.PageMode;
 import org.apache.pdfbox.pdmodel.common.COSArrayList;
 import org.apache.pdfbox.pdmodel.common.PDNumberTreeNode;
 import org.apache.pdfbox.pdmodel.common.PDStream;
@@ -352,8 +353,8 @@ public class PDFMergerUtility
             }
         }
 
-        String destPageMode = destCatalog.getPageMode();
-        String srcPageMode = srcCatalog.getPageMode();
+        PageMode destPageMode = destCatalog.getPageMode();
+        PageMode srcPageMode = srcCatalog.getPageMode();
         if (destPageMode == null)
         {
             destCatalog.setPageMode(srcPageMode);