Java Code Examples for org.apache.pdfbox.pdmodel.PDDocument

The following are top voted examples for showing how to use org.apache.pdfbox.pdmodel.PDDocument. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: easytable   File: TableDrawerIntegrationTest.java   Source Code and License 12 votes vote down vote up
@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();
}
 
Example 2
Project: easytable   File: TableDrawerIntegrationTest.java   Source Code and License 11 votes vote down vote up
@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();
}
 
Example 3
Project: 484_P7_1-Java   File: NurminenDetectionAlgorithm.java   Source Code and License 10 votes vote down vote up
private PDDocument removeText(PDPage page) throws IOException {

        PDFStreamParser parser = new PDFStreamParser(page);
        parser.parse();
        List<Object> tokens = parser.getTokens();
        List<Object> newTokens = new ArrayList<>();
        for (Object token : tokens) {
            if (token instanceof Operator) {
                Operator op = (Operator) token;
                if (op.getName().equals("TJ") || op.getName().equals("Tj")) {
                    //remove the one argument to this operator
                    newTokens.remove(newTokens.size() - 1);
                    continue;
                }
            }
            newTokens.add(token);
        }

        PDDocument document = new PDDocument();
        document.addPage(page);

        PDStream newContents = new PDStream(document);
        OutputStream out = newContents.createOutputStream(COSName.FLATE_DECODE);
        ContentStreamWriter writer = new ContentStreamWriter(out);
        writer.writeTokens(newTokens);
        out.close();
        page.setContents(newContents);

        return document;

    }
 
Example 4
Project: PDF2RDF   File: TitleExtractor.java   Source Code and License 9 votes vote down vote up
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;
}
 
Example 5
Project: Lernkartei_2017   File: Printer.java   Source Code and License 8 votes vote down vote up
public static void printDocument() throws IOException, PrinterException
{	
	
	PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();

    pras.add(Sides.TWO_SIDED_SHORT_EDGE);
	PDDocument input = PDDocument.load(new File("Karteikarten.pdf"));
	
	PrinterJob job = PrinterJob.getPrinterJob();
	job.setPageable(new PDFPageable(input));
	if (job.printDialog(pras)) {
	    job.print(pras);
	}
	
}
 
Example 6
Project: alfresco-repository   File: TextToPdfContentTransformerTest.java   Source Code and License 8 votes vote down vote up
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
    );
}
 
Example 7
Project: easytable   File: TableDrawerIntegrationTest.java   Source Code and License 8 votes vote down vote up
@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();
}
 
Example 8
Project: BoxTable   File: Table.java   Source Code and License 8 votes vote down vote up
/**
 * Renders this table to a document
 * 
 * @param document
 *            The document this table will be rendered to
 * @param width
 *            The width of the table
 * @param left
 *            The left edge of the table
 * @param top
 *            The top edge of the table
 * @param paddingTop
 *            The amount of free space at the top of a new page (if a page break is necessary)
 * @param paddingBottom
 *            The minimal amount of free space at the bottom of the page before inserting a page break
 * @return The bottom edge of the last rendered table part
 * @throws IOException
 *             If writing to the document fails
 */
@SuppressWarnings("resource")
public float render(final PDDocument document, final float width, final float left, float top, final float paddingTop, final float paddingBottom)
		throws IOException {
	float yPos = top;
	final PDPage page = document.getPage(document.getNumberOfPages() - 1);
	final PDRectangle pageSize = page.getMediaBox();
	PDPageContentStream stream = new PDPageContentStream(document, page, AppendMode.APPEND, true);
	float height = getHeight(width);
	if (height > pageSize.getHeight() - paddingTop - paddingBottom) {
		final float[] colWidths = getColumnWidths(width);
		for (int i = 0; i < rows.size(); ++i) {
			if (rows.get(i).getHeight(colWidths) > yPos - paddingBottom) {
				drawBorder(stream, left, top, width, top - yPos);
				stream = newPage(document, stream);
				top = pageSize.getHeight() - paddingTop;
				yPos = top;
				yPos = renderRows(document, stream, 0, getNumHeaderRows(), width, left, yPos);
				i = Math.max(i, getNumHeaderRows());
			}
			yPos = renderRows(document, stream, i, i + 1, width, left, yPos);
		}
		drawBorder(stream, left, top, width, top - yPos);

		handleEvent(EventType.AFTER_TABLE, document, stream, left, top, width, top - yPos);
	} else {
		if (height > top - paddingBottom) {
			stream = newPage(document, stream);
			top = pageSize.getHeight() - paddingTop;
			yPos = top;
		}
		yPos = renderRows(document, stream, 0, -1, width, left, yPos);
		drawBorder(stream, left, top, width, top - yPos);
		handleEvent(EventType.AFTER_TABLE, document, stream, left, top, width, top - yPos);
	}
	stream.close();

	return yPos;
}
 
Example 9
Project: pdfbox-graphics2d   File: MultiPageTest.java   Source Code and License 8 votes vote down vote up
@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();
}
 
Example 10
Project: ctsms   File: PDFPainterBase.java   Source Code and License 7 votes vote down vote up
public void setMetadata(PDDocument doc) throws Exception {
	PDDocumentCatalog catalog = doc.getDocumentCatalog();
	PDDocumentInformation info = doc.getDocumentInformation();
	GregorianCalendar cal = new GregorianCalendar();
	cal.setTime(now);
	XMPMetadata metadata = new XMPMetadata();
	XMPSchemaPDF pdfSchema = metadata.addPDFSchema();
	pdfSchema.setKeywords(info.getKeywords());
	pdfSchema.setProducer(info.getProducer());
	XMPSchemaBasic basicSchema = metadata.addBasicSchema();
	basicSchema.setModifyDate(cal);
	basicSchema.setCreateDate(cal);
	basicSchema.setCreatorTool(info.getCreator());
	basicSchema.setMetadataDate(cal);
	XMPSchemaDublinCore dcSchema = metadata.addDublinCoreSchema();
	dcSchema.setTitle(info.getTitle());
	dcSchema.addCreator("PDFBox");
	dcSchema.setDescription(info.getSubject());
	PDMetadata metadataStream = new PDMetadata(doc);
	metadataStream.importXMPMetadata(metadata);
	catalog.setMetadata(metadataStream);
}
 
Example 11
Project: PrintDF   File: BookletMaker.java   Source Code and License 7 votes vote down vote up
/**
 * Creates a pdf page from two pages from another 'original' pdf document
 * @param doc original pdf from which the pages will be taken
 * @param leftPage page number of the page to go on the left side 
 * @param rightPage page number of the page to go on the right side
 * @return generated page containing the left and right pages from the original document side-by-side.
 */
private static PDPage createBookPage(PDDocument doc, int leftPage, int rightPage) {
	// double the width of a normal page to create the booklet
	PDRectangle baseSize = doc.getPage(0).getMediaBox();		
	PDRectangle box = new PDRectangle(baseSize.getWidth()*2, baseSize.getHeight());
	

	if(sizeOverride != null) {
		box = sizeOverride.asPDRectangle();
	}
	
	PDPage page = new PDPage(box);
	
	try {
		PDImageXObject leftImg = PrintDF.pageToImage(doc, leftPage, scale);
		PDImageXObject rightImg = PrintDF.pageToImage(doc, rightPage, scale);
		
		
		
		PDPageContentStream contentStream = new PDPageContentStream(doc, page);
		if(leftImg != null)
			contentStream.drawImage(leftImg, 0, 0, box.getWidth()/2, box.getHeight());
		if(rightImg != null)
			contentStream.drawImage(rightImg, box.getWidth()/2, 0, box.getWidth()/2, box.getHeight());
		contentStream.close();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	return page;
}
 
Example 12
Project: IDBuilderFX   File: ExtractPhoto.java   Source Code and License 7 votes vote down vote up
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();	
	}
 
Example 13
Project: Lernkartei_2017   File: Printer.java   Source Code and License 6 votes vote down vote up
static void createPage(PDDocument temp,int count)
{
	for(int i = 0; i < count;i++)
	{
		PDPage page = new PDPage(new PDRectangle(297 * POINTS_PER_MM, 210 * POINTS_PER_MM));
		temp.addPage(page);
	}
}
 
Example 14
Project: pdfjumbler   File: PdfEditor.java   Source Code and License 6 votes vote down vote up
@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();
		}
	}
}
 
Example 15
Project: Java-for-Data-Science   File: PDFExtractionExample.java   Source Code and License 6 votes vote down vote up
public static void main(String[] args) {
    try {
        PDDocument document = PDDocument.load(new File("PDF File.pdf"));
            PDFTextStripper Tstripper = new PDFTextStripper();
            String documentText = Tstripper.getText(document);
            System.out.println(documentText);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 16
Project: pdf-table   File: PdfTableReader.java   Source Code and License 6 votes vote down vote up
/**
 * Saves debug images of PDF pages from specified range and saves them in specified directory.
 *
 * @param document  PDF document instance
 * @param startPage first page in range to process (first page == 1)
 * @param endPage   last page in range
 * @param outputDir destination directory
 * @throws IOException
 */
public void savePdfTablePagesDebugImages(PDDocument document, int startPage, int endPage, Path outputDir) throws IOException {
    TableExtractor debugExtractor = new TableExtractor(settings);
    PDFRenderer renderer = new PDFRenderer(document);
    for (int page = startPage - 1; page < endPage; ++page) {
        PdfTableSettings debugSettings = PdfTableSettings.getBuilder()
                .setDebugImages(true)
                .setDebugFileOutputDir(outputDir)
                .setDebugFilename("page_" + (page + 1))
                .build();
        debugExtractor.setSettings(debugSettings);
        BufferedImage bi;
        synchronized (this) {
            bi = renderer.renderImageWithDPI(page, settings.getPdfRenderingDpi(), ImageType.RGB);
        }
        debugExtractor.getTableBoundingRectangles(bufferedImage2GrayscaleMat(bi));
    }
}
 
Example 17
Project: pdf-table   File: PdfTableReaderTest.java   Source Code and License 6 votes vote down vote up
@SuppressWarnings("ConstantConditions")
private PDDocument getTestPDF() {
    try {
        ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(classLoader.getResource(TEST_FILENAME).getFile());
        return PDDocument.load(file);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e.getCause());
    }
}
 
Example 18
Project: pdf-converter   File: TxtCreator.java   Source Code and License 6 votes vote down vote up
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);
        }
    }
 
Example 19
Project: campingsimulator2017   File: ReservationsController.java   Source Code and License 6 votes vote down vote up
/**
 * @param facture
 * @throws IOException
 * export the bill as pdf
 */
public void exportFacturePDF(PDDocument facture) throws IOException {
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Exporter la facture");
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Portable Document Format", "*.pdf"));
    File selectedFile = fileChooser.showSaveDialog(homeController.getView().getWindow());

    facture.save(selectedFile);
}
 
Example 20
Project: esup-ecandidat   File: CreateSignaturePdf.java   Source Code and License 6 votes vote down vote up
/** 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();
}
 
Example 21
Project: PDF2RDF   File: AuthorExtractor.java   Source Code and License 6 votes vote down vote up
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;
}
 
Example 22
Project: pdfbox-graphics2d   File: PdfBoxGraphics2D.java   Source Code and License 6 votes vote down vote up
/**
 * @param document
 *            The document the graphics should be used to create a XForm in.
 * @param bbox
 *            Bounding Box of the graphics
 * @throws IOException
 *             when something goes wrong with writing into the content
 *             stream of the {@link PDDocument}.
 */
public PdfBoxGraphics2D(PDDocument document, PDRectangle bbox) throws IOException {
	this.document = document;
	this.bbox = bbox;

	PDAppearanceStream appearance = new PDAppearanceStream(document);
	xFormObject = appearance;
	xFormObject.setResources(new PDResources());
	xFormObject.setBBox(bbox);
	contentStream = new PDPageContentStream(document, appearance, xFormObject.getStream().createOutputStream(COSName.FLATE_DECODE));
	contentStreamSaveState();

	baseTransform = new AffineTransform();
	baseTransform.translate(0, bbox.getHeight());
	baseTransform.scale(1, -1);

	calcImage = new BufferedImage(100, 100, BufferedImage.TYPE_4BYTE_ABGR);
	calcGfx = calcImage.createGraphics();
	font = calcGfx.getFont();
	cloneInfo = null;
}
 
Example 23
Project: pdfbox   File: App.java   Source Code and License 6 votes vote down vote up
public static void extractAttachments(String pdfPath, String extractPath) throws IOException {

  PDDocument document = null;

  try {

    File input = new File(pdfPath);

    String filePath = input.getParent() + System.getProperty("file.separator");

    document = PDDocument.load(input);

    PDDocumentNameDictionary namesDictionary =
    new PDDocumentNameDictionary( document.getDocumentCatalog() );
    PDEmbeddedFilesNameTreeNode efTree = namesDictionary.getEmbeddedFiles();

    if (efTree != null) {

      Map<String, PDComplexFileSpecification> names = efTree.getNames();

      if (names != null) {
        extractFiles(names, filePath);
      } else {

        List<PDNameTreeNode<PDComplexFileSpecification>> kids = efTree.getKids();
        for (PDNameTreeNode<PDComplexFileSpecification> node : kids) {
          names = node.getNames();
          extractFiles(names, filePath);
        };

      };

    };

    for (PDPage page : document.getPages()) {
      for (PDAnnotation annotation : page.getAnnotations()) {
        if (annotation instanceof PDAnnotationFileAttachment) {
          PDAnnotationFileAttachment annotationFileAttachment = (PDAnnotationFileAttachment) annotation;
          PDComplexFileSpecification fileSpec = (PDComplexFileSpecification) annotationFileAttachment.getFile();
          PDEmbeddedFile embeddedFile = getEmbeddedFile(fileSpec);
          extractFile(filePath, fileSpec.getFilename(), embeddedFile);
        };
      };
    };

  } finally {

  };

}
 
Example 24
Project: ctsms   File: CVPDFPainter.java   Source Code and License 6 votes vote down vote up
@Override
public void loadImages(PDDocument doc) {
	if (imageVOMap != null && Settings.getBoolean(CVPDFSettingCodes.SHOW_IMAGES, Bundle.CV_PDF, CVPDFDefaultSettings.SHOW_IMAGES)) {
		Float width = Settings.getFloatNullable(CVPDFSettingCodes.IMAGE_WIDTH, Bundle.CV_PDF, CVPDFDefaultSettings.IMAGE_WIDTH);
		Float height = Settings.getFloatNullable(CVPDFSettingCodes.IMAGE_HEIGHT, Bundle.CV_PDF, CVPDFDefaultSettings.IMAGE_HEIGHT);
		int quality = Settings.getInt(CVPDFSettingCodes.IMAGE_QUALITY, Bundle.CV_PDF, CVPDFDefaultSettings.IMAGE_QUALITY);
		int dpi = Settings.getInt(CVPDFSettingCodes.IMAGE_DPI, Bundle.CV_PDF, CVPDFDefaultSettings.IMAGE_DPI);
		Iterator<StaffImageOutVO> it = imageVOMap.values().iterator();
		while (it.hasNext()) {
			StaffImageOutVO staffImage = it.next();
			if (staffImage.getHasImage() && staffImage.getShowCv()) {
				images.put(staffImage.getId(),
						PDFJpeg.prepareScaledImage(doc, staffImage.getDatas(), width == null ? 0 : width, height == null ? 0 : height, quality, dpi, null));
			}
		}
	}
}
 
Example 25
Project: PrintDF   File: PrintDF.java   Source Code and License 6 votes vote down vote up
public static void createBooklet(File f, BookletSettings setts) {
	try {
		PDDocument document = PDDocument.load(f);
		
		if (document.isEncrypted()) {
			System.err.println("Error: Encrypted documents are not supported!");
			System.exit(1);
		}
		
		PageSize size = setts.getSize().getPageSize();
		size.setOrientation(Orientation.LANDSCAPE);
		
		BookletMaker.setSizeOverride(size);			
		BookletMaker.setScale(setts.getScale());
		
		PDDocument booklet = BookletMaker.make(document);
		
		if(setts.isRotateEvens())
			BookletRotater.flip(booklet, PageSelector.pagesEven(booklet));
		
		String name = setts.getDirectory(f);
		System.out.println(name);

		booklet.save(name);
		document.close();
	}
	catch(IOException e) {
		e.printStackTrace();
	}
}
 
Example 26
Project: git-rekt   File: BillPdfGenerator.java   Source Code and License 6 votes vote down vote up
public PDDocument getBillAsPdf() throws IOException {
    // Add blank page to document
    PDPage firstPage = new PDPage();
    document.addPage(firstPage);
    PDPageContentStream contentStream = new PDPageContentStream(document, firstPage);
    generateBillHeader(firstPage, contentStream);
    generateBillInfoText(firstPage, contentStream);
    contentStream.beginText();
    contentStream.newLineAtOffset(60f, 640f);

    // Add the column line item header line
    contentStream.setFont(PDType1Font.COURIER_BOLD, NORMAL_FONT_SIZE);
    contentStream.newLine();
    contentStream.showText(lineItemHeader);

    // Add bill line items
    contentStream.setFont(NORMAL_FONT, NORMAL_FONT_SIZE);
    for(String lineItem : lineItems) {
        contentStream.newLine();
        contentStream.showText(lineItem);
    }

    // Display the bill total at the end
    contentStream.newLine();
    String billTotalLine = getBillTotalLine();
    contentStream.setFont(PDType1Font.COURIER_BOLD, NORMAL_FONT_SIZE);
    contentStream.newLine();
    contentStream.showText(billTotalLine);

    // Display important bill information
    contentStream.newLine();
    contentStream.newLine();
    contentStream.showText("WARNING: Failure to pay bill may result in broken legs.");

    contentStream.endText();
    contentStream.close();

    return document;
}
 
Example 27
Project: 484_P7_1-Java   File: TestObjectExtractor.java   Source Code and License 6 votes vote down vote up
@Test
public void testCanReadPDFWithOwnerEncryption() throws IOException {
    PDDocument pdf_document = PDDocument.load(new File("src/test/resources/technology/tabula/S2MNCEbirdisland.pdf"));
    ObjectExtractor oe = new ObjectExtractor(pdf_document);
    PageIterator pi = oe.extract();
    int i = 0;
    while (pi.hasNext()) {
        i++;
        pi.next();
    }
    assertEquals(2, i);
}
 
Example 28
Project: 484_P7_1-Java   File: TestObjectExtractor.java   Source Code and License 6 votes vote down vote up
@Test
public void testTextExtractionDoesNotRaise() throws IOException {
    PDDocument pdf_document = PDDocument.load(new File("src/test/resources/technology/tabula/rotated_page.pdf"));
    ObjectExtractor oe = new ObjectExtractor(pdf_document);
    PageIterator pi = oe.extract();

    assertTrue(pi.hasNext());
    assertNotNull(pi.next());
    assertFalse(pi.hasNext());

}
 
Example 29
Project: 484_P7_1-Java   File: TestObjectExtractor.java   Source Code and License 6 votes vote down vote up
@Test
public void testShouldDetectRulings() throws IOException {
    PDDocument pdf_document = PDDocument.load(new File("src/test/resources/technology/tabula/should_detect_rulings.pdf"));
    ObjectExtractor oe = new ObjectExtractor(pdf_document);
    PageIterator pi = oe.extract();

    Page page = pi.next();
    List<Ruling> rulings = page.getRulings();

    for (Ruling r: rulings) {
        assertTrue(page.contains(r.getBounds()));
    }
}
 
Example 30
Project: 484_P7_1-Java   File: TestObjectExtractor.java   Source Code and License 6 votes vote down vote up
@Test
public void testExtractOnePage() throws IOException {
    PDDocument pdf_document = PDDocument.load(new File("src/test/resources/technology/tabula/S2MNCEbirdisland.pdf"));
    assertEquals(2, pdf_document.getNumberOfPages());

    ObjectExtractor oe = new ObjectExtractor(pdf_document);
    Page page = oe.extract(2);

    assertNotNull(page);

}
 
Example 31
Project: 484_P7_1-Java   File: TestObjectExtractor.java   Source Code and License 6 votes vote down vote up
@Test
public void testTextElementsContainedInPage() throws IOException {
    PDDocument pdf_document = PDDocument.load(new File("src/test/resources/technology/tabula/cs-en-us-pbms.pdf"));
    ObjectExtractor oe = new ObjectExtractor(pdf_document);

    Page page = oe.extractPage(1);

    for (TextElement te: page.getText()) {
        assertTrue(page.contains(te));
    }
}
 
Example 32
Project: ctsms   File: Settings.java   Source Code and License 6 votes vote down vote up
private static String checkPDFTemplateFileName(String PDFTemplateFileName, String path) throws IOException, IllegalArgumentException {
	String template;
	if (PDFTemplateFileName != null && PDFTemplateFileName.length() > 0) {
		File file = new File(PDFTemplateFileName);
		if (file.exists()) {
			if (file.isFile()) {
				if (file.canRead()) {
					PDDocument doc = null;
					try {
						doc = PDDocument.load(file);
						PDPage page = (PDPage) doc.getDocumentCatalog().getAllPages().get(0);
						PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);
						contentStream.close();
						template = file.getCanonicalPath();
					} catch (Exception e) {
						throw new IllegalArgumentException(L10nUtil.getMessage(MessageCodes.PDF_TEMPLATE_FILE_INVALID_PDF_ERROR,
								DefaultMessages.PDF_TEMPLATE_FILE_INVALID_PDF_ERROR, PDFTemplateFileName), e);
					} finally {
						if (doc != null) {
							doc.close();
						}
					}
				} else {
					throw new IllegalArgumentException(L10nUtil.getMessage(MessageCodes.PDF_TEMPLATE_FILE_ACCESS_ERROR, DefaultMessages.PDF_TEMPLATE_FILE_ACCESS_ERROR,
							PDFTemplateFileName));
				}
			} else {
				throw new IllegalArgumentException(L10nUtil.getMessage(MessageCodes.PDF_TEMPLATE_FILE_NOTAFILE_ERROR, DefaultMessages.PDF_TEMPLATE_FILE_NOTAFILE_ERROR,
						PDFTemplateFileName));
			}
		} else {
			throw new IllegalArgumentException(L10nUtil.getMessage(MessageCodes.PDF_TEMPLATE_FILE_DOES_NOT_EXIST_ERROR, DefaultMessages.PDF_TEMPLATE_FILE_DOES_NOT_EXIST_ERROR,
					PDFTemplateFileName));
		}
	} else {
		template = "";
	}
	return template;
}
 
Example 33
Project: Machine-Learning-End-to-Endguide-for-Java-developers   File: PDFExtractionExample.java   Source Code and License 5 votes vote down vote up
public static void main(String[] args) {
    try {
        PDDocument document = PDDocument.load(new File("PDF File.pdf"));
            PDFTextStripper Tstripper = new PDFTextStripper();
            String documentText = Tstripper.getText(document);
            System.out.println(documentText);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 34
Project: chart-parser   File: ChartParser.java   Source Code and License 5 votes vote down vote up
/**
 * Uses {@link ChartStripper} (an extension of Apache PDFBox's {@link PDFTextStripper}) to
 * extract the text from the PDF and, adding a header row, write a CSV String with each row
 * being a character from the PDF with its location etc.
 */
static String createCsvChart(PDDocument raceChart) throws IOException {
    ChartStripper chartStripper = new ChartStripper(new StringWriter());
    try (StringWriter writer = chartStripper.getWriter()) {
        try (StringWriter throwawayWriter = new StringWriter()) {
            writer.write("xDirAdj|yDirAdj|fontSize|xScale|height|widthOfSpace|widthDirAdj|" +
                    "unicode");
            chartStripper.writeText(raceChart, throwawayWriter);
        }
        return writer.getBuffer().toString();
    }
}
 
Example 35
Project: document-management-store-app   File: PdfThumbnailCreator.java   Source Code and License 5 votes vote down vote up
public BufferedImage getImg(InputStream pdf) {
    try {
        PDDocument document = PDDocument.load(pdf);
        PDFRenderer pdfRenderer = new PDFRenderer(document);
        BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(0, 300, ImageType.RGB);
        return ImageThumbnailCreator.resizeImage(bufferedImage);
    } catch (Exception e) {
        throw new CantCreateThumbnailException(e);
    }
}
 
Example 36
Project: Java-Data-Science-Made-Easy   File: PDFExtractionExample.java   Source Code and License 5 votes vote down vote up
public static void main(String[] args) {
    try {
        PDDocument document = PDDocument.load(new File("PDF File.pdf"));
            PDFTextStripper Tstripper = new PDFTextStripper();
            String documentText = Tstripper.getText(document);
            System.out.println(documentText);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 37
Project: pdf-table   File: PdfTableReader.java   Source Code and License 5 votes vote down vote up
/**
 * Parses range of PDF pages and returns list of lists of rows containing cell texts.
 *
 * @param document  PDF document instance
 * @param startPage first page in range to parse (first page == 1)
 * @param endPage   last page in range
 * @return List of pages
 * @throws IOException
 */
public List<ParsedTablePage> parsePdfTablePages(PDDocument document, int startPage, int endPage) throws IOException {
    List<ParsedTablePage> out = new ArrayList<>();
    PDFRenderer renderer = new PDFRenderer(document);
    for (int page = startPage - 1; page < endPage; ++page) {
        BufferedImage bi;
        synchronized (this) {
            bi = renderer.renderImageWithDPI(page, settings.getPdfRenderingDpi(), ImageType.RGB);
        }
        ParsedTablePage parsedTablePage = parsePdfTablePage(bi, document.getPage(page), page + 1);
        out.add(parsedTablePage);
    }
    return out;
}
 
Example 38
Project: ctsms   File: InquiriesPDFPainter.java   Source Code and License 5 votes vote down vote up
@Override
public void loadFonts(PDDocument doc) throws Exception {
	fontA = PDFUtil.loadFont(Settings.getPDFFontName(InquiriesPDFSettingCodes.FONT_A, Bundle.INQUIRIES_PDF, null), doc, DEFAULT_BASE_FONT);
	fontB = PDFUtil.loadFont(Settings.getPDFFontName(InquiriesPDFSettingCodes.FONT_B, Bundle.INQUIRIES_PDF, null), doc, DEFAULT_BASE_FONT);
	fontC = PDFUtil.loadFont(Settings.getPDFFontName(InquiriesPDFSettingCodes.FONT_C, Bundle.INQUIRIES_PDF, null), doc, DEFAULT_BASE_FONT);
	fontD = PDFUtil.loadFont(Settings.getPDFFontName(InquiriesPDFSettingCodes.FONT_D, Bundle.INQUIRIES_PDF, null), doc, DEFAULT_BASE_FONT);
	// fontE = PDFUtil.loadFont(Settings.getPDFFontName(InquiriesPDFSettingCodes.FONT_E, Bundle.INQUIRIES_PDF, null), doc, DEFAULT_BASE_FONT);
	// fontF = PDFUtil.loadFont(Settings.getPDFFontName(InquiriesPDFSettingCodes.FONT_F, Bundle.INQUIRIES_PDF, null), doc, DEFAULT_BASE_FONT);
}
 
Example 39
Project: BoxTable   File: Row.java   Source Code and License 5 votes vote down vote up
/**
 * Renders this row at the specified position
 * 
 * @param table
 *            The table this row is part of
 * @param document
 *            The document this cell is rendered to
 * @param stream
 *            The PDPageContentStream used to render this cell
 * @param index
 *            The index of this row in the table
 * @param colWidths
 *            The widths of the individual columns of the table
 * @param filler
 *            The CellFiller for the table
 * @param left
 *            The coordinate of the left edge of the cell
 * @param top
 *            The coordinate of the top edge of the cell
 * @param width
 *            The width the cell will be rendered
 * @param height
 *            The height the cell will be rendered
 * @throws IOException
 *             If writing to the stream fails
 */
public void render(final Table table, final PDDocument document, final PDPageContentStream stream, final int index, final float[] colWidths,
		final CellFiller filler, final float left, final float top, final float width, final float height) throws IOException {
	drawBorder(stream, left, top, width, height);

	float xPos = left;
	int colId = 0;
	for (final Cell cell : getCells()) {
		float cellWidth = colWidths[colId];
		for (int j = 1; j < cell.getColSpan(); ++j) {
			cellWidth += colWidths[colId + j];
		}
		float cellHeight = height;
		for (int j = 1; j < cell.getRowSpan(); ++j) {
			cellHeight += getHeight(colWidths);
		}

		if (filler != null) {
			filler.fill(stream, index, colId, xPos, top, cellWidth, cellHeight);
		}

		table.handleEvent(EventType.BEFORE_CELL, document, stream, xPos, top, cellWidth, cellHeight);
		handleEvent(EventType.BEFORE_CELL, document, stream, xPos, top, cellWidth, cellHeight);
		cell.handleEvent(EventType.BEFORE_CELL, document, stream, xPos, top, cellWidth, cellHeight);

		cell.render(document, stream, xPos, top, cellWidth, cellHeight);

		cell.handleEvent(EventType.AFTER_CELL, document, stream, xPos, top, cellWidth, cellHeight);
		handleEvent(EventType.AFTER_CELL, document, stream, xPos, top, cellWidth, cellHeight);
		table.handleEvent(EventType.AFTER_CELL, document, stream, xPos, top, cellWidth, cellHeight);

		colId += cell.getColSpan();
		xPos += cellWidth;
	}
}
 
Example 40
Project: BoxTable   File: Table.java   Source Code and License 5 votes vote down vote up
/**
 * Renders a subset of the rows of this table
 * 
 * @param document
 *            The document the table is rendered to
 * @param stream
 *            The PDPageContentStream used to render the rows
 * @param startIndex
 *            The start of the rows to be rendered (inclusive)
 * @param endIndex
 *            The end of the rows to be rendered (exclusive) or -1 if all rows up to the last one are to be rendered
 * @param width
 *            The width of the table
 * @param left
 *            The left edge of the rendered rows
 * @param top
 *            The top edge of the rendered rows
 * @return The bottom edge of the last rendered row
 * @throws IOException
 *             If writing to the stream fails
 */
public float renderRows(final PDDocument document, final PDPageContentStream stream, final int startIndex, int endIndex, final float width,
		final float left, final float top) throws IOException {
	if (endIndex == -1) {
		endIndex = rows.size();
	}

	stream.setStrokingColor(Color.BLACK);

	final float[] colWidths = getColumnWidths(width);
	float yPos = top;

	for (int i = startIndex; i < endIndex; ++i) {
		final Row row = rows.get(i);
		final float height = row.getHeight(colWidths);

		handleEvent(EventType.BEFORE_ROW, document, stream, left, yPos, width, height);
		row.handleEvent(EventType.BEFORE_ROW, document, stream, left, yPos, width, height);

		row.render(this, document, stream, i, colWidths, filler, left, yPos, width, height);

		row.handleEvent(EventType.AFTER_ROW, document, stream, left, yPos, width, height);
		handleEvent(EventType.AFTER_ROW, document, stream, left, yPos, width, height);

		yPos -= height;
	}

	return yPos;
}