Search in sources :

Example 16 with PDPageContentStream

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

the class TestLayerUtility method createMainPDF.

private File createMainPDF() throws IOException {
    File targetFile = new File(testResultsDir, "text-doc.pdf");
    PDDocument doc = new PDDocument();
    try {
        // Create new page
        PDPage page = new PDPage();
        doc.addPage(page);
        PDResources resources = page.getResources();
        if (resources == null) {
            resources = new PDResources();
            page.setResources(resources);
        }
        final String[] text = new String[] { "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer fermentum lacus in eros", "condimentum eget tristique risus viverra. Sed ac sem et lectus ultrices placerat. Nam", "fringilla tincidunt nulla id euismod. Vivamus eget mauris dui. Mauris luctus ullamcorper", "leo, et laoreet diam suscipit et. Nulla viverra commodo sagittis. Integer vitae rhoncus velit.", "Mauris porttitor ipsum in est sagittis non luctus purus molestie. Sed placerat aliquet", "vulputate." };
        // Setup page content stream and paint background/title
        PDPageContentStream contentStream = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.OVERWRITE, false);
        PDFont font = PDType1Font.HELVETICA_BOLD;
        contentStream.beginText();
        contentStream.newLineAtOffset(50, 720);
        contentStream.setFont(font, 14);
        contentStream.showText("Simple test document with text.");
        contentStream.endText();
        font = PDType1Font.HELVETICA;
        contentStream.beginText();
        int fontSize = 12;
        contentStream.setFont(font, fontSize);
        contentStream.newLineAtOffset(50, 700);
        for (String line : text) {
            contentStream.newLineAtOffset(0, -fontSize * 1.2f);
            contentStream.showText(line);
        }
        contentStream.endText();
        contentStream.close();
        doc.save(targetFile.getAbsolutePath());
    } finally {
        doc.close();
    }
    return targetFile;
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) File(java.io.File)

Example 17 with PDPageContentStream

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

the class TestLayerUtility method createOverlay1.

private File createOverlay1() throws IOException {
    File targetFile = new File(testResultsDir, "overlay1.pdf");
    PDDocument doc = new PDDocument();
    try {
        // Create new page
        PDPage page = new PDPage();
        doc.addPage(page);
        PDResources resources = page.getResources();
        if (resources == null) {
            resources = new PDResources();
            page.setResources(resources);
        }
        // Setup page content stream and paint background/title
        PDPageContentStream contentStream = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.OVERWRITE, false);
        PDFont font = PDType1Font.HELVETICA_BOLD;
        contentStream.setNonStrokingColor(AWTColor.LIGHT_GRAY);
        contentStream.beginText();
        float fontSize = 96;
        contentStream.setFont(font, fontSize);
        String text = "OVERLAY";
        // float sw = font.getStringWidth(text);
        // Too bad, base 14 fonts don't return character metrics.
        PDRectangle crop = page.getCropBox();
        float cx = crop.getWidth() / 2f;
        float cy = crop.getHeight() / 2f;
        Matrix transform = new Matrix();
        transform.translate(cx, cy);
        transform.rotate(Math.toRadians(45));
        transform.translate(-190, /* sw/2 */
        0);
        contentStream.setTextMatrix(transform);
        contentStream.showText(text);
        contentStream.endText();
        contentStream.close();
        doc.save(targetFile.getAbsolutePath());
    } finally {
        doc.close();
    }
    return targetFile;
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) Matrix(com.tom_roush.pdfbox.util.Matrix) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) File(java.io.File)

Example 18 with PDPageContentStream

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

the class PDFCloneUtilityTest method testClonePDFWithCosArrayStream2.

/**
 * broader test that saves to a real PDF document.
 *
 * @throws IOException
 */
public void testClonePDFWithCosArrayStream2() throws IOException {
    final String TESTDIR = "target/test-output/clone/";
    final String CLONESRC = "clone-src.pdf";
    final String CLONEDST = "clone-dst.pdf";
    new File(TESTDIR).mkdirs();
    PDDocument srcDoc = new PDDocument();
    PDPage pdPage = new PDPage();
    srcDoc.addPage(pdPage);
    PDPageContentStream pdPageContentStream1 = new PDPageContentStream(srcDoc, pdPage, AppendMode.APPEND, false);
    pdPageContentStream1.setNonStrokingColor(AWTColor.black);
    pdPageContentStream1.addRect(100, 600, 300, 100);
    pdPageContentStream1.fill();
    pdPageContentStream1.close();
    PDPageContentStream pdPageContentStream2 = new PDPageContentStream(srcDoc, pdPage, AppendMode.APPEND, false);
    pdPageContentStream2.setNonStrokingColor(AWTColor.red);
    pdPageContentStream2.addRect(100, 500, 300, 100);
    pdPageContentStream2.fill();
    pdPageContentStream2.close();
    PDPageContentStream pdPageContentStream3 = new PDPageContentStream(srcDoc, pdPage, AppendMode.APPEND, false);
    pdPageContentStream3.setNonStrokingColor(AWTColor.yellow);
    pdPageContentStream3.addRect(100, 400, 300, 100);
    pdPageContentStream3.fill();
    pdPageContentStream3.close();
    srcDoc.save(TESTDIR + CLONESRC);
    PDFMergerUtility merger = new PDFMergerUtility();
    PDDocument dstDoc = new PDDocument();
    // this calls PDFCloneUtility.cloneForNewDocument(),
    // which would fail before the fix in PDFBOX-2052
    merger.appendDocument(dstDoc, srcDoc);
    // save and reload PDF, so that one can see that the files are legit
    dstDoc.save(TESTDIR + CLONEDST);
    PDDocument.load(new File(TESTDIR + CLONESRC)).close();
    PDDocument.load(new File(TESTDIR + CLONESRC), (String) null).close();
    PDDocument.load(new File(TESTDIR + CLONEDST)).close();
    PDDocument.load(new File(TESTDIR + CLONEDST), (String) null).close();
}
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)

Example 19 with PDPageContentStream

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

the class PDFontTest method testPDFBox3826createDoc.

private byte[] testPDFBox3826createDoc(TrueTypeFont ttf) throws IOException {
    PDDocument doc = new PDDocument();
    PDPage page = new PDPage();
    doc.addPage(page);
    // type 0 subset embedding
    PDFont font = PDType0Font.load(doc, ttf, true);
    PDPageContentStream cs = new PDPageContentStream(doc, page);
    cs.beginText();
    cs.newLineAtOffset(10, 700);
    cs.setFont(font, 10);
    cs.showText("testMultipleFontFileReuse1");
    cs.endText();
    // type 0 full embedding
    font = PDType0Font.load(doc, ttf, false);
    cs.beginText();
    cs.newLineAtOffset(10, 650);
    cs.setFont(font, 10);
    cs.showText("testMultipleFontFileReuse2");
    cs.endText();
    // tt full embedding but only WinAnsiEncoding
    font = PDTrueTypeFont.load(doc, ttf, WinAnsiEncoding.INSTANCE);
    cs.beginText();
    cs.newLineAtOffset(10, 600);
    cs.setFont(font, 10);
    cs.showText("testMultipleFontFileReuse3");
    cs.endText();
    cs.close();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    doc.save(baos);
    doc.close();
    return baos.toByteArray();
}
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)

Example 20 with PDPageContentStream

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

the class CCITTFactoryTest method testCreateFromRandomAccessSingle.

/**
 * Tests CCITTFactory#createFromRandomAccess(PDDocument document,
 * RandomAccess reader) with a single page TIFF
 */
public void testCreateFromRandomAccessSingle() throws IOException {
    String tiffG3Path = "pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/ccittg3.tif";
    String tiffG4Path = "pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/ccittg4.tif";
    PDDocument document = new PDDocument();
    PDImageXObject ximage3 = CCITTFactory.createFromRandomAccess(document, new RandomAccessBuffer(testContext.getAssets().open(tiffG3Path)));
    validate(ximage3, 1, 344, 287, "tiff", PDDeviceGray.INSTANCE.getName());
    // Bitmap bim3 = ImageIO.read(new File(tiffG3Path));
    // checkIdent(bim3, ximage3.getOpaqueImage());
    PDPage page = new PDPage(PDRectangle.A4);
    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false);
    contentStream.drawImage(ximage3, 0, 0, ximage3.getWidth(), ximage3.getHeight());
    contentStream.close();
    PDImageXObject ximage4 = CCITTFactory.createFromRandomAccess(document, new RandomAccessBuffer(testContext.getAssets().open(tiffG4Path)));
    validate(ximage4, 1, 344, 287, "tiff", PDDeviceGray.INSTANCE.getName());
    // Bitmap bim4 = ImageIO.read(new File(tiffG3Path));
    // checkIdent(bim4, ximage4.getOpaqueImage());
    page = new PDPage(PDRectangle.A4);
    document.addPage(page);
    contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false);
    contentStream.drawImage(ximage4, 0, 0);
    contentStream.close();
    document.save(testResultsDir + "/singletiff.pdf");
    document.close();
    document = PDDocument.load(new File(testResultsDir, "singletiff.pdf"));
    assertEquals(2, document.getNumberOfPages());
    document.close();
}
Also used : RandomAccessBuffer(com.tom_roush.pdfbox.io.RandomAccessBuffer) 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)

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