use of com.tom_roush.pdfbox.pdmodel.PDPageContentStream 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();
}
use of com.tom_roush.pdfbox.pdmodel.PDPageContentStream 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.PDPageContentStream 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);
}
}
use of com.tom_roush.pdfbox.pdmodel.PDPageContentStream 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();
}
}
use of com.tom_roush.pdfbox.pdmodel.PDPageContentStream in project PdfBox-Android by TomRoush.
the class TestOptionalContentGroups method testOCGGeneration.
/**
* Tests OCG generation.
* @throws Exception if an error occurs
*/
public void testOCGGeneration() 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 OCG for background
PDOptionalContentGroup background = new PDOptionalContentGroup("background");
ocprops.addGroup(background);
assertTrue(ocprops.isGroupEnabled("background"));
// Create OCG for enabled
PDOptionalContentGroup enabled = new PDOptionalContentGroup("enabled");
ocprops.addGroup(enabled);
assertFalse(ocprops.setGroupEnabled("enabled", true));
assertTrue(ocprops.isGroupEnabled("enabled"));
// Create OCG for disabled
PDOptionalContentGroup disabled = new PDOptionalContentGroup("disabled");
ocprops.addGroup(disabled);
assertFalse(ocprops.setGroupEnabled("disabled", true));
assertTrue(ocprops.isGroupEnabled("disabled"));
assertTrue(ocprops.setGroupEnabled("disabled", false));
assertFalse(ocprops.isGroupEnabled("disabled"));
// 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, background);
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 a green textline, but no red text line.");
contentStream.endText();
contentStream.endMarkedContent();
// Paint enabled layer
contentStream.beginMarkedContent(COSName.OC, enabled);
contentStream.setNonStrokingColor(AWTColor.GREEN);
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(80, 600);
contentStream.showText("This is from an enabled layer. If you see this, that's good.");
contentStream.endText();
contentStream.endMarkedContent();
// Paint disabled layer
contentStream.beginMarkedContent(COSName.OC, disabled);
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.pdf");
doc.save(targetFile.getAbsolutePath());
} finally {
doc.close();
}
}
Aggregations