Rotate a barcode

This is a code example of iText PDF, discover more.

17th December 2015
admin-marketing

Switch code for this example

RotatedText.java
  1. /*
  2.  * This example was written in answer to:
  3.  * http://stackoverflow.com/questions/34310018/changing-font-on-pdf-rotated-text
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Chunk;
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Element;
  12. import com.itextpdf.text.Font;
  13. import com.itextpdf.text.Font.FontFamily;
  14. import com.itextpdf.text.Image;
  15. import com.itextpdf.text.Paragraph;
  16. import com.itextpdf.text.Phrase;
  17. import com.itextpdf.text.Rectangle;
  18. import com.itextpdf.text.pdf.Barcode;
  19. import com.itextpdf.text.pdf.BarcodeEAN;
  20. import com.itextpdf.text.pdf.ColumnText;
  21. import com.itextpdf.text.pdf.PdfContentByte;
  22. import com.itextpdf.text.pdf.PdfTemplate;
  23. import com.itextpdf.text.pdf.PdfWriter;
  24. import java.io.File;
  25. import java.io.FileOutputStream;
  26. import java.io.IOException;
  27. import sandbox.WrapToTest;
  28.  
  29. /**
  30.  * @author Bruno Lowagie (iText Software)
  31.  */
  32. @WrapToTest
  33. public class RotatedText {
  34.    
  35.     public static final String DEST = "results/objects/rotated_text.pdf";
  36.    
  37.     public static void main(String[] args) throws IOException, DocumentException {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new RotatedText().createPdf(DEST);
  41.     }
  42.    
  43.     public void createPdf(String dest) throws IOException, DocumentException {
  44.         // step 1
  45.         Document document = new Document(new Rectangle(60, 120), 5, 5, 5, 5);
  46.         // step 2
  47.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  48.         // step 3
  49.         document.open();
  50.         // step 4
  51.         PdfContentByte canvas = writer.getDirectContent();
  52.        
  53.         Font big_bold = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
  54.         Font small_bold = new Font(FontFamily.HELVETICA, 6, Font.BOLD);
  55.         Font regular = new Font(FontFamily.HELVETICA, 6);
  56.         Paragraph p1 = new Paragraph();
  57.         p1.add(new Chunk("23", big_bold));
  58.         p1.add(new Chunk("000", small_bold));
  59.         document.add(p1);
  60.        
  61.         Paragraph p2 = new Paragraph("T.T.C.", regular);
  62.         p2.setAlignment(Element.ALIGN_RIGHT);
  63.         document.add(p2);
  64.        
  65.         BarcodeEAN barcode = new BarcodeEAN();
  66.         barcode.setCodeType(Barcode.EAN8);
  67.         barcode.setCode("12345678");
  68.         Rectangle rect = barcode.getBarcodeSize();
  69.         PdfTemplate template = canvas.createTemplate(rect.getWidth(), rect.getHeight() + 10);
  70.         ColumnText.showTextAligned(template, Element.ALIGN_LEFT,
  71.                 new Phrase("DARK GRAY", regular), 0, rect.getHeight() + 2, 0);
  72.         barcode.placeBarcode(template, BaseColor.BLACK, BaseColor.BLACK);
  73.         Image image = Image.getInstance(template);
  74.         image.setRotationDegrees(90);
  75.         document.add(image);
  76.        
  77.         Paragraph p3 = new Paragraph("SMALL", regular);
  78.         p3.setAlignment(Element.ALIGN_CENTER);
  79.         document.add(p3);
  80.        
  81.         // step 5
  82.         document.close();
  83.     }
  84. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now