更改 PDF 旋转文本的字体
Changing Font on PDF Rotated Text
我正在使用 iText 在与此格式相同的 PDF 上创建条形码:
问题是左边的数字,第一个零数字必须较小,而其余数字也必须是粗体。 "T.T.C." 也必须更小(不必在另一行)。
我可以使用以下代码轮换号码:
String price = "23000 T.T.C.";
PdfContentByte cb = docWriter.getDirectContent();
PdfTemplate textTemplate = cb.createTemplate(50, 50);
ColumnText columnText = new ColumnText(textTemplate);
columnText.setSimpleColumn(0, 0, 50, 50);
columnText.addElement(new Paragraph(price));
columnText.go();
Image image;
image = Image.getInstance(textTemplate);
image.setAlignment(Image.MIDDLE);
image.setRotationDegrees(90);
doc.add(image);
问题是我无法在线找到一种方法来更改 String price 打印在 PDF 上时某些字符的字体。
我创建了一个小型概念验证,生成的 PDF 如下所示:
如您所见,它具有不同大小和样式的文本。它还有一个旋转的条形码。
看看RotatedText例子:
public void createPdf(String dest) throws IOException, DocumentException {
// step 1
Document document = new Document(new Rectangle(60, 120), 5, 5, 5, 5);
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
// step 3
document.open();
// step 4
PdfContentByte canvas = writer.getDirectContent();
Font big_bold = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
Font small_bold = new Font(FontFamily.HELVETICA, 6, Font.BOLD);
Font regular = new Font(FontFamily.HELVETICA, 6);
Paragraph p1 = new Paragraph();
p1.add(new Chunk("23", big_bold));
p1.add(new Chunk("000", small_bold));
document.add(p1);
Paragraph p2 = new Paragraph("T.T.C.", regular);
p2.setAlignment(Element.ALIGN_RIGHT);
document.add(p2);
BarcodeEAN barcode = new BarcodeEAN();
barcode.setCodeType(Barcode.EAN8);
barcode.setCode("12345678");
Rectangle rect = barcode.getBarcodeSize();
PdfTemplate template = canvas.createTemplate(rect.getWidth(), rect.getHeight() + 10);
ColumnText.showTextAligned(template, Element.ALIGN_LEFT,
new Phrase("DARK GRAY", regular), 0, rect.getHeight() + 2, 0);
barcode.placeBarcode(template, BaseColor.BLACK, BaseColor.BLACK);
Image image = Image.getInstance(template);
image.setRotationDegrees(90);
document.add(image);
Paragraph p3 = new Paragraph("SMALL", regular);
p3.setAlignment(Element.ALIGN_CENTER);
document.add(p3);
// step 5
document.close();
}
这个例子解决了你所有的问题:
- 您希望
Paragraph
使用不同的字体:使用不同的 Chunk
对象组成 Paragraph
。
- 您想在条形码上添加额外的文本:将条形码添加到
PdfTemplate
并使用 ColumnText.showTextAligned()
添加额外的文本(并不是说您也可以编写 Phrase
使用不同的 Chunk
对象,如果你在额外的文本中需要不止一种字体)。
- 您想旋转条形码:将
PdfTemplate
包裹在 Image
对象中并旋转图像。
您可以查看结果:rotated_text.pdf
希望对您有所帮助。
我正在使用 iText 在与此格式相同的 PDF 上创建条形码:
问题是左边的数字,第一个零数字必须较小,而其余数字也必须是粗体。 "T.T.C." 也必须更小(不必在另一行)。
我可以使用以下代码轮换号码:
String price = "23000 T.T.C.";
PdfContentByte cb = docWriter.getDirectContent();
PdfTemplate textTemplate = cb.createTemplate(50, 50);
ColumnText columnText = new ColumnText(textTemplate);
columnText.setSimpleColumn(0, 0, 50, 50);
columnText.addElement(new Paragraph(price));
columnText.go();
Image image;
image = Image.getInstance(textTemplate);
image.setAlignment(Image.MIDDLE);
image.setRotationDegrees(90);
doc.add(image);
问题是我无法在线找到一种方法来更改 String price 打印在 PDF 上时某些字符的字体。
我创建了一个小型概念验证,生成的 PDF 如下所示:
如您所见,它具有不同大小和样式的文本。它还有一个旋转的条形码。
看看RotatedText例子:
public void createPdf(String dest) throws IOException, DocumentException {
// step 1
Document document = new Document(new Rectangle(60, 120), 5, 5, 5, 5);
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
// step 3
document.open();
// step 4
PdfContentByte canvas = writer.getDirectContent();
Font big_bold = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
Font small_bold = new Font(FontFamily.HELVETICA, 6, Font.BOLD);
Font regular = new Font(FontFamily.HELVETICA, 6);
Paragraph p1 = new Paragraph();
p1.add(new Chunk("23", big_bold));
p1.add(new Chunk("000", small_bold));
document.add(p1);
Paragraph p2 = new Paragraph("T.T.C.", regular);
p2.setAlignment(Element.ALIGN_RIGHT);
document.add(p2);
BarcodeEAN barcode = new BarcodeEAN();
barcode.setCodeType(Barcode.EAN8);
barcode.setCode("12345678");
Rectangle rect = barcode.getBarcodeSize();
PdfTemplate template = canvas.createTemplate(rect.getWidth(), rect.getHeight() + 10);
ColumnText.showTextAligned(template, Element.ALIGN_LEFT,
new Phrase("DARK GRAY", regular), 0, rect.getHeight() + 2, 0);
barcode.placeBarcode(template, BaseColor.BLACK, BaseColor.BLACK);
Image image = Image.getInstance(template);
image.setRotationDegrees(90);
document.add(image);
Paragraph p3 = new Paragraph("SMALL", regular);
p3.setAlignment(Element.ALIGN_CENTER);
document.add(p3);
// step 5
document.close();
}
这个例子解决了你所有的问题:
- 您希望
Paragraph
使用不同的字体:使用不同的Chunk
对象组成Paragraph
。 - 您想在条形码上添加额外的文本:将条形码添加到
PdfTemplate
并使用ColumnText.showTextAligned()
添加额外的文本(并不是说您也可以编写Phrase
使用不同的Chunk
对象,如果你在额外的文本中需要不止一种字体)。 - 您想旋转条形码:将
PdfTemplate
包裹在Image
对象中并旋转图像。
您可以查看结果:rotated_text.pdf
希望对您有所帮助。