Search in sources :

Example 1 with PDPageContentStream

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

the class LayerUtility method appendFormAsLayer.

/**
 * Places the given form over the existing content of the indicated page (like an overlay).
 * The form is enveloped in a marked content section to indicate that it's part of an
 * optional content group (OCG), here used as a layer. This optional group is returned and
 * can be enabled and disabled through methods on {@link PDOptionalContentProperties}.
 * <p>
 * You may want to call {@link #wrapInSaveRestore(PDPage) wrapInSaveRestore(PDPage)} before calling this method to make
 * sure that the graphics state is reset.
 *
 * @param targetPage the target page
 * @param form the form to place
 * @param transform the transformation matrix that controls the placement of your form. You'll
 * need this if your page has a crop box different than the media box, or if these have negative
 * coordinates, or if you want to scale or adjust your form.
 * @param layerName the name for the layer/OCG to produce
 * @return the optional content group that was generated for the form usage
 * @throws IOException if an I/O error occurs
 */
public PDOptionalContentGroup appendFormAsLayer(PDPage targetPage, PDFormXObject form, AffineTransform transform, String layerName) throws IOException {
    PDDocumentCatalog catalog = targetDoc.getDocumentCatalog();
    PDOptionalContentProperties ocprops = catalog.getOCProperties();
    if (ocprops == null) {
        ocprops = new PDOptionalContentProperties();
        catalog.setOCProperties(ocprops);
    }
    if (ocprops.hasGroup(layerName)) {
        throw new IllegalArgumentException("Optional group (layer) already exists: " + layerName);
    }
    PDRectangle cropBox = targetPage.getCropBox();
    if ((cropBox.getLowerLeftX() < 0 || cropBox.getLowerLeftY() < 0) && transform.isIdentity()) {
        // PDFBOX-4044
        Log.w("PdfBox-Android", "Negative cropBox " + cropBox + " and identity transform may make your form invisible");
    }
    PDOptionalContentGroup layer = new PDOptionalContentGroup(layerName);
    ocprops.addGroup(layer);
    PDPageContentStream contentStream = new PDPageContentStream(targetDoc, targetPage, AppendMode.APPEND, !DEBUG);
    contentStream.beginMarkedContent(COSName.OC, layer);
    contentStream.saveGraphicsState();
    contentStream.transform(new Matrix(transform));
    contentStream.drawForm(form);
    contentStream.restoreGraphicsState();
    contentStream.endMarkedContent();
    contentStream.close();
    return layer;
}
Also used : Matrix(com.tom_roush.pdfbox.util.Matrix) PDOptionalContentGroup(com.tom_roush.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentGroup) PDOptionalContentProperties(com.tom_roush.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentProperties) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) PDDocumentCatalog(com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)

Example 2 with PDPageContentStream

use of com.tom_roush.pdfbox.pdmodel.PDPageContentStream 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 3 with PDPageContentStream

use of com.tom_roush.pdfbox.pdmodel.PDPageContentStream 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 4 with PDPageContentStream

use of com.tom_roush.pdfbox.pdmodel.PDPageContentStream 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)

Example 5 with PDPageContentStream

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

the class PDFontTest method testPDFBOX4115.

/**
 * PDFBOX-4115: Test ability to create PDF with german umlaut glyphs with a type 1 font.
 * Test for everything that went wrong before this was fixed.
 *
 * @throws IOException
 */
@Test
public void testPDFBOX4115() throws IOException {
    File fontFile = TestResourceGenerator.downloadTestResource(IN_DIR, "n019003l.pfb", "https://issues.apache.org/jira/secure/attachment/12911053/n019003l.pfb");
    assumeTrue(fontFile.exists());
    File outputFile = new File(OUT_DIR, "FontType1.pdf");
    String text = "äöüÄÖÜ";
    PDDocument doc = new PDDocument();
    PDPage page = new PDPage();
    PDPageContentStream contentStream = new PDPageContentStream(doc, page);
    PDType1Font font = new PDType1Font(doc, new FileInputStream(fontFile), WinAnsiEncoding.INSTANCE);
    contentStream.beginText();
    contentStream.setFont(font, 10);
    contentStream.newLineAtOffset(10, 700);
    contentStream.showText(text);
    contentStream.endText();
    contentStream.close();
    doc.addPage(page);
    doc.save(outputFile);
    doc.close();
    doc = PDDocument.load(outputFile);
    font = (PDType1Font) doc.getPage(0).getResources().getFont(COSName.getPDFName("F1"));
    Assert.assertEquals(font.getEncoding(), WinAnsiEncoding.INSTANCE);
    for (char c : text.toCharArray()) {
        String name = font.getEncoding().getName(c);
        Assert.assertEquals("dieresis", name.substring(1));
        Assert.assertFalse(font.getPath(name).isEmpty());
    }
    PDFTextStripper stripper = new PDFTextStripper();
    Assert.assertEquals(text, stripper.getText(doc).trim());
    doc.close();
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) File(java.io.File) FileInputStream(java.io.FileInputStream) PDFTextStripper(com.tom_roush.pdfbox.text.PDFTextStripper) Test(org.junit.Test)

Aggregations

PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)27 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)24 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)22 File (java.io.File)18 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)8 Test (org.junit.Test)7 Bitmap (android.graphics.Bitmap)5 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)5 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)5 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)4 PDFRenderer (com.tom_roush.pdfbox.rendering.PDFRenderer)4 COSArray (com.tom_roush.pdfbox.cos.COSArray)3 Matrix (com.tom_roush.pdfbox.util.Matrix)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Paint (android.graphics.Paint)2 RandomAccessBuffer (com.tom_roush.pdfbox.io.RandomAccessBuffer)2 PDFTextStripper (com.tom_roush.pdfbox.text.PDFTextStripper)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2