Search in sources :

Example 26 with PDPageContentStream

use of com.tom_roush.pdfbox.pdmodel.PDPageContentStream in project PdfBox-Android by TomRoush.

the class TestFontEmbedding method testMaxEntries.

/**
 * Test corner case of PDFBOX-4302.
 *
 * @throws java.io.IOException
 */
@Test
public void testMaxEntries() throws IOException {
    File file;
    String text;
    text = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん" + "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン" + "12345678";
    // The test must have MAX_ENTRIES_PER_OPERATOR unique characters
    Set<Character> set = new HashSet<Character>(ToUnicodeWriter.MAX_ENTRIES_PER_OPERATOR);
    for (int i = 0; i < text.length(); ++i) {
        set.add(text.charAt(i));
    }
    assertEquals(ToUnicodeWriter.MAX_ENTRIES_PER_OPERATOR, set.size());
    PDDocument document = new PDDocument();
    PDPage page = new PDPage(PDRectangle.A0);
    document.addPage(page);
    File ipafont = new File("target/fonts/ipag00303", "ipag.ttf");
    assumeTrue(ipafont.exists());
    PDType0Font font = PDType0Font.load(document, ipafont);
    PDPageContentStream contentStream = new PDPageContentStream(document, page);
    contentStream.beginText();
    contentStream.setFont(font, 20);
    contentStream.newLineAtOffset(50, 3000);
    contentStream.showText(text);
    contentStream.endText();
    contentStream.close();
    file = new File(OUT_DIR, "PDFBOX-4302-test.pdf");
    document.save(file);
    document.close();
    // check that the extracted text matches what we wrote
    String extracted = getUnicodeText(file);
    assertEquals(text, extracted.trim());
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 27 with PDPageContentStream

use of com.tom_roush.pdfbox.pdmodel.PDPageContentStream in project PdfBox-Android by TomRoush.

the class TestFontEncoding method testPDFBox3884.

/**
 * PDFBOX-3826: Some unicodes are reached by several names in glyphlist.txt, e.g. tilde and
 * ilde.
 *
 * @throws IOException
 */
public void testPDFBox3884() throws IOException {
    PDDocument doc = new PDDocument();
    PDPage page = new PDPage();
    doc.addPage(page);
    PDPageContentStream cs = new PDPageContentStream(doc, page);
    cs.setFont(PDType1Font.HELVETICA, 20);
    cs.beginText();
    cs.newLineAtOffset(100, 700);
    // first tilde is "asciitilde" (from the keyboard), 2nd tilde is "tilde"
    // using ˜ would bring IllegalArgumentException prior to bugfix
    cs.showText("~˜");
    cs.endText();
    cs.close();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    doc.save(baos);
    doc.close();
    // verify
    doc = PDDocument.load(baos.toByteArray());
    PDFTextStripper stripper = new PDFTextStripper();
    String text = stripper.getText(doc);
    assertEquals("~˜", text.trim());
    doc.close();
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PDFTextStripper(com.tom_roush.pdfbox.text.PDFTextStripper)

Aggregations

PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)27 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)24 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)22 File (java.io.File)18 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)8 Test (org.junit.Test)7 Bitmap (android.graphics.Bitmap)5 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)5 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)5 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)4 PDFRenderer (com.tom_roush.pdfbox.rendering.PDFRenderer)4 COSArray (com.tom_roush.pdfbox.cos.COSArray)3 Matrix (com.tom_roush.pdfbox.util.Matrix)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Paint (android.graphics.Paint)2 RandomAccessBuffer (com.tom_roush.pdfbox.io.RandomAccessBuffer)2 PDFTextStripper (com.tom_roush.pdfbox.text.PDFTextStripper)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2