Rotate a barcode

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

31st May 2016
iText PDF

Switch code for this example

RotatedText.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example was written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/34310018/changing-font-on-pdf-rotated-text
  11.  */
  12. package com.itextpdf.samples.sandbox.objects;
  13.  
  14. import com.itextpdf.barcodes.BarcodeEAN;
  15. import com.itextpdf.io.font.FontConstants;
  16. import com.itextpdf.kernel.color.Color;
  17. import com.itextpdf.kernel.font.PdfFont;
  18. import com.itextpdf.kernel.font.PdfFontFactory;
  19. import com.itextpdf.kernel.geom.PageSize;
  20. import com.itextpdf.kernel.geom.Rectangle;
  21. import com.itextpdf.kernel.pdf.PdfDocument;
  22. import com.itextpdf.kernel.pdf.PdfWriter;
  23. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  24. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  25. import com.itextpdf.layout.Canvas;
  26. import com.itextpdf.layout.Document;
  27. import com.itextpdf.layout.element.Image;
  28. import com.itextpdf.layout.element.Paragraph;
  29. import com.itextpdf.layout.element.Text;
  30. import com.itextpdf.layout.property.TextAlignment;
  31. import com.itextpdf.samples.GenericTest;
  32. import com.itextpdf.test.annotations.type.SampleTest;
  33.  
  34. import org.junit.experimental.categories.Category;
  35.  
  36. import java.io.File;
  37. import java.io.IOException;
  38.  
  39. @Category(SampleTest.class)
  40. public class RotatedText extends GenericTest {
  41.     public static final String DEST = "./target/test/resources/sandbox/objects/rotated_text.pdf";
  42.  
  43.     public static void main(String[] args) throws IOException {
  44.         File file = new File(DEST);
  45.         file.getParentFile().mkdirs();
  46.         new RotatedText().manipulatePdf(DEST);
  47.     }
  48.  
  49.     public void manipulatePdf(String dest) throws IOException {
  50.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  51.         Document doc = new Document(pdfDoc, new PageSize(60, 140));
  52.         doc.setMargins(5, 5, 5, 5);
  53.  
  54.         PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
  55.         PdfFont regular = PdfFontFactory.createFont(FontConstants.HELVETICA);
  56.  
  57.         Paragraph p1 = new Paragraph();
  58.         p1.add(new Text("23").setFont(bold).setFontSize(12));
  59.         p1.add(new Text("000").setFont(bold).setFontSize(6));
  60.         doc.add(p1);
  61.  
  62.         Paragraph p2 = new Paragraph("T.T.C.").setFont(regular).setFontSize(6);
  63.         p2.setTextAlignment(TextAlignment.RIGHT);
  64.         doc.add(p2);
  65.  
  66.         BarcodeEAN barcode = new BarcodeEAN(pdfDoc);
  67.         barcode.setCodeType(BarcodeEAN.EAN8);
  68.         barcode.setCode("12345678");
  69.         Rectangle rect = barcode.getBarcodeSize();
  70.         PdfFormXObject template = new PdfFormXObject(new Rectangle(rect.getWidth(), rect.getHeight() + 10));
  71.         PdfCanvas templateCanvas = new PdfCanvas(template, pdfDoc);
  72.         new Canvas(templateCanvas, pdfDoc, new Rectangle(rect.getWidth(), rect.getHeight() + 10))
  73.                 .showTextAligned(new Paragraph("DARK GRAY").setFont(regular).setFontSize(6), 0, rect.getHeight() + 2, TextAlignment.LEFT);
  74.         barcode.placeBarcode(templateCanvas, Color.BLACK, Color.BLACK);
  75.         Image image = new Image(template);
  76.         image.setRotationAngle(Math.toRadians(90));
  77.         image.setAutoScale(true);
  78.         doc.add(image);
  79.  
  80.         Paragraph p3 = new Paragraph("SMALL").setFont(regular).setFontSize(6);
  81.         p3.setTextAlignment(TextAlignment.CENTER);
  82.         doc.add(p3);
  83.  
  84.         doc.close();
  85.     }
  86. }
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