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();
}
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);
}
}
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);
}
}
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();
}
}
}
Aggregations