use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDStreamTest method testCreateInputStreamEmptyFilters.
/**
* Test for empty filter list
*/
@Test
public void testCreateInputStreamEmptyFilters() throws Exception {
PDDocument doc = new PDDocument();
InputStream is = new ByteArrayInputStream(new byte[] { 12, 34, 56, 78 });
PDStream pdStream = new PDStream(doc, is, new COSArray());
Assert.assertEquals(0, pdStream.getFilters().size());
List<String> stopFilters = new ArrayList<String>();
stopFilters.add(COSName.DCT_DECODE.toString());
stopFilters.add(COSName.DCT_DECODE_ABBREVIATION.toString());
is = pdStream.createInputStream(stopFilters);
Assert.assertEquals(12, is.read());
Assert.assertEquals(34, is.read());
Assert.assertEquals(56, is.read());
Assert.assertEquals(78, is.read());
Assert.assertEquals(-1, is.read());
doc.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDStreamTest method testCreateInputStreamNullFilters.
/**
* Test for null filter list (PDFBOX-2948)
*/
@Test
public void testCreateInputStreamNullFilters() throws Exception {
PDDocument doc = new PDDocument();
InputStream is = new ByteArrayInputStream(new byte[] { 12, 34, 56, 78 });
PDStream pdStream = new PDStream(doc, is, (COSArray) null);
Assert.assertNull(pdStream.getFilters());
List<String> stopFilters = new ArrayList<String>();
stopFilters.add(COSName.DCT_DECODE.toString());
stopFilters.add(COSName.DCT_DECODE_ABBREVIATION.toString());
is = pdStream.createInputStream(stopFilters);
Assert.assertEquals(12, is.read());
Assert.assertEquals(34, is.read());
Assert.assertEquals(56, is.read());
Assert.assertEquals(78, is.read());
Assert.assertEquals(-1, is.read());
doc.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDStreamTest method testCreateInputStreamNullStopFilters.
/**
* Test for null stop filters
*/
@Test
public void testCreateInputStreamNullStopFilters() throws Exception {
PDDocument doc = new PDDocument();
InputStream is = new ByteArrayInputStream(new byte[] { 12, 34, 56, 78 });
PDStream pdStream = new PDStream(doc, is, new COSArray());
Assert.assertEquals(0, pdStream.getFilters().size());
is = pdStream.createInputStream((List<String>) null);
Assert.assertEquals(12, is.read());
Assert.assertEquals(34, is.read());
Assert.assertEquals(56, is.read());
Assert.assertEquals(78, is.read());
Assert.assertEquals(-1, is.read());
doc.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class TestEmbeddedFiles method testNullEmbeddedFile.
@Test
public void testNullEmbeddedFile() throws IOException {
PDEmbeddedFile embeddedFile = null;
boolean ok = false;
try {
PDDocument doc = PDDocument.load(getClass().getResourceAsStream("/pdfbox/com/tom_roush/pdfbox/pdmodel/common/null_PDComplexFileSpecification.pdf"));
PDDocumentCatalog catalog = doc.getDocumentCatalog();
PDDocumentNameDictionary names = catalog.getNames();
assertEquals("expected two files", 2, names.getEmbeddedFiles().getNames().size());
PDEmbeddedFilesNameTreeNode embeddedFiles = names.getEmbeddedFiles();
PDComplexFileSpecification spec = embeddedFiles.getNames().get("non-existent-file.docx");
if (spec != null) {
embeddedFile = spec.getEmbeddedFile();
ok = true;
}
// now test for actual attachment
spec = embeddedFiles.getNames().get("My first attachment");
assertNotNull("one attachment actually exists", spec);
assertEquals("existing file length", 17660, spec.getEmbeddedFile().getLength());
spec = embeddedFiles.getNames().get("non-existent-file.docx");
} catch (NullPointerException e) {
assertNotNull("null pointer exception", null);
}
assertTrue("Was able to get file without exception", ok);
assertNull("EmbeddedFile was correctly null", embeddedFile);
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class TestOptionalContentGroups method testOCGConsumption.
/**
* Tests OCG functions on a loaded PDF.
* @throws Exception if an error occurs
*/
public void testOCGConsumption() throws Exception {
File pdfFile = new File(testResultsDir, "ocg-generation.pdf");
if (!pdfFile.exists()) {
testOCGGeneration();
}
PDDocument doc = PDDocument.load(pdfFile);
try {
assertEquals(1.5f, doc.getVersion());
PDDocumentCatalog catalog = doc.getDocumentCatalog();
PDPage page = doc.getPage(0);
PDResources resources = page.getResources();
COSName mc0 = COSName.getPDFName("oc1");
PDOptionalContentGroup ocg = (PDOptionalContentGroup) resources.getProperties(mc0);
assertNotNull(ocg);
assertEquals("background", ocg.getName());
assertNull(resources.getProperties(COSName.getPDFName("inexistent")));
PDOptionalContentProperties ocgs = catalog.getOCProperties();
assertEquals(BaseState.ON, ocgs.getBaseState());
Set<String> names = new java.util.HashSet<String>(Arrays.asList(ocgs.getGroupNames()));
assertEquals(3, names.size());
assertTrue(names.contains("background"));
assertTrue(ocgs.isGroupEnabled("background"));
assertTrue(ocgs.isGroupEnabled("enabled"));
assertFalse(ocgs.isGroupEnabled("disabled"));
ocgs.setGroupEnabled("background", false);
assertFalse(ocgs.isGroupEnabled("background"));
PDOptionalContentGroup background = ocgs.getGroup("background");
assertEquals(ocg.getName(), background.getName());
assertNull(ocgs.getGroup("inexistent"));
Collection<PDOptionalContentGroup> coll = ocgs.getOptionalContentGroups();
assertEquals(3, coll.size());
Set<String> nameSet = new HashSet<String>();
for (PDOptionalContentGroup ocg2 : coll) {
nameSet.add(ocg2.getName());
}
assertTrue(nameSet.contains("background"));
assertTrue(nameSet.contains("enabled"));
assertTrue(nameSet.contains("disabled"));
} finally {
doc.close();
}
}
Aggregations