Search in sources :

Example 11 with PDFont

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

the class TestLayerUtility method createOverlay1.

private File createOverlay1() throws IOException {
    File targetFile = new File(testResultsDir, "overlay1.pdf");
    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);
        }
        // Setup page content stream and paint background/title
        PDPageContentStream contentStream = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.OVERWRITE, false);
        PDFont font = PDType1Font.HELVETICA_BOLD;
        contentStream.setNonStrokingColor(AWTColor.LIGHT_GRAY);
        contentStream.beginText();
        float fontSize = 96;
        contentStream.setFont(font, fontSize);
        String text = "OVERLAY";
        // float sw = font.getStringWidth(text);
        // Too bad, base 14 fonts don't return character metrics.
        PDRectangle crop = page.getCropBox();
        float cx = crop.getWidth() / 2f;
        float cy = crop.getHeight() / 2f;
        Matrix transform = new Matrix();
        transform.translate(cx, cy);
        transform.rotate(Math.toRadians(45));
        transform.translate(-190, /* sw/2 */
        0);
        contentStream.setTextMatrix(transform);
        contentStream.showText(text);
        contentStream.endText();
        contentStream.close();
        doc.save(targetFile.getAbsolutePath());
    } finally {
        doc.close();
    }
    return targetFile;
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) Matrix(com.tom_roush.pdfbox.util.Matrix) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) File(java.io.File)

Example 12 with PDFont

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

the class PDAbstractContentStream method showTextInternal.

/**
 * Outputs a string using the correct encoding and subsetting as required.
 *
 * @param text The Unicode text to show.
 *
 * @throws IOException If an io exception occurs.
 */
protected void showTextInternal(String text) throws IOException {
    if (!inTextMode) {
        throw new IllegalStateException("Must call beginText() before showText()");
    }
    if (fontStack.isEmpty()) {
        throw new IllegalStateException("Must call setFont() before showText()");
    }
    PDFont font = fontStack.peek();
    // complex text layout
    byte[] encodedText = null;
    if (encodedText == null) {
        encodedText = font.encode(text);
    }
    // Unicode code points to keep when subsetting
    if (font.willBeSubset()) {
        int offset = 0;
        while (offset < text.length()) {
            int codePoint = text.codePointAt(offset);
            font.addToSubset(codePoint);
            offset += Character.charCount(codePoint);
        }
    }
    COSWriter.writeString(encodedText, outputStream);
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont)

Example 13 with PDFont

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

the class MainActivity method createPdf.

/**
 * Creates a new PDF from scratch and saves it to a file
 */
public void createPdf(View v) {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    // Create a new font object selecting one of the PDF base fonts
    PDFont font = PDType1Font.HELVETICA;
    // Or a custom font
    // try
    // {
    // // Replace MyFontFile with the path to the asset font you'd like to use.
    // // Or use LiberationSans "com/tom_roush/pdfbox/resources/ttf/LiberationSans-Regular.ttf"
    // font = PDType0Font.load(document, assetManager.open("MyFontFile.TTF"));
    // }
    // catch (IOException e)
    // {
    // Log.e("PdfBox-Android-Sample", "Could not load font", e);
    // }
    PDPageContentStream contentStream;
    try {
        // Define a content stream for adding to the PDF
        contentStream = new PDPageContentStream(document, page);
        // Write Hello World in blue text
        contentStream.beginText();
        contentStream.setNonStrokingColor(15, 38, 192);
        contentStream.setFont(font, 12);
        contentStream.newLineAtOffset(100, 700);
        contentStream.showText("Hello World");
        contentStream.endText();
        // Load in the images
        InputStream in = assetManager.open("falcon.jpg");
        InputStream alpha = assetManager.open("trans.png");
        // Draw a green rectangle
        contentStream.addRect(5, 500, 100, 100);
        contentStream.setNonStrokingColor(0, 255, 125);
        contentStream.fill();
        // Draw the falcon base image
        PDImageXObject ximage = JPEGFactory.createFromStream(document, in);
        contentStream.drawImage(ximage, 20, 20);
        // Draw the red overlay image
        Bitmap alphaImage = BitmapFactory.decodeStream(alpha);
        PDImageXObject alphaXimage = LosslessFactory.createFromImage(document, alphaImage);
        contentStream.drawImage(alphaXimage, 20, 20);
        // Make sure that the content stream is closed:
        contentStream.close();
        // Save the final pdf document to a file
        String path = root.getAbsolutePath() + "/Created.pdf";
        document.save(path);
        document.close();
        tv.setText("Successfully wrote PDF to " + path);
    } catch (IOException e) {
        Log.e("PdfBox-Android-Sample", "Exception thrown while creating PDF", e);
    }
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) PDImageXObject(com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject) Bitmap(android.graphics.Bitmap) 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) IOException(java.io.IOException)

Example 14 with PDFont

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

the class MainActivity method createEncryptedPdf.

/**
 * Creates a simple pdf and encrypts it
 */
public void createEncryptedPdf(View v) {
    String path = root.getAbsolutePath() + "/crypt.pdf";
    // 128 bit is the highest currently supported
    int keyLength = 128;
    // Limit permissions of those without the password
    AccessPermission ap = new AccessPermission();
    ap.setCanPrint(false);
    // Sets the owner password and user password
    StandardProtectionPolicy spp = new StandardProtectionPolicy("12345", "hi", ap);
    // Setups up the encryption parameters
    spp.setEncryptionKeyLength(keyLength);
    spp.setPermissions(ap);
    BouncyCastleProvider provider = new BouncyCastleProvider();
    Security.addProvider(provider);
    PDFont font = PDType1Font.HELVETICA;
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    try {
        PDPageContentStream contentStream = new PDPageContentStream(document, page);
        // Write Hello World in blue text
        contentStream.beginText();
        contentStream.setNonStrokingColor(15, 38, 192);
        contentStream.setFont(font, 12);
        contentStream.newLineAtOffset(100, 700);
        contentStream.showText("Hello World");
        contentStream.endText();
        contentStream.close();
        // Save the final pdf document to a file
        // Apply the protections to the PDF
        document.protect(spp);
        document.save(path);
        document.close();
        tv.setText("Successfully wrote PDF to " + path);
    } catch (IOException e) {
        Log.e("PdfBox-Android-Sample", "Exception thrown while creating PDF for encryption", e);
    }
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) StandardProtectionPolicy(com.tom_roush.pdfbox.pdmodel.encryption.StandardProtectionPolicy) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) AccessPermission(com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) IOException(java.io.IOException) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 15 with PDFont

use of com.tom_roush.pdfbox.pdmodel.font.PDFont 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();
    }
}
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) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) File(java.io.File)

Aggregations

PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)19 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)8 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)8 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)8 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)7 IOException (java.io.IOException)6 File (java.io.File)5 COSBase (com.tom_roush.pdfbox.cos.COSBase)4 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)3 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)3 Bitmap (android.graphics.Bitmap)2 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)2 COSName (com.tom_roush.pdfbox.cos.COSName)2 COSString (com.tom_roush.pdfbox.cos.COSString)2 PDTextState (com.tom_roush.pdfbox.pdmodel.graphics.state.PDTextState)2 Matrix (com.tom_roush.pdfbox.util.Matrix)2 InputStream (java.io.InputStream)2 Test (org.junit.Test)2 MissingOperandException (com.tom_roush.pdfbox.contentstream.operator.MissingOperandException)1 COSObject (com.tom_roush.pdfbox.cos.COSObject)1