Search code examples
pdfbox

How to change text height in pdfbox


PDFBOX / JSF

Im trying to change the font height of a given text. I know how to change the fontsize only.

PDPageContentStream contentStreambc = new PDPageContentStream(doc1, page, true, true);
contentStreambc.setFont( fonta, 16 );

contentStreambc.beginText();
contentStreambc.moveTextPositionByAmount(200, 320);
contentStreambc.drawString( "abcdef");
contentStreambc.endText();
contentStreambc.close();

The code works fine. But How I change the font height ?

thanks in advance stack members.


Solution

  • If you need something like this

    enter image description here

    you can create it with this code:

    PDRectangle rec = new PDRectangle(220, 70);
    PDDocument document = null;
    document = new PDDocument();
    
    PDPage page = new PDPage(rec);
    document.addPage(page);
    
    PDPageContentStream content = new PDPageContentStream(document, page, true, true);
    
    content.beginText();
    content.moveTextPositionByAmount(7, 55);
    content.setFont(PDType1Font.HELVETICA, 12);
    content.drawString("Normal text (size 12)");
    content.setTextMatrix(1, 0, 0, 1.5f, 7, 30);
    content.drawString("Stretched text (size 12, factor 1.5)");
    content.setTextMatrix(1, 0, 0, 2f, 7, 5);
    content.drawString("Stretched text (size 12, factor 2)");
    content.endText();
    
    content.close();
    
    document.save("SimplePdfStretchedText.pdf");
    

    The code stretches the text by setting the text matrix accordingly; for details cf. chapter 9 of the PDF specification ISO 32000-1.

    PS: As you mention bar codes in a comment to another answer, this should indeed allow you to make higher bar codes while keeping the distances.