Search in sources :

Example 46 with PDPage

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

the class PDFontTest method testPDFBox3826createDoc.

private byte[] testPDFBox3826createDoc(TrueTypeFont ttf) throws IOException {
    PDDocument doc = new PDDocument();
    PDPage page = new PDPage();
    doc.addPage(page);
    // type 0 subset embedding
    PDFont font = PDType0Font.load(doc, ttf, true);
    PDPageContentStream cs = new PDPageContentStream(doc, page);
    cs.beginText();
    cs.newLineAtOffset(10, 700);
    cs.setFont(font, 10);
    cs.showText("testMultipleFontFileReuse1");
    cs.endText();
    // type 0 full embedding
    font = PDType0Font.load(doc, ttf, false);
    cs.beginText();
    cs.newLineAtOffset(10, 650);
    cs.setFont(font, 10);
    cs.showText("testMultipleFontFileReuse2");
    cs.endText();
    // tt full embedding but only WinAnsiEncoding
    font = PDTrueTypeFont.load(doc, ttf, WinAnsiEncoding.INSTANCE);
    cs.beginText();
    cs.newLineAtOffset(10, 600);
    cs.setFont(font, 10);
    cs.showText("testMultipleFontFileReuse3");
    cs.endText();
    cs.close();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    doc.save(baos);
    doc.close();
    return baos.toByteArray();
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 47 with PDPage

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

the class CCITTFactoryTest method testCreateFromRandomAccessSingle.

/**
 * Tests CCITTFactory#createFromRandomAccess(PDDocument document,
 * RandomAccess reader) with a single page TIFF
 */
public void testCreateFromRandomAccessSingle() throws IOException {
    String tiffG3Path = "pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/ccittg3.tif";
    String tiffG4Path = "pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/ccittg4.tif";
    PDDocument document = new PDDocument();
    PDImageXObject ximage3 = CCITTFactory.createFromRandomAccess(document, new RandomAccessBuffer(testContext.getAssets().open(tiffG3Path)));
    validate(ximage3, 1, 344, 287, "tiff", PDDeviceGray.INSTANCE.getName());
    // Bitmap bim3 = ImageIO.read(new File(tiffG3Path));
    // checkIdent(bim3, ximage3.getOpaqueImage());
    PDPage page = new PDPage(PDRectangle.A4);
    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false);
    contentStream.drawImage(ximage3, 0, 0, ximage3.getWidth(), ximage3.getHeight());
    contentStream.close();
    PDImageXObject ximage4 = CCITTFactory.createFromRandomAccess(document, new RandomAccessBuffer(testContext.getAssets().open(tiffG4Path)));
    validate(ximage4, 1, 344, 287, "tiff", PDDeviceGray.INSTANCE.getName());
    // Bitmap bim4 = ImageIO.read(new File(tiffG3Path));
    // checkIdent(bim4, ximage4.getOpaqueImage());
    page = new PDPage(PDRectangle.A4);
    document.addPage(page);
    contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false);
    contentStream.drawImage(ximage4, 0, 0);
    contentStream.close();
    document.save(testResultsDir + "/singletiff.pdf");
    document.close();
    document = PDDocument.load(new File(testResultsDir, "singletiff.pdf"));
    assertEquals(2, document.getNumberOfPages());
    document.close();
}
Also used : RandomAccessBuffer(com.tom_roush.pdfbox.io.RandomAccessBuffer) 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)

Example 48 with PDPage

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

the class LosslessFactoryTest method testCreateLosslessFromImageRGB.

/**
 * Tests RGB LosslessFactoryTest#createFromImage(PDDocument document,
 * BufferedImage image)
 *
 * @throws java.io.IOException
 */
@Test
public void testCreateLosslessFromImageRGB() throws IOException {
    PDDocument document = new PDDocument();
    Bitmap image = BitmapFactory.decodeStream(testContext.getAssets().open("pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/png.png"));
    PDImageXObject ximage1 = LosslessFactory.createFromImage(document, image);
    validate(ximage1, 8, image.getWidth(), image.getHeight(), "png", PDDeviceRGB.INSTANCE.getName());
    checkIdent(image, ximage1.getImage());
    // Create a grayscale image
    Bitmap grayImage = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ALPHA_8);
    Canvas canvas = new Canvas();
    canvas.setBitmap(grayImage);
    Paint paint = new Paint();
    paint.setColor(Color.TRANSPARENT);
    canvas.drawBitmap(image, 0, 0, paint);
    PDImageXObject ximage2 = LosslessFactory.createFromImage(document, grayImage);
    validate(ximage2, 8, grayImage.getWidth(), grayImage.getHeight(), "png", PDDeviceGray.INSTANCE.getName());
    checkIdent(grayImage, ximage2.getImage());
    // Create a bitonal image
    // BufferedImage bitonalImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY); TODO: PdfBox-Android
    // avoid multiple of 8 to test padding
    // assertFalse(bitonalImage.getWidth() % 8 == 0);
    // g = bitonalImage.getGraphics();
    // g.drawImage(image, 0, 0, null);
    // g.dispose();
    // PDImageXObject ximage3 = LosslessFactory.createFromImage(document, bitonalImage);
    // validate(ximage3, 1, bitonalImage.getWidth(), bitonalImage.getHeight(), "png", PDDeviceGray.INSTANCE.getName());
    // checkIdent(bitonalImage, ximage3.getImage());
    // This part isn't really needed because this test doesn't break
    // if the mask has the wrong colorspace (PDFBOX-2057), but it is still useful
    // if something goes wrong in the future and we want to have a PDF to open.
    PDPage page = new PDPage();
    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, false);
    contentStream.drawImage(ximage1, 200, 300, ximage1.getWidth() / 2, ximage1.getHeight() / 2);
    contentStream.drawImage(ximage2, 200, 450, ximage2.getWidth() / 2, ximage2.getHeight() / 2);
    // contentStream.drawImage(ximage3, 200, 600, ximage3.getWidth() / 2, ximage3.getHeight() / 2);
    contentStream.close();
    File pdfFile = new File(testResultsDir, "misc.pdf");
    document.save(pdfFile);
    document.close();
    document = PDDocument.load(pdfFile, (String) null);
    new PDFRenderer(document).renderImage(0);
    document.close();
}
Also used : Bitmap(android.graphics.Bitmap) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) Canvas(android.graphics.Canvas) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) Paint(android.graphics.Paint) File(java.io.File) PDFRenderer(com.tom_roush.pdfbox.rendering.PDFRenderer) FlakyTest(androidx.test.filters.FlakyTest) Test(org.junit.Test)

Example 49 with PDPage

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

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

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