Search in sources :

Example 6 with PDPage

use of com.tom_roush.pdfbox.pdmodel.PDPage in project PdfBox-Android by TomRoush.

the class PDAcroFormTest method createAcroFormWithMissingResourceInformation.

private byte[] createAcroFormWithMissingResourceInformation() throws IOException {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    PDAcroForm newAcroForm = new PDAcroForm(document);
    document.getDocumentCatalog().setAcroForm(newAcroForm);
    PDTextField textBox = new PDTextField(newAcroForm);
    textBox.setPartialName("SampleField");
    newAcroForm.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);
    // acroForm.setNeedAppearances(true);
    // acroForm.getField("SampleField").getCOSObject().setString(COSName.V, "content");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // this is a working PDF
    document.save(baos);
    document.close();
    return baos.toByteArray();
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDAnnotationWidget(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 7 with PDPage

use of com.tom_roush.pdfbox.pdmodel.PDPage in project PdfBox-Android by TomRoush.

the class PDAcroFormTest method testAcroFormDefaultFonts.

/**
 * PDFBOX-3732, PDFBOX-4303, PDFBOX-4393: Test whether /Helv and /ZaDb get added, but only if
 * they don't exist.
 */
@Test
public void testAcroFormDefaultFonts() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PDDocument doc = new PDDocument();
    PDPage page = new PDPage(PDRectangle.A4);
    doc.addPage(page);
    PDAcroForm acroForm2 = new PDAcroForm(doc);
    doc.getDocumentCatalog().setAcroForm(acroForm2);
    PDResources defaultResources = acroForm2.getDefaultResources();
    assertNull(defaultResources);
    defaultResources = new PDResources();
    acroForm2.setDefaultResources(defaultResources);
    assertNull(defaultResources.getFont(COSName.HELV));
    assertNull(defaultResources.getFont(COSName.ZA_DB));
    // getting AcroForm sets the two fonts
    acroForm2 = doc.getDocumentCatalog().getAcroForm();
    defaultResources = acroForm2.getDefaultResources();
    assertNotNull(defaultResources.getFont(COSName.HELV));
    assertNotNull(defaultResources.getFont(COSName.ZA_DB));
    // repeat with a new AcroForm (to delete AcroForm cache) and thus missing /DR
    doc.getDocumentCatalog().setAcroForm(new PDAcroForm(doc));
    acroForm2 = doc.getDocumentCatalog().getAcroForm();
    defaultResources = acroForm2.getDefaultResources();
    PDFont helv = defaultResources.getFont(COSName.HELV);
    PDFont zadb = defaultResources.getFont(COSName.ZA_DB);
    assertNotNull(helv);
    assertNotNull(zadb);
    doc.save(baos);
    doc.close();
    doc = PDDocument.load(baos.toByteArray());
    acroForm2 = doc.getDocumentCatalog().getAcroForm();
    defaultResources = acroForm2.getDefaultResources();
    helv = defaultResources.getFont(COSName.HELV);
    zadb = defaultResources.getFont(COSName.ZA_DB);
    assertNotNull(helv);
    assertNotNull(zadb);
    // make sure that font wasn't overwritten
    assertNotEquals(PDType1Font.HELVETICA, helv);
    assertNotEquals(PDType1Font.ZAPF_DINGBATS, zadb);
    doc.close();
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 8 with PDPage

use of com.tom_roush.pdfbox.pdmodel.PDPage in project PdfBox-Android by TomRoush.

the class TestFontEmbedding method testCIDFontType2VerticalSubsetProportional.

/**
 * Embed a proportional TTF as vertical CIDFontType2 with subsetting.
 *
 * @throws IOException
 */
@Test
public void testCIDFontType2VerticalSubsetProportional() throws IOException {
    String text = "「ABC」";
    String expectedExtractedtext = "「\nA\nB\nC\n」";
    File pdf = new File(OUT_DIR, "CIDFontType2VP.pdf");
    PDDocument document = new PDDocument();
    PDPage page = new PDPage(PDRectangle.A4);
    document.addPage(page);
    File ipafont = new File("target/fonts/ipagp00303", "ipagp.ttf");
    assumeTrue(ipafont.exists());
    PDType0Font vfont = PDType0Font.loadVertical(document, ipafont);
    PDPageContentStream contentStream = new PDPageContentStream(document, page);
    contentStream.beginText();
    contentStream.setFont(vfont, 20);
    contentStream.newLineAtOffset(50, 700);
    contentStream.showText(text);
    contentStream.endText();
    contentStream.close();
    // Check the font substitution
    byte[] encode = vfont.encode(text);
    int cid = ((encode[0] & 0xFF) << 8) + (encode[1] & 0xFF);
    // it's 12461 without substitution
    assertEquals(12607, cid);
    // Check the dictionaries
    COSDictionary fontDict = vfont.getCOSObject();
    assertEquals(COSName.IDENTITY_V, fontDict.getDictionaryObject(COSName.ENCODING));
    document.save(pdf);
    // Vertical metrics are fixed during subsetting, so do this after calling save()
    COSDictionary descFontDict = vfont.getDescendantFont().getCOSObject();
    COSArray dw2 = (COSArray) descFontDict.getDictionaryObject(COSName.DW2);
    // This font uses default values for DW2
    assertNull(dw2);
    // c [ w1_1y v_1x v_1y ... w1_ny v_nx v_ny ]
    COSArray w2 = (COSArray) descFontDict.getDictionaryObject(COSName.W2);
    assertEquals(2, w2.size());
    // Start CID
    assertEquals(12607, w2.getInt(0));
    COSArray metrics = (COSArray) w2.getObject(1);
    int i = 0;
    for (int n : new int[] { -570, 500, 450, -570, 500, 880 }) {
        assertEquals(n, metrics.getInt(i++));
    }
    document.close();
    // Check text extraction
    String extracted = getUnicodeText(pdf);
    assertEquals(expectedExtractedtext, extracted.replaceAll("\r", "").trim());
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSArray(com.tom_roush.pdfbox.cos.COSArray) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) File(java.io.File) Test(org.junit.Test)

Example 9 with PDPage

use of com.tom_roush.pdfbox.pdmodel.PDPage in project PdfBox-Android by TomRoush.

the class TestFontEmbedding method testCIDFontType2VerticalSubsetMonospace.

/**
 * Embed a monospace TTF as vertical CIDFontType2 with subsetting.
 *
 * @throws IOException
 */
@Test
public void testCIDFontType2VerticalSubsetMonospace() throws IOException {
    String text = "「ABC」";
    String expectedExtractedtext = "「\nA\nB\nC\n」";
    File pdf = new File(OUT_DIR, "CIDFontType2VM.pdf");
    PDDocument document = new PDDocument();
    PDPage page = new PDPage(PDRectangle.A4);
    document.addPage(page);
    File ipafont = new File("target/fonts/ipag00303", "ipag.ttf");
    assumeTrue(ipafont.exists());
    PDType0Font vfont = PDType0Font.loadVertical(document, ipafont);
    PDPageContentStream contentStream = new PDPageContentStream(document, page);
    contentStream.beginText();
    contentStream.setFont(vfont, 20);
    contentStream.newLineAtOffset(50, 700);
    contentStream.showText(text);
    contentStream.endText();
    contentStream.close();
    // Check the font substitution
    byte[] encode = vfont.encode(text);
    int cid = ((encode[0] & 0xFF) << 8) + (encode[1] & 0xFF);
    // it's 441 without substitution
    assertEquals(7392, cid);
    // Check the dictionaries
    COSDictionary fontDict = vfont.getCOSObject();
    assertEquals(COSName.IDENTITY_V, fontDict.getDictionaryObject(COSName.ENCODING));
    document.save(pdf);
    // Vertical metrics are fixed during subsetting, so do this after calling save()
    COSDictionary descFontDict = vfont.getDescendantFont().getCOSObject();
    COSArray dw2 = (COSArray) descFontDict.getDictionaryObject(COSName.DW2);
    // This font uses default values for DW2
    assertNull(dw2);
    COSArray w2 = (COSArray) descFontDict.getDictionaryObject(COSName.W2);
    // Monospaced font has no entries
    assertEquals(0, w2.size());
    document.close();
    // Check text extraction
    String extracted = getUnicodeText(pdf);
    assertEquals(expectedExtractedtext, extracted.replaceAll("\r", "").trim());
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSArray(com.tom_roush.pdfbox.cos.COSArray) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) File(java.io.File) Test(org.junit.Test)

Example 10 with PDPage

use of com.tom_roush.pdfbox.pdmodel.PDPage in project PdfBox-Android by TomRoush.

the class TestFontEmbedding method validateCIDFontType2.

private void validateCIDFontType2(boolean useSubset) throws Exception {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage(PDRectangle.A4);
    document.addPage(page);
    InputStream input = PDFont.class.getResourceAsStream("/com/tom_roush/pdfbox/resources/ttf/LiberationSans-Regular.ttf");
    PDType0Font font = PDType0Font.load(document, input, useSubset);
    PDPageContentStream stream = new PDPageContentStream(document, page);
    stream.beginText();
    stream.setFont(font, 12);
    String text = "Unicode русский язык Tiếng Việt";
    stream.newLineAtOffset(50, 600);
    stream.showText(text);
    stream.endText();
    stream.close();
    File file = new File(OUT_DIR, "CIDFontType2.pdf");
    document.save(file);
    document.close();
    // check that the extracted text matches what we wrote
    String extracted = getUnicodeText(file);
    assertEquals(text, extracted.trim());
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) File(java.io.File)

Aggregations

PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)55 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)37 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)24 File (java.io.File)23 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)13 Test (org.junit.Test)13 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)11 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)10 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)8 IOException (java.io.IOException)7 Bitmap (android.graphics.Bitmap)6 COSArray (com.tom_roush.pdfbox.cos.COSArray)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)5 PDFRenderer (com.tom_roush.pdfbox.rendering.PDFRenderer)5 COSBase (com.tom_roush.pdfbox.cos.COSBase)4 PDAnnotation (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation)4 PDAnnotationWidget (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget)4 Paint (android.graphics.Paint)3 COSName (com.tom_roush.pdfbox.cos.COSName)3