use of com.tom_roush.pdfbox.pdmodel.PDPage in project PdfBox-Android by TomRoush.
the class TestOptionalContentGroups method testOCGsWithSameNameCanHaveDifferentVisibility.
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(AWTColor.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();
}
}
use of com.tom_roush.pdfbox.pdmodel.PDPage in project PdfBox-Android by TomRoush.
the class TestOptionalContentGroups method testOCGGeneration.
/**
* Tests OCG generation.
* @throws Exception if an error occurs
*/
public void testOCGGeneration() 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 OCG for background
PDOptionalContentGroup background = new PDOptionalContentGroup("background");
ocprops.addGroup(background);
assertTrue(ocprops.isGroupEnabled("background"));
// Create OCG for enabled
PDOptionalContentGroup enabled = new PDOptionalContentGroup("enabled");
ocprops.addGroup(enabled);
assertFalse(ocprops.setGroupEnabled("enabled", true));
assertTrue(ocprops.isGroupEnabled("enabled"));
// Create OCG for disabled
PDOptionalContentGroup disabled = new PDOptionalContentGroup("disabled");
ocprops.addGroup(disabled);
assertFalse(ocprops.setGroupEnabled("disabled", true));
assertTrue(ocprops.isGroupEnabled("disabled"));
assertTrue(ocprops.setGroupEnabled("disabled", false));
assertFalse(ocprops.isGroupEnabled("disabled"));
// 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, background);
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 a green textline, but no red text line.");
contentStream.endText();
contentStream.endMarkedContent();
// Paint enabled layer
contentStream.beginMarkedContent(COSName.OC, enabled);
contentStream.setNonStrokingColor(AWTColor.GREEN);
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(80, 600);
contentStream.showText("This is from an enabled layer. If you see this, that's good.");
contentStream.endText();
contentStream.endMarkedContent();
// Paint disabled layer
contentStream.beginMarkedContent(COSName.OC, disabled);
contentStream.setNonStrokingColor(AWTColor.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.pdf");
doc.save(targetFile.getAbsolutePath());
} finally {
doc.close();
}
}
use of com.tom_roush.pdfbox.pdmodel.PDPage in project PdfBox-Android by TomRoush.
the class TestFontEmbedding method testMaxEntries.
/**
* Test corner case of PDFBOX-4302.
*
* @throws java.io.IOException
*/
@Test
public void testMaxEntries() throws IOException {
File file;
String text;
text = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん" + "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン" + "12345678";
// The test must have MAX_ENTRIES_PER_OPERATOR unique characters
Set<Character> set = new HashSet<Character>(ToUnicodeWriter.MAX_ENTRIES_PER_OPERATOR);
for (int i = 0; i < text.length(); ++i) {
set.add(text.charAt(i));
}
assertEquals(ToUnicodeWriter.MAX_ENTRIES_PER_OPERATOR, set.size());
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A0);
document.addPage(page);
File ipafont = new File("target/fonts/ipag00303", "ipag.ttf");
assumeTrue(ipafont.exists());
PDType0Font font = PDType0Font.load(document, ipafont);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.beginText();
contentStream.setFont(font, 20);
contentStream.newLineAtOffset(50, 3000);
contentStream.showText(text);
contentStream.endText();
contentStream.close();
file = new File(OUT_DIR, "PDFBOX-4302-test.pdf");
document.save(file);
document.close();
// check that the extracted text matches what we wrote
String extracted = getUnicodeText(file);
assertEquals(text, extracted.trim());
}
use of com.tom_roush.pdfbox.pdmodel.PDPage in project PdfBox-Android by TomRoush.
the class TestFontEncoding method testPDFBox3884.
/**
* PDFBOX-3826: Some unicodes are reached by several names in glyphlist.txt, e.g. tilde and
* ilde.
*
* @throws IOException
*/
public void testPDFBox3884() throws IOException {
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDPageContentStream cs = new PDPageContentStream(doc, page);
cs.setFont(PDType1Font.HELVETICA, 20);
cs.beginText();
cs.newLineAtOffset(100, 700);
// first tilde is "asciitilde" (from the keyboard), 2nd tilde is "tilde"
// using ˜ would bring IllegalArgumentException prior to bugfix
cs.showText("~˜");
cs.endText();
cs.close();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
doc.save(baos);
doc.close();
// verify
doc = PDDocument.load(baos.toByteArray());
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(doc);
assertEquals("~˜", text.trim());
doc.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDPage in project PdfBox-Android by TomRoush.
the class PDAcroFormTest method testBadDA.
/**
* PDFBOX-4235: a bad /DA string should not result in an NPE.
*
* @throws IOException
*/
@Test
public void testBadDA() throws IOException {
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDAcroForm acroForm = new PDAcroForm(document);
doc.getDocumentCatalog().setAcroForm(acroForm);
acroForm.setDefaultResources(new PDResources());
PDTextField textBox = new PDTextField(acroForm);
textBox.setPartialName("SampleField");
// https://stackoverflow.com/questions/50609478/
// "tf" is a typo, should have been "Tf" and this results that no font is chosen
textBox.setDefaultAppearance("/Helv 0 tf 0 g");
acroForm.getFields().add(textBox);
PDAnnotationWidget widget = textBox.getWidgets().get(0);
PDRectangle rect = new PDRectangle(50, 750, 200, 20);
widget.setRectangle(rect);
widget.setPage(page);
page.getAnnotations().add(widget);
try {
textBox.setValue("huhu");
} catch (IllegalArgumentException ex) {
return;
} finally {
doc.close();
}
fail("IllegalArgumentException should have been thrown");
}
Aggregations