Java Code Examples for org.apache.pdfbox.pdmodel.PDDocument.getPage()

The following are Jave code examples for showing how to use getPage() of the org.apache.pdfbox.pdmodel.PDDocument class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: BoxTable   File: Table.java   Source Code and License Vote up 8 votes
/**
 * 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 2
Project: 484_P7_1-Java   File: Debug.java   Source Code and License Vote up 5 votes
public static void renderPage(String pdfPath, String outPath, int pageNumber, Rectangle area,
                              boolean drawTextChunks, boolean drawSpreadsheets, boolean drawRulings, boolean drawIntersections,
                              boolean drawColumns, boolean drawCharacters, boolean drawArea, boolean drawCells,
                              boolean drawUnprocessedRulings, boolean drawProjectionProfile, boolean drawClippingPaths,
                              boolean drawDetectedTables) throws IOException {
    PDDocument document = PDDocument.load(new File(pdfPath));

    ObjectExtractor oe = new ObjectExtractor(document);

    Page page = oe.extract(pageNumber + 1);

    if (area != null) {
        page = page.getArea(area);
    }

    PDPage p = document.getPage(pageNumber);

    BufferedImage image = Utils.pageConvertToImage(p, 72, ImageType.RGB);

    Graphics2D g = (Graphics2D) image.getGraphics();

    if (drawTextChunks) {
        debugTextChunks(g, page);
    }
    if (drawSpreadsheets) {
        debugSpreadsheets(g, page);
    }
    if (drawRulings) {
        debugRulings(g, page);
    }
    if (drawIntersections) {
        debugIntersections(g, page);
    }
    if (drawColumns) {
        debugColumns(g, page);
    }
    if (drawCharacters) {
        debugCharacters(g, page);
    }
    if (drawArea) {
        g.setColor(Color.ORANGE);
        drawShape(g, area);
    }
    if (drawCells) {
        debugCells(g, area, page);
    }
    if (drawUnprocessedRulings) {
        debugNonCleanRulings(g, page);
    }
    if (drawProjectionProfile) {
        debugProjectionProfile(g, page);
    }
    if (drawClippingPaths) {
        // TODO: Enable when oe.clippingPaths is done
        //drawShapes(g, oe.clippingPaths,
        //		new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10f, new float[] { 3f }, 0f));
    }
    if (drawDetectedTables) {
        debugDetectedTables(g, page);
    }

    document.close();

    ImageIO.write(image, "jpg", new File(outPath));
}
 
Example 3
Project: 484_P7_1-Java   File: TestUtils.java   Source Code and License Vote up 5 votes
@Test
public void testJPEG2000DoesNotRaise() throws IOException {
    PDDocument pdf_document = PDDocument.load(new File("src/test/resources/technology/tabula/jpeg2000.pdf"));
    PDPage page = pdf_document.getPage(0);
    Utils.pageConvertToImage(page, 360, ImageType.RGB);
}