使用 iText 的条形码打印问题
Barcode printing issue using iText
我正在使用 iText 动态生成 PDF docs.I 我能够生成 pdf,但我能够以下面提到的格式打印条形码
但需要以下面提到的格式打印条形码
条形码生成和PDF的代码return是:
public void generateShipmentItemPDF(ShipmentItemBarcodeVO objShipmentItem) {
Document document = new Document();
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
ec.responseReset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
PdfWriter docWriter = null;
String fileName =
"ShipmentItem_PO# " + objShipmentItem.getPoNumber() + "-" + "Line# " + objShipmentItem.getPoNumber() + "-" + "Item# " + objShipmentItem.getItemNumber().trim() + ".pdf";
LOGGER.info("fileName-: " + fileName);
ec.setResponseHeader("Content-Disposition",
"attachment; filename=\"" + fileName +
"\""); // The Save As popup magic is done here. You can give it any file name you want, this only won't work in MSIE, it will use current request URL as file name instead.
try{
docWriter = PdfWriter.getInstance(document, new FileOutputStream("D:\" + fileName));
document.open();
// document.add(new Paragraph("A Hello World PDF document."));
// document.newPage();
// document.add(new Paragraph("A New Page Hello World PDF document."));
PdfContentByte cb = docWriter.getDirectContent();
Barcode128 code128 = new Barcode128();
code128.setBarHeight(5f);
code128.setX(0.1f);
code128.setCode(objShipmentItem.getItemNumber());
code128.setCodeType(Barcode128.CODE128);
Image code128Image = code128.createImageWithBarcode(cb, null, null);
// code128Image.setAbsolutePosition(10,500);
// code128Image.scalePercent(50);
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(100);
// table.setSpacingBefore(100f);
// table.setSpacingAfter(100f);
// first row Print
String newline = System.getProperty("line.separator");
String texting = objShipmentItem.getSupplierName() + newline + objShipmentItem.getSupplierAddress().toString().trim() ;
PdfPCell cell = new PdfPCell(new Phrase(texting));
cell.setFixedHeight(40f);
cell.setColspan(10);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPadding(5.0f);
table.addCell(cell);
// table.addCell(code128Image);
cell = new PdfPCell(new Phrase("Item Description: " + objShipmentItem.getItemDescription()));
cell.setPadding(5.0f);
cell.setFixedHeight(40f);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Quantity : " + objShipmentItem.getQuantity()));
cell.setPadding(5.0f);
cell.setFixedHeight(40f);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Country Of Origin : " + objShipmentItem.getCountryOfOrigin()));
cell.setPadding(5.0f);
cell.setFixedHeight(40f);
table.addCell(cell);
cell = new PdfPCell(new Phrase("PO #: " + objShipmentItem.getPoNumber()));
cell.setPadding(5.0f);
cell.setFixedHeight(40f);
table.addCell(cell);
cell = new PdfPCell(new Phrase("PO Line #: " + objShipmentItem.getPoLine()));
cell.setPadding(5.0f);
cell.setFixedHeight(40f);
table.addCell(cell);
// String itemtext = "Item #: " + objShipmentItem.getItemNumber() + newline + code128Image ;
// document.add(new Paragraph("Item #: "));
// cell = new PdfPCell(new Phrase(itemtext));
cell.setPadding(5.0f);
cell.setFixedHeight(40f);
table.addCell(code128Image);
cell = new PdfPCell(new Phrase("Lot Serial #: "));
cell.setPadding(5.0f);
cell.setFixedHeight(40f);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Lot Expiration Date: " + sdf.format(objShipmentItem.getLotExpDate())));
cell.setPadding(5.0f);
cell.setFixedHeight(40f);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Shipment #: "+ objShipmentItem.getShipmentNumber()));
cell.setPadding(5.0f);
cell.setFixedHeight(40f);
table.addCell(cell );
table.addCell(" ");
document.add(table);
document.close(); // no need to close PDFwriter?
}catch (DocumentException e) {
e.printStackTrace();
}catch (FileNotFoundException e) {
e.printStackTrace();
}
catch(Exception E){
E.printStackTrace();
}
}
任何人都可以帮助我以正确的格式显示条形码
BarCode class(和 BarCode128
扩展 BarCode
),有一个 setBaseLine
方法。
它指出提供负值会将文本置于条形上方:
public void setBaseline(float baseline)
Sets the text baseline. If positive, the text distance under the bars.
If zero or negative, the text distance above the bars.
请查看 BarcodeInTable 示例。在本例中,我们以两种不同的方式添加条形码:
在第一种方式中,我们使用带负值的setBaseLine()
方法来确保将代码添加到条形码上方而不是条形码下方:
Barcode128 code128 = new Barcode128();
code128.setBaseline(-1);
code128.setSize(12);
code128.setCode(code);
code128.setCodeType(Barcode128.CODE128);
Image code128Image = code128.createImageWithBarcode(cb, null, null);
PdfPCell cell = new PdfPCell(code128Image);
table.addCell(cell);
请注意,我删除了 setX()
方法并定义了不同的字体大小以使条形码看起来正确。我看到您还定义了条形高度,但是您添加条形码的方式会缩放以匹配单元格,因此您可能希望使用不同的策略来创建条形码单元格。
这种方法有一些缺点。第一个缺点是您必须调整一些尺寸参数才能获得好的结果(也许还可以使用 setTextAlignment()
来更改默认对齐方式)。第二个缺点是您可能希望对要添加的文本有更多的自由。例如:您可能想要添加 PO #:
就像在您的示例中所做的那样。
这就是为什么我还提供了第二种添加文本和条形码的方法:
code128 = new Barcode128();
code128.setFont(null);
code128.setCode(code);
code128.setCodeType(Barcode128.CODE128);
code128Image = code128.createImageWithBarcode(cb, null, null);
cell = new PdfPCell();
cell.addElement(new Phrase("PO #: " + code));
cell.addElement(code128Image);
table.addCell(cell);
在这种情况下,我们将字体设置为null
,这样就不会生成文本。我们将文本与条形码一起添加到单元格中。现在我们可以更自由地按照我们想要的方式来格式化文本。
code128Image
已缩放以适合单元格的宽度,但您可以通过设置图像的宽度百分比来更改此设置,或者您可以更改列的宽度。
我正在使用 iText 动态生成 PDF docs.I 我能够生成 pdf,但我能够以下面提到的格式打印条形码
但需要以下面提到的格式打印条形码
条形码生成和PDF的代码return是:
public void generateShipmentItemPDF(ShipmentItemBarcodeVO objShipmentItem) {
Document document = new Document();
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
ec.responseReset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
PdfWriter docWriter = null;
String fileName =
"ShipmentItem_PO# " + objShipmentItem.getPoNumber() + "-" + "Line# " + objShipmentItem.getPoNumber() + "-" + "Item# " + objShipmentItem.getItemNumber().trim() + ".pdf";
LOGGER.info("fileName-: " + fileName);
ec.setResponseHeader("Content-Disposition",
"attachment; filename=\"" + fileName +
"\""); // The Save As popup magic is done here. You can give it any file name you want, this only won't work in MSIE, it will use current request URL as file name instead.
try{
docWriter = PdfWriter.getInstance(document, new FileOutputStream("D:\" + fileName));
document.open();
// document.add(new Paragraph("A Hello World PDF document."));
// document.newPage();
// document.add(new Paragraph("A New Page Hello World PDF document."));
PdfContentByte cb = docWriter.getDirectContent();
Barcode128 code128 = new Barcode128();
code128.setBarHeight(5f);
code128.setX(0.1f);
code128.setCode(objShipmentItem.getItemNumber());
code128.setCodeType(Barcode128.CODE128);
Image code128Image = code128.createImageWithBarcode(cb, null, null);
// code128Image.setAbsolutePosition(10,500);
// code128Image.scalePercent(50);
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(100);
// table.setSpacingBefore(100f);
// table.setSpacingAfter(100f);
// first row Print
String newline = System.getProperty("line.separator");
String texting = objShipmentItem.getSupplierName() + newline + objShipmentItem.getSupplierAddress().toString().trim() ;
PdfPCell cell = new PdfPCell(new Phrase(texting));
cell.setFixedHeight(40f);
cell.setColspan(10);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPadding(5.0f);
table.addCell(cell);
// table.addCell(code128Image);
cell = new PdfPCell(new Phrase("Item Description: " + objShipmentItem.getItemDescription()));
cell.setPadding(5.0f);
cell.setFixedHeight(40f);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Quantity : " + objShipmentItem.getQuantity()));
cell.setPadding(5.0f);
cell.setFixedHeight(40f);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Country Of Origin : " + objShipmentItem.getCountryOfOrigin()));
cell.setPadding(5.0f);
cell.setFixedHeight(40f);
table.addCell(cell);
cell = new PdfPCell(new Phrase("PO #: " + objShipmentItem.getPoNumber()));
cell.setPadding(5.0f);
cell.setFixedHeight(40f);
table.addCell(cell);
cell = new PdfPCell(new Phrase("PO Line #: " + objShipmentItem.getPoLine()));
cell.setPadding(5.0f);
cell.setFixedHeight(40f);
table.addCell(cell);
// String itemtext = "Item #: " + objShipmentItem.getItemNumber() + newline + code128Image ;
// document.add(new Paragraph("Item #: "));
// cell = new PdfPCell(new Phrase(itemtext));
cell.setPadding(5.0f);
cell.setFixedHeight(40f);
table.addCell(code128Image);
cell = new PdfPCell(new Phrase("Lot Serial #: "));
cell.setPadding(5.0f);
cell.setFixedHeight(40f);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Lot Expiration Date: " + sdf.format(objShipmentItem.getLotExpDate())));
cell.setPadding(5.0f);
cell.setFixedHeight(40f);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Shipment #: "+ objShipmentItem.getShipmentNumber()));
cell.setPadding(5.0f);
cell.setFixedHeight(40f);
table.addCell(cell );
table.addCell(" ");
document.add(table);
document.close(); // no need to close PDFwriter?
}catch (DocumentException e) {
e.printStackTrace();
}catch (FileNotFoundException e) {
e.printStackTrace();
}
catch(Exception E){
E.printStackTrace();
}
}
任何人都可以帮助我以正确的格式显示条形码
BarCode class(和 BarCode128
扩展 BarCode
),有一个 setBaseLine
方法。
它指出提供负值会将文本置于条形上方:
public void setBaseline(float baseline)
Sets the text baseline. If positive, the text distance under the bars. If zero or negative, the text distance above the bars.
请查看 BarcodeInTable 示例。在本例中,我们以两种不同的方式添加条形码:
在第一种方式中,我们使用带负值的setBaseLine()
方法来确保将代码添加到条形码上方而不是条形码下方:
Barcode128 code128 = new Barcode128();
code128.setBaseline(-1);
code128.setSize(12);
code128.setCode(code);
code128.setCodeType(Barcode128.CODE128);
Image code128Image = code128.createImageWithBarcode(cb, null, null);
PdfPCell cell = new PdfPCell(code128Image);
table.addCell(cell);
请注意,我删除了 setX()
方法并定义了不同的字体大小以使条形码看起来正确。我看到您还定义了条形高度,但是您添加条形码的方式会缩放以匹配单元格,因此您可能希望使用不同的策略来创建条形码单元格。
这种方法有一些缺点。第一个缺点是您必须调整一些尺寸参数才能获得好的结果(也许还可以使用 setTextAlignment()
来更改默认对齐方式)。第二个缺点是您可能希望对要添加的文本有更多的自由。例如:您可能想要添加 PO #:
就像在您的示例中所做的那样。
这就是为什么我还提供了第二种添加文本和条形码的方法:
code128 = new Barcode128();
code128.setFont(null);
code128.setCode(code);
code128.setCodeType(Barcode128.CODE128);
code128Image = code128.createImageWithBarcode(cb, null, null);
cell = new PdfPCell();
cell.addElement(new Phrase("PO #: " + code));
cell.addElement(code128Image);
table.addCell(cell);
在这种情况下,我们将字体设置为null
,这样就不会生成文本。我们将文本与条形码一起添加到单元格中。现在我们可以更自由地按照我们想要的方式来格式化文本。
code128Image
已缩放以适合单元格的宽度,但您可以通过设置图像的宽度百分比来更改此设置,或者您可以更改列的宽度。