use of org.apache.pdfbox.pdmodel.PDDocument in project paper2ebook by ogrisel.
the class Transformer method main.
public static void main(String[] args) throws IOException, COSVisitorException {
String original_pdf;
if (args.length < 1 || args.length > 2) {
System.err.println("Usage: java -jar paper2ebook-*.jar input.pdf [output.pdf]");
return;
} else {
original_pdf = args[0];
}
Transformer transformer = new Transformer(PDDocument.load(original_pdf));
PDDocument output = transformer.extract();
if (args.length == 1) {
String orig_no_pdf = original_pdf.substring(0, original_pdf.length() - 4);
output.save(orig_no_pdf + "_ebook.pdf");
} else {
output.save(args[1]);
}
}
use of org.apache.pdfbox.pdmodel.PDDocument in project mustangproject by ZUGFeRD.
the class ZUGFeRDExporterFromA1Factory method loadFromPDFA1.
/**
* Makes A PDF/A3a-compliant document from a PDF-A1 compliant document (on
* the metadata level, this will not e.g. convert graphics to JPG-2000)
*
* @param pdfBinary binary of a PDF/A1 compliant document
*/
public ZUGFeRDExporter loadFromPDFA1(byte[] pdfBinary) throws IOException, TransformerException {
ensurePDFIsValidA1(new ByteArrayDataSource(new ByteArrayInputStream(pdfBinary)));
PDDocument doc = PDDocument.load(pdfBinary);
makePDFA3compliant(doc);
return new ZUGFeRDExporter(doc);
}
use of org.apache.pdfbox.pdmodel.PDDocument in project camel by apache.
the class PdfProducer method doExtractText.
private String doExtractText(Exchange exchange) throws IOException, CryptographyException, InvalidPasswordException, BadSecurityHandlerException {
LOG.debug("Got {} operation, going to extract text from provided pdf.", pdfConfiguration.getOperation());
PDDocument document = exchange.getIn().getBody(PDDocument.class);
if (document.isEncrypted()) {
DecryptionMaterial decryptionMaterial = exchange.getIn().getHeader(DECRYPTION_MATERIAL_HEADER_NAME, DecryptionMaterial.class);
if (decryptionMaterial == null) {
throw new IllegalArgumentException(String.format("%s header is expected for %s operation " + "on encrypted document", DECRYPTION_MATERIAL_HEADER_NAME, pdfConfiguration.getOperation()));
}
document.openProtection(decryptionMaterial);
}
PDFTextStripper pdfTextStripper = new PDFTextStripper();
return pdfTextStripper.getText(document);
}
use of org.apache.pdfbox.pdmodel.PDDocument in project camel by apache.
the class PdfAppendTest method testAppend.
@Test
public void testAppend() throws Exception {
final String originalText = "Test";
final String textToAppend = "Append";
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.setFont(PDType1Font.HELVETICA, 12);
contentStream.beginText();
contentStream.moveTextPositionByAmount(20, 400);
contentStream.drawString(originalText);
contentStream.endText();
contentStream.close();
template.sendBodyAndHeader("direct:start", textToAppend, PdfHeaderConstants.PDF_DOCUMENT_HEADER_NAME, document);
resultEndpoint.setExpectedMessageCount(1);
resultEndpoint.expectedMessagesMatches(new Predicate() {
@Override
public boolean matches(Exchange exchange) {
Object body = exchange.getIn().getBody();
assertThat(body, instanceOf(ByteArrayOutputStream.class));
try {
PDDocument doc = PDDocument.load(new ByteArrayInputStream(((ByteArrayOutputStream) body).toByteArray()));
PDFTextStripper pdfTextStripper = new PDFTextStripper();
String text = pdfTextStripper.getText(doc);
assertEquals(2, doc.getNumberOfPages());
assertThat(text, containsString(originalText));
assertThat(text, containsString(textToAppend));
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}
});
resultEndpoint.assertIsSatisfied();
}
use of org.apache.pdfbox.pdmodel.PDDocument in project camel by apache.
the class PdfAppendTest method testAppendEncrypted.
@Test
public void testAppendEncrypted() throws Exception {
final String originalText = "Test";
final String textToAppend = "Append";
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.setFont(PDType1Font.HELVETICA, 12);
contentStream.beginText();
contentStream.moveTextPositionByAmount(20, 400);
contentStream.drawString(originalText);
contentStream.endText();
contentStream.close();
final String ownerPass = "ownerPass";
final String userPass = "userPass";
AccessPermission accessPermission = new AccessPermission();
accessPermission.setCanExtractContent(false);
StandardProtectionPolicy protectionPolicy = new StandardProtectionPolicy(ownerPass, userPass, accessPermission);
protectionPolicy.setEncryptionKeyLength(128);
document.protect(protectionPolicy);
ByteArrayOutputStream output = new ByteArrayOutputStream();
document.save(output);
// Encryption happens after saving.
PDDocument encryptedDocument = PDDocument.load(new ByteArrayInputStream(output.toByteArray()));
Map<String, Object> headers = new HashMap<String, Object>();
headers.put(PdfHeaderConstants.PDF_DOCUMENT_HEADER_NAME, encryptedDocument);
headers.put(PdfHeaderConstants.DECRYPTION_MATERIAL_HEADER_NAME, new StandardDecryptionMaterial(userPass));
template.sendBodyAndHeaders("direct:start", textToAppend, headers);
resultEndpoint.setExpectedMessageCount(1);
resultEndpoint.expectedMessagesMatches(new Predicate() {
@Override
public boolean matches(Exchange exchange) {
Object body = exchange.getIn().getBody();
assertThat(body, instanceOf(ByteArrayOutputStream.class));
try {
PDDocument doc = PDDocument.load(new ByteArrayInputStream(((ByteArrayOutputStream) body).toByteArray()));
PDFTextStripper pdfTextStripper = new PDFTextStripper();
String text = pdfTextStripper.getText(doc);
assertEquals(2, doc.getNumberOfPages());
assertThat(text, containsString(originalText));
assertThat(text, containsString(textToAppend));
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}
});
resultEndpoint.assertIsSatisfied();
}
Aggregations