Search in sources :

Example 1 with PDImageXObject

use of com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject in project PdfBox-Android by TomRoush.

the class PDPageContentStream method drawXObject.

/**
 * Draw an xobject(form or image) using the given {@link AffineTransform} to position
 * the xobject.
 *
 * @param xobject The xobject to draw.
 * @param transform the transformation matrix
 * @throws IOException If there is an error writing to the stream.
 * @throws IllegalStateException If the method was called within a text block.
 * @deprecated Use {@link #drawImage(PDImageXObject, Matrix) drawImage(PDImageXObject, Matrix)}
 * or {@link #drawForm(PDFormXObject) drawForm(PDFormXObject)} with
 * {@link #transform(Matrix) transform(Matrix)} instead.
 */
@Deprecated
public void drawXObject(PDXObject xobject, AffineTransform transform) throws IOException {
    if (inTextMode) {
        throw new IllegalStateException("Error: drawXObject is not allowed within a text block.");
    }
    String xObjectPrefix;
    if (xobject instanceof PDImageXObject) {
        xObjectPrefix = "Im";
    } else {
        xObjectPrefix = "Form";
    }
    COSName objMapping = resources.add(xobject, xObjectPrefix);
    saveGraphicsState();
    transform(new Matrix(transform));
    writeOperand(objMapping);
    writeOperator(OperatorName.DRAW_OBJECT);
    restoreGraphicsState();
}
Also used : PDImageXObject(com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject) Matrix(com.tom_roush.pdfbox.util.Matrix) COSName(com.tom_roush.pdfbox.cos.COSName)

Example 2 with PDImageXObject

use of com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject in project PdfBox-Android by TomRoush.

the class PDXObject method createXObject.

/**
 * Creates a new XObject instance of the appropriate type for the COS stream.
 *
 * @param base The stream which is wrapped by this XObject.
 * @param resources
 * @return A new XObject instance.
 * @throws java.io.IOException if there is an error creating the XObject.
 */
public static PDXObject createXObject(COSBase base, PDResources resources) throws IOException {
    if (base == null) {
        // TODO throw an exception?
        return null;
    }
    if (!(base instanceof COSStream)) {
        throw new IOException("Unexpected object type: " + base.getClass().getName());
    }
    COSStream stream = (COSStream) base;
    String subtype = stream.getNameAsString(COSName.SUBTYPE);
    if (COSName.IMAGE.getName().equals(subtype)) {
        return new PDImageXObject(new PDStream(stream), resources);
    } else if (COSName.FORM.getName().equals(subtype)) {
        ResourceCache cache = resources != null ? resources.getResourceCache() : null;
        COSDictionary group = (COSDictionary) stream.getDictionaryObject(COSName.GROUP);
        if (group != null && COSName.TRANSPARENCY.equals(group.getCOSName(COSName.S))) {
            return new PDTransparencyGroup(stream, cache);
        }
        return new PDFormXObject(stream, cache);
    } else if (COSName.PS.getName().equals(subtype)) {
        return new PDPostScriptXObject(stream);
    } else {
        throw new IOException("Invalid XObject Subtype: " + subtype);
    }
}
Also used : PDTransparencyGroup(com.tom_roush.pdfbox.pdmodel.graphics.form.PDTransparencyGroup) COSStream(com.tom_roush.pdfbox.cos.COSStream) PDImageXObject(com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) IOException(java.io.IOException) PDStream(com.tom_roush.pdfbox.pdmodel.common.PDStream) ResourceCache(com.tom_roush.pdfbox.pdmodel.ResourceCache)

Example 3 with PDImageXObject

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

use of com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject in project PdfBox-Android by TomRoush.

the class DrawObject method process.

@Override
public void process(Operator operator, List<COSBase> operands) throws IOException {
    if (operands.isEmpty()) {
        throw new MissingOperandException(operator, operands);
    }
    COSBase base0 = operands.get(0);
    if (!(base0 instanceof COSName)) {
        return;
    }
    COSName objectName = (COSName) base0;
    PDXObject xobject = context.getResources().getXObject(objectName);
    if (xobject == null) {
        throw new MissingResourceException("Missing XObject: " + objectName.getName());
    } else if (xobject instanceof PDImageXObject) {
        PDImageXObject image = (PDImageXObject) xobject;
        context.drawImage(image);
    } else if (xobject instanceof PDFormXObject) {
        try {
            context.increaseLevel();
            if (context.getLevel() > 25) {
                Log.e("PdfBox-Android", "recursion is too deep, skipping form XObject");
                return;
            }
            PDFormXObject form = (PDFormXObject) xobject;
            if (form instanceof PDTransparencyGroup) {
                context.showTransparencyGroup((PDTransparencyGroup) form);
            } else {
                context.showForm(form);
            }
        } finally {
            context.decreaseLevel();
        }
    }
}
Also used : PDTransparencyGroup(com.tom_roush.pdfbox.pdmodel.graphics.form.PDTransparencyGroup) PDImageXObject(com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject) COSName(com.tom_roush.pdfbox.cos.COSName) MissingOperandException(com.tom_roush.pdfbox.contentstream.operator.MissingOperandException) MissingResourceException(com.tom_roush.pdfbox.pdmodel.MissingResourceException) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) PDXObject(com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)

Aggregations

PDImageXObject (com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject)4 COSName (com.tom_roush.pdfbox.cos.COSName)2 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)2 PDTransparencyGroup (com.tom_roush.pdfbox.pdmodel.graphics.form.PDTransparencyGroup)2 IOException (java.io.IOException)2 Bitmap (android.graphics.Bitmap)1 MissingOperandException (com.tom_roush.pdfbox.contentstream.operator.MissingOperandException)1 COSBase (com.tom_roush.pdfbox.cos.COSBase)1 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)1 COSStream (com.tom_roush.pdfbox.cos.COSStream)1 MissingResourceException (com.tom_roush.pdfbox.pdmodel.MissingResourceException)1 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)1 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)1 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)1 ResourceCache (com.tom_roush.pdfbox.pdmodel.ResourceCache)1 PDStream (com.tom_roush.pdfbox.pdmodel.common.PDStream)1 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)1 PDXObject (com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)1 Matrix (com.tom_roush.pdfbox.util.Matrix)1 InputStream (java.io.InputStream)1