Java Code Examples for com.lowagie.text.pdf.PdfReader#getPageN()

The following examples show how to use com.lowagie.text.pdf.PdfReader#getPageN() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Prd5873Test.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void runTest(MasterReport report)
        throws ResourceException, ReportProcessingException, IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    PdfReportUtil.createPDF(report, bout);

    PdfReader reader = new PdfReader(bout.toByteArray());

    printPdfPage(reader);

    final PdfDictionary pageN = reader.getPageN(1);
    final PdfDictionary asDict = pageN.getAsDict(PdfName.RESOURCES);
    final byte[] pageContent = reader.getPageContent(1);
    PdfValidator pv = new PdfValidator();
    pv.processContent(pageContent, asDict);

}
 
Example 2
Source File: PdfContentReaderTool.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Writes information about a specific page from PdfReader to the specified output stream.
 * 
 * @since 2.1.5
 * @param reader the PdfReader to read the page content from
 * @param pageNum the page number to read
 * @param out the output stream to send the content to
 * @throws IOException
 */
static public void listContentStreamForPage(PdfReader reader, int pageNum, PrintWriter out) throws IOException {
	out.println("==============Page " + pageNum + "====================");
	out.println("- - - - - Dictionary - - - - - -");
	PdfDictionary pageDictionary = reader.getPageN(pageNum);
	out.println(getDictionaryDetail(pageDictionary));
	out.println("- - - - - Content Stream - - - - - -");
	RandomAccessFileOrArray f = reader.getSafeFile();

	byte[] contentBytes = reader.getPageContent(pageNum, f);
	f.close();

	InputStream is = new ByteArrayInputStream(contentBytes);
	int ch;
	while ((ch = is.read()) != -1) {
		out.print((char) ch);
	}

	out.println("- - - - - Text Extraction - - - - - -");
	PdfTextExtractor extractor = new PdfTextExtractor(reader);
	String extractedText = extractor.getTextFromPage(pageNum);
	if (extractedText.length() != 0) {
		out.println(extractedText);
	} else {
		out.println("No text found on page " + pageNum);
	}

	out.println();

}
 
Example 3
Source File: PdfContentReaderTool.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Writes information about a specific page from PdfReader to the specified output stream.
 * @since 2.1.5
 * @param reader    the PdfReader to read the page content from
 * @param pageNum   the page number to read
 * @param out       the output stream to send the content to
 * @throws IOException
 */
static public void listContentStreamForPage(PdfReader reader, int pageNum, PrintWriter out) throws IOException {
    out.println("==============Page " + pageNum + "====================");
    out.println("- - - - - Dictionary - - - - - -");
    PdfDictionary pageDictionary = reader.getPageN(pageNum);
    out.println(getDictionaryDetail(pageDictionary));
    out.println("- - - - - Content Stream - - - - - -");
    RandomAccessFileOrArray f = reader.getSafeFile();
    
    byte[] contentBytes = reader.getPageContent(pageNum, f);
    f.close();

    
    InputStream is = new ByteArrayInputStream(contentBytes);
    int ch;
    while ((ch = is.read()) != -1){
        out.print((char)ch);
    }

    out.println("- - - - - Text Extraction - - - - - -");
    PdfTextExtractor extractor = new PdfTextExtractor(reader);
    String extractedText = extractor.getTextFromPage(pageNum);
    if (extractedText.length() != 0)
        out.println(extractedText);
    else
        out.println("No text found on page " + pageNum);
    
    out.println();
    
}
 
Example 4
Source File: PdfContentStreamProcessorTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void processBytes(final byte[] pdfBytes, final int pageNumber) throws IOException {
	final PdfReader pdfReader = new PdfReader(pdfBytes);

	final PdfDictionary pageDictionary = pdfReader.getPageN(pageNumber);

	final PdfDictionary resourceDictionary = pageDictionary.getAsDict(PdfName.RESOURCES);

	final PdfObject contentObject = pageDictionary.get(PdfName.CONTENTS);
	final byte[] contentBytes = readContentBytes(contentObject);
	_processor.processContent(contentBytes, resourceDictionary);
}
 
Example 5
Source File: PaperightPdfConverter.java    From website with GNU Affero General Public License v3.0 5 votes vote down vote up
public String cropPdf(String pdfFilePath) throws DocumentException, IOException, Exception {
	String filename = FilenameUtils.getBaseName(pdfFilePath) + "_cropped." + FilenameUtils.getExtension(pdfFilePath);
	filename = FilenameUtils.concat(System.getProperty("java.io.tmpdir"), filename);
	PdfReader reader = new PdfReader(pdfFilePath);
	try {
		PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
		try {
			for (int i = 1; i <= reader.getNumberOfPages(); i++) {
				PdfDictionary pdfDictionary = reader.getPageN(i);
				PdfArray cropArray = new PdfArray();
				Rectangle box = getSmallestBox(reader, i);
				//Rectangle cropbox = reader.getCropBox(i);
				if (box != null) {
					cropArray.add(new PdfNumber(box.getLeft()));
					cropArray.add(new PdfNumber(box.getBottom()));
					cropArray.add(new PdfNumber(box.getLeft() + box.getWidth()));
					cropArray.add(new PdfNumber(box.getBottom() + box.getHeight()));
					pdfDictionary.put(PdfName.CROPBOX, cropArray);
					pdfDictionary.put(PdfName.MEDIABOX, cropArray);
					pdfDictionary.put(PdfName.TRIMBOX, cropArray);
					pdfDictionary.put(PdfName.BLEEDBOX, cropArray);
				}
			}
			return filename;
		} finally {
			stamper.close();
		}
	} catch (Exception e) {
		logger.error(e.getMessage(), e);
		throw e;
	} finally {
		reader.close();
	}
}