Java Code Examples for com.itextpdf.text.pdf.PdfContentByte#showText()

The following examples show how to use com.itextpdf.text.pdf.PdfContentByte#showText() . 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: SimpleRedactionTest.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
static byte[] createClippingTextPdf() throws DocumentException, IOException
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, baos);
    document.open();

    PdfContentByte directContent = writer.getDirectContent();
    directContent.beginText();
    directContent.setTextRenderingMode(PdfPatternPainter.TEXT_RENDER_MODE_CLIP);
    directContent.setTextMatrix(AffineTransform.getTranslateInstance(100, 400));
    directContent.setFontAndSize(BaseFont.createFont(), 100);
    directContent.showText("Test");
    directContent.endText();
    
    BufferedImage bim = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = bim.createGraphics();
    g2d.setColor(Color.BLUE);
    g2d.fillRect(0, 0, 500, 500);
    g2d.dispose();

    Image image = Image.getInstance(bim, null);
    directContent.addImage(image, 500, 0, 0, 599, 50, 50);
    document.close();

    return baos.toByteArray();
}
 
Example 2
Source File: CreateLink.java    From testarea-itext5 with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/34734669/define-background-color-and-transparency-of-link-annotation-in-pdf">
 * Define background color and transparency of link annotation in PDF
 * </a>
 * <p>
 * This test creates a link annotation with custom appearance. Adobe Reader chooses
 * to ignore it but other viewers use it. Interestingly Adobe Acrobat export-as-image
 * does use the custom appearance...
 * </p>
 */
@Test
public void testCreateLinkWithAppearance() throws IOException, DocumentException
{
    Document doc = new Document();
    PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(new File(RESULT_FOLDER, "custom-link.appearance.pdf")));
    writer.setCompressionLevel(0);
    doc.open();

    BaseFont baseFont = BaseFont.createFont();
    int fontSize = 15;
    doc.add(new Paragraph("Hello", new Font(baseFont, fontSize)));
    
    PdfContentByte content = writer.getDirectContent();
    
    String text = "Test";
    content.setFontAndSize(baseFont, fontSize);
    content.beginText();
    content.moveText(100, 500);
    content.showText(text);
    content.endText();
    
    Rectangle linkLocation = new Rectangle(95, 495 + baseFont.getDescentPoint(text, fontSize),
            105 + baseFont.getWidthPoint(text, fontSize), 505 + baseFont.getAscentPoint(text, fontSize));

    PdfAnnotation linkGreen = PdfAnnotation.createLink(writer, linkLocation, PdfName.HIGHLIGHT, "green" );
    PdfTemplate appearance = PdfTemplate.createTemplate(writer, linkLocation.getWidth(), linkLocation.getHeight());
    PdfGState state = new PdfGState();
    //state.FillOpacity = .3f;
    // IMPROVEMENT: Use blend mode Darken instead of transparency; you may also want to try Multiply.
    state.setBlendMode(new PdfName("Darken"));
    appearance.setGState(state);

    appearance.setColorFill(BaseColor.GREEN);
    appearance.rectangle(0, 0, linkLocation.getWidth(), linkLocation.getHeight());
    appearance.fill();
    linkGreen.setAppearance(PdfName.N, appearance);
    writer.addAnnotation(linkGreen);

    doc.open();
    doc.close();
}