Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using the example to generate a PDF with text and then reading that text with the tutorial in question</p> <pre><code>package com.mycompany.mavenproject; import java.io.File; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.apache.pdfbox.util.PDFTextStripper; /** * Unit test for simple App. */ public class AppTest extends TestCase { public static Test suite() { return new TestSuite(AppTest.class); } public void test() throws Exception { final String fileName = "PDFWithText.pdf"; writeDocument(fileName); PDDocument pd = PDDocument.load(new File(fileName)); PDFTextStripper stripper = new PDFTextStripper(); String text = stripper.getText(pd); assertEquals("Hello from www.printmyfolders.com", text.trim()); } private void writeDocument(String fileName) throws Exception { PDDocument doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); PDFont font = PDType1Font.HELVETICA_BOLD; PDPageContentStream content = new PDPageContentStream(doc, page); content.beginText(); content.setFont(font, 12); content.moveTextPositionByAmount(100, 700); content.drawString("Hello from www.printmyfolders.com"); content.endText(); content.close(); doc.save(fileName); doc.close(); } } </code></pre> <p>works without exception. Considering the exception bubbles up from the load method, ensure the PDF is not malformed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload