如何使用 iText 在小尺寸页面中放置文本和图像
how to place text and images in small sized page using iText
我需要制作一个如下所示的 PDF 页面:
我在制作适合小尺寸页面的两栏时遇到问题。
这是我的代码:
public void createSizedPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.setMargins(5,5,5,5);
Rectangle one = new Rectangle(290,100);
one.setBackgroundColor(Color.YELLOW);
document.setPageSize(one);
document.open();
Paragraph consigneeName = new Paragraph("Ahmed");
Paragraph address = new Paragraph("Casa ST 121");
String codeBL = "14785236987541";
PdfContentByte cb = writer.getDirectContent();
Barcode128 code128 = new Barcode128();
code128.setBaseline(9);
code128.setSize(9);
code128.setCode(codeBL);
code128.setCodeType(Barcode128.CODE128);
Image code128Image = code128.createImageWithBarcode(cb, null, null);
Paragraph right = new Paragraph();
right.add(consigneeName);
right.add(address);
right.add(code128Image);
Chunk glue = new Chunk(new VerticalPositionMark());
Paragraph p = new Paragraph();
p.add(right);
p.add(new Chunk(glue));
p.add(code128Image);
document.add(p);
document.close();
}
解决您的问题的一种方法是创建一个带有 AcroForm 字段的模板 PDF。您可以手动创建一个漂亮的设计,然后通过将数据(文本、条形码)放在由充当占位符的字段定义的适当位置以编程方式填写表单。
另一种方法是从头开始创建 PDF,查看您的代码,这似乎是您采用的方法。
你的问题并不完全清楚,因为你分享了你的代码,但你没有解释你遇到的问题。正如我已经评论过的:
are you unable to scale images? are you unable to define a smaller
font size? are you unable to create a table with specific dimension?
You say I'm having problems to make two columns that fits in a small
sized page but you forgot to describe the problems.
你没有给出这些问题的答案,这使得别人很难回答你的问题。 Stack Overflow reader 唯一能做的就是在你的位置上完成你的工作。这不是 Stack Overflow 的目的。
此外,您的问题的答案是如此微不足道,以至于 Stack Overflow reader 很难理解您发布问题的原因。
你说你需要在两列中添加数据(文本和条形码),你实际上是说你想创建一个table。这是 table:
的一个例子
如果您查看 SmallTable 示例,您可以了解它是如何构建的。
您想要一个尺寸为 290 x 100 用户单位的 PDF,每边边距为 5 个用户单位。这意味着您有 space 用于测量 280 x 90 用户单位的 table。查看您的屏幕截图,我会说您有一列 160 个用户单元和一列 120 个用户单元。我还想说您有三行,每行 30 个用户单位。
好的,那你为什么不根据这些维度创建一个 table?
public void createPdf(String dest) throws IOException, DocumentException {
Rectangle small = new Rectangle(290,100);
Font smallfont = new Font(FontFamily.HELVETICA, 10);
Document document = new Document(small, 5, 5, 5, 5);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(2);
table.setTotalWidth(new float[]{ 160, 120 });
table.setLockedWidth(true);
PdfContentByte cb = writer.getDirectContent();
// first row
PdfPCell cell = new PdfPCell(new Phrase("Some text here"));
cell.setFixedHeight(30);
cell.setBorder(Rectangle.NO_BORDER);
cell.setColspan(2);
table.addCell(cell);
// second row
cell = new PdfPCell(new Phrase("Some more text", smallfont));
cell.setFixedHeight(30);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
Barcode128 code128 = new Barcode128();
code128.setCode("14785236987541");
code128.setCodeType(Barcode128.CODE128);
Image code128Image = code128.createImageWithBarcode(cb, null, null);
cell = new PdfPCell(code128Image, true);
cell.setBorder(Rectangle.NO_BORDER);
cell.setFixedHeight(30);
table.addCell(cell);
// third row
table.addCell(cell);
cell = new PdfPCell(new Phrase("and something else here", smallfont));
cell.setBorder(Rectangle.NO_BORDER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
document.add(table);
document.close();
}
在这个例子中,
- 您将学习如何更改单元格中内容的字体,
- 您将学习如何更改水平和垂直对齐方式,
- 您将学习如何缩放条形码以使其适合单元格,
- ...
所有这些功能都在官方文档中进行了说明。正如我之前所说:您没有解释问题的性质。您的文档中有什么不清楚的地方?你的问题是什么?
我需要制作一个如下所示的 PDF 页面:
我在制作适合小尺寸页面的两栏时遇到问题。
这是我的代码:
public void createSizedPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.setMargins(5,5,5,5);
Rectangle one = new Rectangle(290,100);
one.setBackgroundColor(Color.YELLOW);
document.setPageSize(one);
document.open();
Paragraph consigneeName = new Paragraph("Ahmed");
Paragraph address = new Paragraph("Casa ST 121");
String codeBL = "14785236987541";
PdfContentByte cb = writer.getDirectContent();
Barcode128 code128 = new Barcode128();
code128.setBaseline(9);
code128.setSize(9);
code128.setCode(codeBL);
code128.setCodeType(Barcode128.CODE128);
Image code128Image = code128.createImageWithBarcode(cb, null, null);
Paragraph right = new Paragraph();
right.add(consigneeName);
right.add(address);
right.add(code128Image);
Chunk glue = new Chunk(new VerticalPositionMark());
Paragraph p = new Paragraph();
p.add(right);
p.add(new Chunk(glue));
p.add(code128Image);
document.add(p);
document.close();
}
解决您的问题的一种方法是创建一个带有 AcroForm 字段的模板 PDF。您可以手动创建一个漂亮的设计,然后通过将数据(文本、条形码)放在由充当占位符的字段定义的适当位置以编程方式填写表单。
另一种方法是从头开始创建 PDF,查看您的代码,这似乎是您采用的方法。
你的问题并不完全清楚,因为你分享了你的代码,但你没有解释你遇到的问题。正如我已经评论过的:
are you unable to scale images? are you unable to define a smaller font size? are you unable to create a table with specific dimension? You say I'm having problems to make two columns that fits in a small sized page but you forgot to describe the problems.
你没有给出这些问题的答案,这使得别人很难回答你的问题。 Stack Overflow reader 唯一能做的就是在你的位置上完成你的工作。这不是 Stack Overflow 的目的。
此外,您的问题的答案是如此微不足道,以至于 Stack Overflow reader 很难理解您发布问题的原因。
你说你需要在两列中添加数据(文本和条形码),你实际上是说你想创建一个table。这是 table:
的一个例子如果您查看 SmallTable 示例,您可以了解它是如何构建的。
您想要一个尺寸为 290 x 100 用户单位的 PDF,每边边距为 5 个用户单位。这意味着您有 space 用于测量 280 x 90 用户单位的 table。查看您的屏幕截图,我会说您有一列 160 个用户单元和一列 120 个用户单元。我还想说您有三行,每行 30 个用户单位。
好的,那你为什么不根据这些维度创建一个 table?
public void createPdf(String dest) throws IOException, DocumentException {
Rectangle small = new Rectangle(290,100);
Font smallfont = new Font(FontFamily.HELVETICA, 10);
Document document = new Document(small, 5, 5, 5, 5);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(2);
table.setTotalWidth(new float[]{ 160, 120 });
table.setLockedWidth(true);
PdfContentByte cb = writer.getDirectContent();
// first row
PdfPCell cell = new PdfPCell(new Phrase("Some text here"));
cell.setFixedHeight(30);
cell.setBorder(Rectangle.NO_BORDER);
cell.setColspan(2);
table.addCell(cell);
// second row
cell = new PdfPCell(new Phrase("Some more text", smallfont));
cell.setFixedHeight(30);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
Barcode128 code128 = new Barcode128();
code128.setCode("14785236987541");
code128.setCodeType(Barcode128.CODE128);
Image code128Image = code128.createImageWithBarcode(cb, null, null);
cell = new PdfPCell(code128Image, true);
cell.setBorder(Rectangle.NO_BORDER);
cell.setFixedHeight(30);
table.addCell(cell);
// third row
table.addCell(cell);
cell = new PdfPCell(new Phrase("and something else here", smallfont));
cell.setBorder(Rectangle.NO_BORDER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
document.add(table);
document.close();
}
在这个例子中,
- 您将学习如何更改单元格中内容的字体,
- 您将学习如何更改水平和垂直对齐方式,
- 您将学习如何缩放条形码以使其适合单元格,
- ...
所有这些功能都在官方文档中进行了说明。正如我之前所说:您没有解释问题的性质。您的文档中有什么不清楚的地方?你的问题是什么?