本文整理匯總了Java中org.apache.pdfbox.pdmodel.PDDocument.close方法的典型用法代碼示例。如果您正苦於以下問題:Java PDDocument.close方法的具體用法?Java PDDocument.close怎麽用?Java PDDocument.close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.pdfbox.pdmodel.PDDocument
的用法示例。
在下文中一共展示了PDDocument.close方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createAlternateRowsDocument
import org.apache.pdfbox.pdmodel.PDDocument; //導入方法依賴的package包/類
@Test
public void createAlternateRowsDocument() throws Exception {
final PDDocument document = new PDDocument();
final PDPage page = new PDPage(PDRectangle.A4);
page.setRotation(90);
document.addPage(page);
final PDPageContentStream contentStream = new PDPageContentStream(document, page);
// TODO replace deprecated method call
contentStream.concatenate2CTM(0, 1, -1, 0, page.getMediaBox().getWidth(), 0);
final float startY = page.getMediaBox().getWidth() - 30;
(new TableDrawer(contentStream, createAndGetTableWithAlternatingColors(), 30, startY)).draw();
contentStream.close();
document.save("target/alternateRows.pdf");
document.close();
}
示例2: createRingManagerDocument
import org.apache.pdfbox.pdmodel.PDDocument; //導入方法依賴的package包/類
@Test
public void createRingManagerDocument() throws Exception {
final PDDocument document = new PDDocument();
final PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
final float startY = page.getMediaBox().getHeight() - 150;
final int startX = 56;
final PDPageContentStream contentStream = new PDPageContentStream(document, page);
Table table = getRingManagerTable();
(new TableDrawer(contentStream, table, startX, startY)).draw();
contentStream.setFont(PDType1Font.HELVETICA, 8.0f);
contentStream.beginText();
contentStream.newLineAtOffset(startX, startY - (table.getHeight() + 22));
contentStream.showText("Dieser Kampf muss der WB nicht entsprechen, da als Sparringskampf angesetzt.");
contentStream.endText();
contentStream.close();
document.save("target/ringmanager.pdf");
document.close();
}
示例3: process
import org.apache.pdfbox.pdmodel.PDDocument; //導入方法依賴的package包/類
private boolean process() throws IOException {
boolean toReturn = false;
PDFTextStripper stripper = new TitleExtractor();
PDDocument document = null;
try {
document = PDDocument.load(new File(this.getFileNamePathWithExtension()));
//((TitleExtractor) stripper).setFileNamePathWithExtension(this.getFileNamePathWithExtension());
stripper.setSortByPosition(true);
stripper.setStartPage(0);
stripper.setEndPage(1);
Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
stripper.writeText(document, dummy);
setTitle(((TitleExtractor) stripper).getTitle());
toReturn = true;
} finally {
if (document != null) {
document.close();
}
}
return toReturn;
}
示例4: transformTextAndCheck
import org.apache.pdfbox.pdmodel.PDDocument; //導入方法依賴的package包/類
private void transformTextAndCheck(String text, String encoding, String checkText)
throws IOException
{
// Get a reader for the text
ContentReader reader = buildContentReader(text, Charset.forName(encoding));
// And a temp writer
File out = TempFileProvider.createTempFile("AlfrescoTest_", ".pdf");
ContentWriter writer = new FileContentWriter(out);
writer.setMimetype("application/pdf");
// Transform to PDF
transformer.transform(reader, writer);
// Read back in the PDF and check it
PDDocument doc = PDDocument.load(out);
PDFTextStripper textStripper = new PDFTextStripper();
StringWriter textWriter = new StringWriter();
textStripper.writeText(doc, textWriter);
doc.close();
String roundTrip = clean(textWriter.toString());
assertEquals(
"Incorrect text in PDF when starting from text in " + encoding,
checkText, roundTrip
);
}
示例5: testMultiPageJFreeChart
import org.apache.pdfbox.pdmodel.PDDocument; //導入方法依賴的package包/類
@Test
public void testMultiPageJFreeChart() throws IOException {
File parentDir = new File("target/test/multipage");
// noinspection ResultOfMethodCallIgnored
parentDir.mkdirs();
File targetPDF = new File(parentDir, "multipage.pdf");
PDDocument document = new PDDocument();
for (int i = 0; i < 4; i++) {
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(document, 800, 400);
drawOnGraphics(pdfBoxGraphics2D, i);
pdfBoxGraphics2D.dispose();
PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
Matrix matrix = new Matrix();
matrix.translate(0, 30);
matrix.scale(0.7f, 1f);
contentStream.saveGraphicsState();
contentStream.transform(matrix);
contentStream.drawForm(appearanceStream);
contentStream.restoreGraphicsState();
contentStream.close();
}
document.save(targetPDF);
document.close();
}
示例6: createSampleDocument
import org.apache.pdfbox.pdmodel.PDDocument; //導入方法依賴的package包/類
@Test
public void createSampleDocument() throws Exception {
// Define the table structure first
TableBuilder tableBuilder = new TableBuilder()
.addColumnOfWidth(300)
.addColumnOfWidth(120)
.addColumnOfWidth(70)
.setFontSize(8)
.setFont(PDType1Font.HELVETICA);
// Header ...
tableBuilder.addRow(new RowBuilder()
.add(Cell.withText("This is right aligned without a border").setHorizontalAlignment(RIGHT))
.add(Cell.withText("And this is another cell"))
.add(Cell.withText("Sum").setBackgroundColor(Color.ORANGE))
.setBackgroundColor(Color.BLUE)
.build());
// ... and some cells
for (int i = 0; i < 10; i++) {
tableBuilder.addRow(new RowBuilder()
.add(Cell.withText(i).withAllBorders())
.add(Cell.withText(i * i).withAllBorders())
.add(Cell.withText(i + (i * i)).withAllBorders())
.setBackgroundColor(i % 2 == 0 ? Color.LIGHT_GRAY : Color.WHITE)
.build());
}
final PDDocument document = new PDDocument();
final PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
final PDPageContentStream contentStream = new PDPageContentStream(document, page);
// Define the starting point
final float startY = page.getMediaBox().getHeight() - 50;
final int startX = 50;
// Draw!
(new TableDrawer(contentStream, tableBuilder.build(), startX, startY)).draw();
contentStream.close();
document.save("target/sampleWithColorsAndBorders.pdf");
document.close();
}
示例7: printPDFFile
import org.apache.pdfbox.pdmodel.PDDocument; //導入方法依賴的package包/類
@FXML
public void printPDFFile(ActionEvent event) throws IOException {
try {
String fileName = "PDFoutput.pdf";
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDPageContentStream content = new PDPageContentStream(doc, page);
content.beginText();
content.setFont(PDType1Font.TIMES_ROMAN, 26);
content.moveTextPositionByAmount(220, 750);
content.drawString("Titel");
content.endText();
content.beginText();
content.setFont(PDType1Font.TIMES_ROMAN, 16);
content.moveTextPositionByAmount(80, 700);
content.drawString("Inhoud");
content.endText();
content.close();
doc.save(fileName);
doc.close();
System.out.println("your file was saved in: " + System.getProperty("user.dir"));
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
示例8: findPhoto
import org.apache.pdfbox.pdmodel.PDDocument; //導入方法依賴的package包/類
public static void findPhoto(String path,int empId) throws IOException, SQLException, Error{
// Loading an existing document
int imageFound=0;
File file = new File(path);
PDDocument document=PDDocument.load(file);
PDPageTree list=document.getPages();
for(PDPage page:list){ //check in all pages of pdf
PDResources pdResources=page.getResources(); //get all resources
for(COSName cosName:pdResources.getXObjectNames()) //loop for all resources
{
PDXObject pdxObject=pdResources.getXObject(cosName);
if (pdxObject instanceof PDImageXObject) { //check that the resource is image
BufferedImage br=((PDImageXObject) pdxObject).getImage();
RgbImage im = RgbImageJ2se.toRgbImage(br);
// step #3 - convert image to greyscale 8-bits
RgbAvgGray toGray = new RgbAvgGray();
toGray.push(im);
// step #4 - initialize face detector with correct Haar profile
InputStream is = ExtractPhoto.class.getResourceAsStream("/haar/HCSB.txt");
Gray8DetectHaarMultiScale detectHaar = new Gray8DetectHaarMultiScale(is, 1,40);
// step #5 - apply face detector to grayscale image
List<Rect> result= detectHaar.pushAndReturn(toGray.getFront());
if(result.size()!=0)
{
database.StorePhoto.storePhoto(empId,br);
imageFound=1;
break;
}
}
}
if(imageFound==1)
break;
}
System.out.println(imageFound);
if(imageFound!=1){
BufferedImage in = ImageIO.read(ExtractPhoto.class.getResource("/images/nopic.jpg"));
database.StorePhoto.storePhoto(empId,in);
}
document.close();
}
示例9: saveDocument
import org.apache.pdfbox.pdmodel.PDDocument; //導入方法依賴的package包/類
@Override
public void saveDocument(List<Page> pages, File file) throws IOException {
if (pages.size() == 0) {
throw new IOException("Empty document.");
}
PDDocument outDoc = new PDDocument();
Map<File,PDDocument> docs = new HashMap<File,PDDocument>();
try {
for (Page page : pages) {
PDDocument pageDoc = docs.get(page.getFile());
if (pageDoc == null) {
pageDoc = PDDocument.load(page.getFile());
docs.put(page.getFile(), pageDoc);
}
outDoc.addPage((PDPage)pageDoc.getPrintable(page.getIndex()));
}
try {
outDoc.save(file.toString());
}
catch (COSVisitorException e) {
throw new IOException(e);
}
}
finally {
outDoc.close();
for (PDDocument doc : docs.values()) {
doc.close();
}
}
}
示例10: process
import org.apache.pdfbox.pdmodel.PDDocument; //導入方法依賴的package包/類
public void process(File pdf, File output){
PDDocument pdDoc;
try {//Kudos for closing: http://stackoverflow.com/questions/156508/closing-a-java-fileinputstream
File tmpfile = File.createTempFile(String.format("txttmp-%s", UUID.randomUUID().toString()), null);
RandomAccessFile raf = new RandomAccessFile(tmpfile, "rw");
pdDoc = PDDocument.loadNonSeq(pdf, raf);
FileWriter writer = new FileWriter(output);
try {
PDFTextStripper stripper = new PDFTextStripper();
int numberOfPages = pdDoc.getNumberOfPages();
for (int j = 1; j < numberOfPages+1; j++) {
stripper.setStartPage(j);
stripper.setEndPage(j);
writer.write(stripper.getText(pdDoc));
writer.flush();
}
} finally {
pdDoc.close();
raf.close();
tmpfile.delete();
writer.close();
}
} catch (IOException ioe) {
// log.warn(String.format("Failed to create txt for file: %s", pdf.getName()), ioe);
}
}
示例11: signPdf
import org.apache.pdfbox.pdmodel.PDDocument; //導入方法依賴的package包/類
/** Signe un document PDF
* @param inStream
* @param name
* @param location
* @param reason
* @param contactInfo
* @return l'inputStream
* @throws IOException
*/
public InputStream signPdf(ByteArrayInOutStream inStream, String name, String location, String reason, String contactInfo) throws IOException {
if (inStream == null) {
throw new FileNotFoundException("Document for signing does not exist");
}
// sign
PDDocument doc = PDDocument.load(inStream.getInputStream());
// create signature dictionary
PDSignature signature = new PDSignature();
signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
signature.setName(name);
signature.setLocation(location);
signature.setContactInfo(contactInfo);
signature.setReason(reason);
// the signing date, needed for valid signature
signature.setSignDate(Calendar.getInstance());
// register signature dictionary and sign interface
doc.addSignature(signature, this);
// write incremental (only for signing purpose)
ByteArrayInOutStream outStream = new ByteArrayInOutStream();
doc.saveIncremental(outStream);
doc.close();
inStream.close();
return outStream.getInputStream();
}
示例12: extractPdfText
import org.apache.pdfbox.pdmodel.PDDocument; //導入方法依賴的package包/類
/**
* Extracts all the Text inside a Pdf
*/
private static String extractPdfText(byte[] pdfData) throws IOException {
PDDocument pdfDocument = PDDocument.load(new ByteArrayInputStream(pdfData));
try {
return new PDFTextStripper().getText(pdfDocument);
} finally {
pdfDocument.close();
}
}
開發者ID:jonashackt,項目名稱:cxf-spring-cloud-netflix-docker,代碼行數:12,代碼來源:WeatherBackendApplicationTests.java
示例13: readPdf
import org.apache.pdfbox.pdmodel.PDDocument; //導入方法依賴的package包/類
private static String readPdf(InputStream is) throws Exception {
String result;
PDDocument doc = PDDocument.load(is);
PDFTextStripper stripper = new PDFTextStripper();
result = stripper.getText(doc);
if(doc!= null) {
doc.close();
}
if (is != null) {
is.close();
}
return result;
}
示例14: process
import org.apache.pdfbox.pdmodel.PDDocument; //導入方法依賴的package包/類
private boolean process() throws IOException {
boolean toReturn = false;
PDFTextStripper stripper = new AuthorExtractor();
PDDocument document = null;
try {
document = PDDocument.load(new File(this.getFileNamePathWithExtension()));
//((TitleExtractor) stripper).setFileNamePathWithExtension(this.getFileNamePathWithExtension());
stripper.setSortByPosition(true);
stripper.setStartPage(0);
stripper.setEndPage(1);
Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
stripper.writeText(document, dummy);
setAuthorNames(((AuthorExtractor) stripper).getAuthorNames());
setAuthorAffiliations(((AuthorExtractor) stripper).getAuthorAffiliations());
setAuthorContacts(((AuthorExtractor) stripper).getAuthorContacts());
toReturn = true;
} finally {
if (document != null) {
document.close();
}
}
return toReturn;
}
示例15: exportGraphic
import org.apache.pdfbox.pdmodel.PDDocument; //導入方法依賴的package包/類
@SuppressWarnings("SpellCheckingInspection")
void exportGraphic(String dir, String name, GraphicsExporter exporter) {
try {
PDDocument document = new PDDocument();
PDFont pdArial = PDFontFactory.createDefaultFont();
File parentDir = new File("target/test/" + dir);
// noinspection ResultOfMethodCallIgnored
parentDir.mkdirs();
BufferedImage image = new BufferedImage(400, 400, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D imageGraphics = image.createGraphics();
exporter.draw(imageGraphics);
imageGraphics.dispose();
ImageIO.write(image, "PNG", new File(parentDir, name + ".png"));
for (Mode m : Mode.values()) {
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(document, 400, 400);
PdfBoxGraphics2DFontTextDrawer fontTextDrawer = null;
contentStream.beginText();
contentStream.setStrokingColor(0, 0, 0);
contentStream.setNonStrokingColor(0, 0, 0);
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 15);
contentStream.setTextMatrix(Matrix.getTranslateInstance(10, 800));
contentStream.showText("Mode " + m);
contentStream.endText();
switch (m) {
case FontTextIfPossible:
fontTextDrawer = new PdfBoxGraphics2DFontTextDrawer();
fontTextDrawer.registerFont(
new File("src/test/resources/de/rototor/pdfbox/graphics2d/DejaVuSerifCondensed.ttf"));
break;
case DefaultFontText: {
fontTextDrawer = new PdfBoxGraphics2DFontTextDrawerDefaultFonts();
fontTextDrawer.registerFont(
new File("src/test/resources/de/rototor/pdfbox/graphics2d/DejaVuSerifCondensed.ttf"));
break;
}
case ForceFontText:
fontTextDrawer = new PdfBoxGraphics2DFontTextForcedDrawer();
fontTextDrawer.registerFont(
PdfBoxGraphics2DTestBase.class.getResourceAsStream("DejaVuSerifCondensed.ttf"));
fontTextDrawer.registerFont("Arial", pdArial);
break;
case DefaultVectorized:
default:
break;
}
if (fontTextDrawer != null) {
pdfBoxGraphics2D.setFontTextDrawer(fontTextDrawer);
}
exporter.draw(pdfBoxGraphics2D);
pdfBoxGraphics2D.dispose();
PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
Matrix matrix = new Matrix();
matrix.translate(0, 20);
contentStream.transform(matrix);
contentStream.drawForm(appearanceStream);
matrix.scale(1.5f, 1.5f);
matrix.translate(0, 100);
contentStream.transform(matrix);
contentStream.drawForm(appearanceStream);
contentStream.close();
}
document.save(new File(parentDir, name + ".pdf"));
document.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}