Rotate a barcode
rotatedtext
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.objects;
import com.itextpdf.barcodes.BarcodeEAN;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.properties.TextAlignment;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class RotatedText {
public static final String DEST = "./target/sandbox/objects/rotated_text.pdf";
public static void main(String[] args) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new RotatedText().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws IOException {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, new PageSize(60, 140));
doc.setMargins(5, 5, 5, 5);
PdfFont boldFont = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);
PdfFont regularFont = PdfFontFactory.createFont(StandardFonts.HELVETICA);
Paragraph p1 = new Paragraph();
p1.add(new Text("23").setFont(boldFont).setFontSize(12));
p1.add(new Text("000").setFont(boldFont).setFontSize(6));
doc.add(p1);
Paragraph p2 = new Paragraph("T.T.C.").setFont(regularFont).setFontSize(6);
p2.setTextAlignment(TextAlignment.RIGHT);
doc.add(p2);
BarcodeEAN barcode = new BarcodeEAN(pdfDoc);
barcode.setCodeType(BarcodeEAN.EAN8);
barcode.setCode("12345678");
Rectangle rect = barcode.getBarcodeSize();
PdfFormXObject formXObject = new PdfFormXObject(new Rectangle(rect.getWidth(), rect.getHeight() + 10));
PdfCanvas pdfCanvas = new PdfCanvas(formXObject, pdfDoc);
new Canvas(pdfCanvas, new Rectangle(rect.getWidth(), rect.getHeight() + 10))
.showTextAligned(new Paragraph("DARK GRAY").setFont(regularFont).setFontSize(6), 0, rect.getHeight() + 2, TextAlignment.LEFT);
barcode.placeBarcode(pdfCanvas, ColorConstants.BLACK, ColorConstants.BLACK);
Image image = new Image(formXObject);
image.setRotationAngle(Math.toRadians(90));
image.setAutoScale(true);
doc.add(image);
Paragraph p3 = new Paragraph("SMALL").setFont(regularFont).setFontSize(6);
p3.setTextAlignment(TextAlignment.CENTER);
doc.add(p3);
doc.close();
}
}
C#
C#
using System;
using System.IO;
using iText.Barcodes;
using iText.IO.Font.Constants;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
namespace iText.Samples.Sandbox.Objects
{
public class RotatedText
{
public static readonly string DEST = "results/sandbox/objects/rotated_text.pdf";
public static void Main(string[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new RotatedText().ManipulatePdf(DEST);
}
protected void ManipulatePdf(string dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, new PageSize(60, 140));
doc.SetMargins(5, 5, 5, 5);
PdfFont boldFont = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);
PdfFont regularFont = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
Paragraph p1 = new Paragraph();
p1.Add(new Text("23").SetFont(boldFont).SetFontSize(12));
p1.Add(new Text("000").SetFont(boldFont).SetFontSize(6));
doc.Add(p1);
Paragraph p2 = new Paragraph("T.T.C.").SetFont(regularFont).SetFontSize(6);
p2.SetTextAlignment(TextAlignment.RIGHT);
doc.Add(p2);
BarcodeEAN barcode = new BarcodeEAN(pdfDoc);
barcode.SetCodeType(BarcodeEAN.EAN8);
barcode.SetCode("12345678");
Rectangle rect = barcode.GetBarcodeSize();
PdfFormXObject formXObject = new PdfFormXObject(new Rectangle(rect.GetWidth(), rect.GetHeight() + 10));
PdfCanvas pdfCanvas = new PdfCanvas(formXObject, pdfDoc);
new Canvas(pdfCanvas, new Rectangle(rect.GetWidth(), rect.GetHeight() + 10))
.ShowTextAligned(new Paragraph("DARK GRAY").SetFont(regularFont).SetFontSize(6), 0,
rect.GetHeight() + 2, TextAlignment.LEFT);
barcode.PlaceBarcode(pdfCanvas, ColorConstants.BLACK, ColorConstants.BLACK);
Image image = new Image(formXObject);
image.SetRotationAngle(Math.PI / 2f);
image.SetAutoScale(true);
doc.Add(image);
Paragraph p3 = new Paragraph("SMALL").SetFont(regularFont).SetFontSize(6);
p3.SetTextAlignment(TextAlignment.CENTER);
doc.Add(p3);
doc.Close();
}
}
}