How can I display different barcodes in multiple columns in a PDF page using itext library to generate pdfs in java? I have to display 12 barcodes in the same PDF page in three columns, each one contains 4 barcodes (in other words it is a 4 by 3 matrix).
I've made a Barcodes example that does exactly what you need. See the resulting pdf: barcodes_table.pdf
There's nothing difficult about it. You just create a table with 4 column and you add 12 cell:
PdfPTable table = new PdfPTable(4);
table.setWidthPercentage(100);
for (int i = 0; i < 12; i++) {
table.addCell(createBarcode(writer, String.format("%08d", i)));
}
The createBarcode() method creates a cell with a barcode:
public static PdfPCell createBarcode(PdfWriter writer, String code) throws DocumentException, IOException {
BarcodeEAN barcode = new BarcodeEAN();
barcode.setCodeType(Barcode.EAN8);
barcode.setCode(code);
PdfPCell cell = new PdfPCell(barcode.createImageWithBarcode(writer.getDirectContent(), BaseColor.BLACK, BaseColor.GRAY), true);
cell.setPadding(10);
return cell;
}
Related
I am trying to convert PDF file to CSV or EXCEL format.
Here is the code I use to convert to CSV format:
public void convert() throws Exception {
PdfReader pdfReader = new PdfReader("example.pdf");
PdfDocument pdf = new PdfDocument(pdfReader);;
int pages = pdf.getNumberOfPages();
FileWriter csvWriter = new FileWriter("student.csv");
for (int i = 1; i <= pages; i++) {
PdfPage page = pdf.getPage(i);
String content = PdfTextExtractor.getTextFromPage(page);
String[] splitContents = content.split("\n");
boolean isTitle = true;
for (int j = 0; j < splitContents.length; j++) {
if (isTitle) {
isTitle = false;
continue;
}
csvWriter.append(splitContents[j].replaceAll(" ", " "));
csvWriter.append("\n");
}
}
csvWriter.flush();
csvWriter.close();
}
This code works correctly, but the fact is that the CSV format groups rows without taking into account existing columns (some of them are empty), so I would like to convert this file (PDF) to EXCEL format.
The PDF file itself is formed as a table.
What do I mean about spaces. For example, in a PDF file, in a table
| name | some data | | | some data 1 | |
+----------+----------------+------------+-------------+-------------------+--------------+
After converting to a CSV file, the line looks like this:
name some data some data 1
How can I get the same result as a PDF table?
I'd suggest to use PDFBox, like here: Parsing PDF files (especially with tables) with PDFBox
or another library that will allow you to check the data in the Table point by point, and will allow you to create a table by column width (something like Table table = page.getTable(dividers)); ).
If the width of the columns changes, you'll have to implement it based on the headers/first data column ([e.g. position.x of the last character of the first word] minus [position.x of the first character of the new word] - you'll have to figure it out yourself), it's hard so you could make it hardcoded in the beginning. Using Foxit Reader PDF App you can easily measure column width. Then, if you don't find any data in a particular column, you will be able to add an empty column in the CSV file. I know from my own experience that it is not easy, so I wish you good luck.
I have a java Swing application which generates barcode labels to be printed on Soho stickers. The stickers are then put on actual items, in my case it is books.
I have followed steps for generating 2D barcodes as describe in the book "iText in Action, 2nd Edition" page 334. I generate the barcodes in a PDF document and print, but it is the scanning that does not work. My scanner is E-POS, model EC301 and can scan other barcode labels very quickly. My code is as shown below:
private PdfPCell createBarcode(String code) throws DocumentException, IOException {
BarcodeEAN barcode = new BarcodeEAN();
barcode.setCodeType(Barcode.EAN13);
barcode.setCode(code);
PdfPCell cell = new PdfPCell(barcode.createImageWithBarcode(writer.getDirectContent(), BaseColor.BLACK, BaseColor.GRAY), true);
cell.setBorderWidth(0.1f);
cell.setPadding(10);
return cell;
}
To create a barcode, i call the function like this:
createBarcode(String.format("%013d", 1));
Where could I possibly be wrong?
This question is already asked but the query i have is not answered. i have a pdf with table in which some columns are not having any values. I need to read those blank spaces.
I have used Itext pdf for extracting data from pdf but while reading the data from table it is read col by col and the column having no value is not read with white spaces but the next column is read.
I have customized LocationTextExtractionStrategy and have overridden getResultantText()
In below image if there is no value for MD and TD col 1,2,3 then while reading the PDF after 1 it is not giving me spaces but giving the next value that is 2. Is there any solution for this to read the blank spaces
PdfReader reader = new PdfReader(filename);
FontRenderFilter fontFilter = new FontRenderFilter();
TextExtractionStrategy strategy = new FilteredTextRenderListener(new MyLocationTextExtractionStrategy(),fontFilter);
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
String finalText = PdfTextExtractor.getTextFromPage(reader, i, strategy);
System.out.println("finalText.." + finalText);
}
I'm using iText to create a PDF with a table. The table headers have 90 degree rotated text, which I'm adding using a CellEvent (code below). This works great, except when the table spans multiple pages the rotated cell header text flows off the top of the page.
I've tried setting cell.setFixedHeight(100) but it doesn't seem to affect the cell. I've tried this solution as well but I can't get the cell to display the resulting image with text at all.
#Override
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
try {
canvas.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, false), this.fontSize);
} catch (DocumentException | IOException e) {
e.printStackTrace();
}
if (this.alignment == PdfPCell.ALIGN_CENTER) {
this.left = ((position.getRight() - position.getLeft()) / 2 );
}
else if (this.alignment == PdfPCell.ALIGN_MIDDLE) {
this.top = ((position.getTop() - position.getBottom()) / 2 );
}
canvas.showTextAligned(this.alignment, this.text, position.getLeft() + this.left, position.getTop() - this.top, this.rotation);
}
Here's what the cell header overflow looks like. In this example it should have the month and year (Mar 2016) displayed.
I'd like to have the cell to be arbitrary height dependent on the actual header text being used. Any ideas on how to solve this?
A cell event is triggered after a cell is drawn. You might already have suspected that much as iText passes a Rectangle object with the position to the cellLayout method. A PdfPCell object is passed, but it is to be used for read-only purposes only. As the position is fixed, you can't use the setFixedHeight() on it.
Looking at the screen shot, I am puzzled: why are you using a cell event to add content that is rotated by 90 degrees? The solution to your problem would be to use the setRotation() method:
PdfPCell cell = new PdfPCell(new Phrase("May 16, 2016"));
cell.setRotation(90);
Now the content will be rotated and the size of the cell will be adapted to the content. Please take a look at the RotatedCell example:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(8);
for (int i = 0; i < 8; i++) {
PdfPCell cell =
new PdfPCell(new Phrase(String.format("May %s, 2016", i + 15)));
cell.setRotation(90);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
}
for(int i = 0; i < 16; i++){
table.addCell("hi");
}
document.add(table);
document.close();
}
The result looks like this: rotated_cell.pdf
Note that the concepts horizontal and vertical are rotated to. If you want to center the rotated content horizontally, you have to center the vertical alignment for the content and the rotate the aligned content.
I have an example below that generates a table with ten rows, the last row taking up two lines. The document height is two points smaller than it needs to be to correctly fit the entire table. As expected the last row on the first page contains the word "Two" and the "Lines" portion continues on another page. The generated PDF, however, extends the last row to fit the entire document, excluding page margin padding, instead of using the height of the text in the last row to determine how high it should be. Note that I use table.setExtendLastRow(false, false) to guarantee that the table shouldn't be extended, and yet the last row is getting extended anyway. Any ideas how to stop the last row from being extended?
FileOutputStream out = new FileOutputStream(new File("table.pdf"));
Document document = new Document(new Rectangle(612, 242));
PdfWriter.getInstance(document, out);
document.open();
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100);
table.setSplitLate(false);
table.setSplitRows(true);
table.setExtendLastRow(false, false);
for (int i = 0; i < 10; i++) {
PdfPCell cell;
if (i == 9) {
cell = new PdfPCell(new Phrase("Two\nLines"));
}
else {
cell = new PdfPCell(new Phrase(Integer.toString(i)));
}
table.addCell(cell);
}
document.add(table);
document.close();
I've uploaded a picture of what I'm seeing. Notice the spacing under the word "Two" is different than the spacing under all the other numbers in the rows. It appears to extend to the bottom of the page margin.
This is expected behavior and iText doesn't currently have support for this edge case.