Java: How to use itext to export PDF text absolute positioning (implementation method)

jar: itext-4.2.1.jar

Absolute positioning is required for the signing of many official documents, so record this code as follows:

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream( "test.pdf" ));
PdfContentByte cb = writer.getDirectContent();
BaseFont bf = BaseFont.createFont( "STSong-Light" , "UniGB-UCS2-H" ,BaseFont.EMBEDDED);
cb .beginText();
cb .setFontAndSize(bf, 12 );
cb .showTextAligned(PdfContentByte.ALIGN_CENTER, text + "This text is centered" , 250 , 700 , 0 );
cb .endText();

Sometimes the absolute positioning of the picture (official seal) is also needed:

Image image = Image.getInstance(request.getSession().getServletContext().getRealPath( "/" )+ "common/images/starpilot/signet.png" ); 
image .scaleAbsolute(mmTopx( 40 ), mmTopx( 39 ));
image .setAbsolutePosition( 400 , flagHeight);
document .add(image);

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *