You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2018/02/11 09:18:15 UTC
svn commit: r1823824 - in /pdfbox/branches/2.0/pdfbox/src:
main/java/org/apache/pdfbox/pdmodel/graphics/optionalcontent/PDOptionalContentProperties.java
test/java/org/apache/pdfbox/pdmodel/graphics/optionalcontent/TestOptionalContentGroups.java
Author: tilman
Date: Sun Feb 11 09:18:15 2018
New Revision: 1823824
URL: http://svn.apache.org/viewvc?rev=1823824&view=rev
Log:
PDFBOX-4103: add ability to use PDOptionalContentGroup as key to check and set group visibility, by Ivan Khaldeev
Modified:
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/optionalcontent/PDOptionalContentProperties.java
pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/optionalcontent/TestOptionalContentGroups.java
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/optionalcontent/PDOptionalContentProperties.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/optionalcontent/PDOptionalContentProperties.java?rev=1823824&r1=1823823&r2=1823824&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/optionalcontent/PDOptionalContentProperties.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/optionalcontent/PDOptionalContentProperties.java Sun Feb 11 09:18:15 2018
@@ -16,6 +16,7 @@
*/
package org.apache.pdfbox.pdmodel.graphics.optionalcontent;
+import java.util.ArrayList;
import java.util.Collection;
import org.apache.pdfbox.cos.COSArray;
@@ -186,7 +187,7 @@ public class PDOptionalContentProperties
*/
public Collection<PDOptionalContentGroup> getOptionalContentGroups()
{
- Collection<PDOptionalContentGroup> coll = new java.util.ArrayList<PDOptionalContentGroup>();
+ Collection<PDOptionalContentGroup> coll = new ArrayList<PDOptionalContentGroup>();
COSArray ocgs = getOCGs();
for (COSBase base : ocgs)
{
@@ -259,18 +260,36 @@ public class PDOptionalContentProperties
*/
public boolean isGroupEnabled(String groupName)
{
+ return isGroupEnabled(getGroup(groupName));
+ }
+
+ /**
+ * Indicates whether an optional content group is enabled.
+ * @param group the group object
+ * @return true if the group is enabled
+ */
+ public boolean isGroupEnabled(PDOptionalContentGroup group)
+ {
//TODO handle Optional Content Configuration Dictionaries,
//i.e. OCProperties/Configs
+ PDOptionalContentProperties.BaseState baseState = getBaseState();
+ boolean enabled = !baseState.equals(BaseState.OFF);
+ //TODO What to do with BaseState.Unchanged?
+
+ if (group == null)
+ {
+ return enabled;
+ }
+
COSDictionary d = getD();
COSBase base = d.getDictionaryObject(COSName.ON);
if (base instanceof COSArray)
{
for (COSBase o : (COSArray) base)
{
- COSDictionary group = toDictionary(o);
- String name = group.getString(COSName.NAME);
- if (name.equals(groupName))
+ COSDictionary dictionary = toDictionary(o);
+ if (dictionary == group.getCOSObject())
{
return true;
}
@@ -282,18 +301,14 @@ public class PDOptionalContentProperties
{
for (COSBase o : (COSArray) base)
{
- COSDictionary group = toDictionary(o);
- String name = group.getString(COSName.NAME);
- if (name.equals(groupName))
+ COSDictionary dictionary = toDictionary(o);
+ if (dictionary == group.getCOSObject())
{
return false;
}
}
}
- BaseState baseState = getBaseState();
- boolean enabled = !baseState.equals(BaseState.OFF);
- //TODO What to do with BaseState.Unchanged?
return enabled;
}
@@ -317,6 +332,17 @@ public class PDOptionalContentProperties
*/
public boolean setGroupEnabled(String groupName, boolean enable)
{
+ return setGroupEnabled(getGroup(groupName), enable);
+ }
+
+ /**
+ * Enables or disables an optional content group.
+ * @param group the group object
+ * @param enable true to enable, false to disable
+ * @return true if the group already had an on or off setting, false otherwise
+ */
+ public boolean setGroupEnabled(PDOptionalContentGroup group, boolean enable)
+ {
COSArray on;
COSArray off;
@@ -347,9 +373,8 @@ public class PDOptionalContentProperties
{
for (COSBase o : off)
{
- COSDictionary group = toDictionary(o);
- String name = group.getString(COSName.NAME);
- if (name.equals(groupName))
+ COSDictionary groupDictionary = toDictionary(o);
+ if (groupDictionary == group.getCOSObject())
{
//enable group
off.remove(o);
@@ -363,9 +388,8 @@ public class PDOptionalContentProperties
{
for (COSBase o : on)
{
- COSDictionary group = toDictionary(o);
- String name = group.getString(COSName.NAME);
- if (name.equals(groupName))
+ COSDictionary groupDictionary = toDictionary(o);
+ if (groupDictionary == group.getCOSObject())
{
//disable group
on.remove(o);
@@ -377,14 +401,13 @@ public class PDOptionalContentProperties
}
if (!found)
{
- PDOptionalContentGroup ocg = getGroup(groupName);
if (enable)
{
- on.add(ocg.getCOSObject());
+ on.add(group.getCOSObject());
}
else
{
- off.add(ocg.getCOSObject());
+ off.add(group.getCOSObject());
}
}
return found;
Modified: pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/optionalcontent/TestOptionalContentGroups.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/optionalcontent/TestOptionalContentGroups.java?rev=1823824&r1=1823823&r2=1823824&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/optionalcontent/TestOptionalContentGroups.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/optionalcontent/TestOptionalContentGroups.java Sun Feb 11 09:18:15 2018
@@ -41,7 +41,7 @@ import org.apache.pdfbox.pdmodel.graphic
*/
public class TestOptionalContentGroups extends TestCase
{
- private File testResultsDir = new File("target/test-output");
+ private final File testResultsDir = new File("target/test-output");
@Override
protected void setUp() throws Exception
@@ -205,4 +205,76 @@ public class TestOptionalContentGroups e
}
}
+ public void testOCGsWithSameNameCanHaveDifferentVisibility() throws Exception
+ {
+ PDDocument doc = new PDDocument();
+ try
+ {
+ //Create new page
+ PDPage page = new PDPage();
+ doc.addPage(page);
+ PDResources resources = page.getResources();
+ if( resources == null )
+ {
+ resources = new PDResources();
+ page.setResources( resources );
+ }
+
+ //Prepare OCG functionality
+ PDOptionalContentProperties ocprops = new PDOptionalContentProperties();
+ doc.getDocumentCatalog().setOCProperties(ocprops);
+ //ocprops.setBaseState(BaseState.ON); //ON=default
+
+ //Create visible OCG
+ PDOptionalContentGroup visible = new PDOptionalContentGroup("layer");
+ ocprops.addGroup(visible);
+ assertTrue(ocprops.isGroupEnabled(visible));
+
+ //Create invisible OCG
+ PDOptionalContentGroup invisible = new PDOptionalContentGroup("layer");
+ ocprops.addGroup(invisible);
+ assertFalse(ocprops.setGroupEnabled(invisible, false));
+ assertFalse(ocprops.isGroupEnabled(invisible));
+
+ //Check that visible layer is still visible
+ assertTrue(ocprops.isGroupEnabled(visible));
+
+ //Setup page content stream and paint background/title
+ PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false);
+ PDFont font = PDType1Font.HELVETICA_BOLD;
+ contentStream.beginMarkedContent(COSName.OC, visible);
+ contentStream.beginText();
+ contentStream.setFont(font, 14);
+ contentStream.newLineAtOffset(80, 700);
+ contentStream.showText("PDF 1.5: Optional Content Groups");
+ contentStream.endText();
+ font = PDType1Font.HELVETICA;
+ contentStream.beginText();
+ contentStream.setFont(font, 12);
+ contentStream.newLineAtOffset(80, 680);
+ contentStream.showText("You should see this text, but no red text line.");
+ contentStream.endText();
+ contentStream.endMarkedContent();
+
+ //Paint disabled layer
+ contentStream.beginMarkedContent(COSName.OC, invisible);
+ contentStream.setNonStrokingColor(Color.RED);
+ contentStream.beginText();
+ contentStream.setFont(font, 12);
+ contentStream.newLineAtOffset(80, 500);
+ contentStream.showText(
+ "This is from a disabled layer. If you see this, that's NOT good!");
+ contentStream.endText();
+ contentStream.endMarkedContent();
+
+ contentStream.close();
+
+ File targetFile = new File(testResultsDir, "ocg-generation-same-name.pdf");
+ doc.save(targetFile.getAbsolutePath());
+ }
+ finally
+ {
+ doc.close();
+ }
+ }
}