I have a simple piece of code that splits a sentence (named content in the code) into tokens and then writes tokens in pdf file, but each in new line.
Document document = new Document(PageSize.A4, 20, 20, 20, 20);
PdfWriter.getInstance(document, new FileOutputStream("ticket.pdf"));
document.open();
Font font = FontFactory.getFont(FontFactory.COURIER, 16, BaseColor.BLACK);
String[] tokens = content.split("\\|");
for (String token : tokens) {
Chunk chunk = new Chunk(token, font);
document.add(chunk);
document.add(Chunk.NEWLINE);
}
document.close();
But when I open the result pdf file, all of the tokens (chunks) are written over each other in the first line, like this:
Can anyone point to me what am I doing wrong here? Code is written in Spring Boot with Java 11, and iText version is 5.5.10
Related
I generate a PDF document with itext. Document has two part.First part should have different footer, second part should have different footer.How can I achieve this problem.I already try this code blocks:
ByteArrayOutputStream fileOut = new ByteArrayOutputStream();
Document document = new Document(PageSize.A4, 36, 36, 36, 145);
FooterPageEvent1 event1 = new FooterPageEvent1();
PdfWriter writer = PdfWriter.getInstance(document, fileOut);
writer.setPageEvent(event1);
document.open();
.....
FooterPageEvent2 event2 = new FooterPageEvent2();
PdfWriter writer2 = PdfWriter.getInstance(document, fileOut);
writer2.setPageEvent(event2);
....
Your approach won't work as each time you create a new PdfWriter, a new pdf file is started. So you have two pdf writers creating separate pdfs which both are writing to the same file. Thus, you get a hodgepodge as output and are lucky if the result can be opened as a pdf at all!
Instead you can either switch page event listeners of the single writer at some time:
Document document = new Document(PageSize.A4, 36, 36, 36, 145);
FooterPageEvent1 event1 = new FooterPageEvent1();
PdfWriter writer = PdfWriter.getInstance(document, fileOut);
writer.setPageEvent(event1);
document.open();
.....
writer.setPageEvent(null);
FooterPageEvent2 event2 = new FooterPageEvent2();
writer.setPageEvent(event2);
.....
Or you can implement a single page event listener with a boolean property that depending on the current value of the property creates one or the other footer. Between document parts you would simply toggle that property.
I am generate Barcode128 with library iText-2.1.3. This is code which I am using:
private void createBarcode(String kodDokumentu, String idSadowka, String projekt) throws IOException, DocumentException
{
File barcodePdf = new File(pathToPdf);
Files.deleteIfExists(barcodePdf.toPath());
Document document = new Document();
Rectangle size = new Rectangle(151,60);
document.setMargins(5, 1, -6, 0);
document.setPageSize(size);
FileOutputStream fos = new FileOutputStream(pathToPdf);
PdfWriter writer = PdfWriter.getInstance(document, fos);
document.open();
PdfContentByte cb = writer.getDirectContent();
Barcode barcode128 = new Barcode128();
barcode128.setBarHeight(40);
barcode128.setX(1.04f);
barcode128.setCode("VL#"+kodDokumentu.toUpperCase());
barcode128.setCodeType(Barcode.CODE128);
Image code128Image = barcode128.createImageWithBarcode(cb, null, null);
Font code = new Font(FontFamily.TIMES_ROMAN, 8, Font.NORMAL, BaseColor.BLACK);
Paragraph p = new Paragraph("ID: "+idSadowka+", Projekt: "+projekt.substring(0, 2), code);
document.add(p);
document.add(code128Image);
document.close();
fos.close();
}
I want to achive as small h (take a look at image) as it is possible, best if h=0.01 because I want to save more place for ID: xxxxx, Projekt: xx to make it bigger and easier to read by human.
First (bottom barcode) I used font size 8, then (upper barcode) I tried to change font size to 10 but when I did it h is bigger than previous. I know that font size is connected with gap between barcode and text above it but is it possible to use bigger font size and set this gap really small?
Not sure if it's available in the version of iText you use, but on iText 5 at least, you have the option to set the "spacing after" on Paragraphs. It would be exactly what you need, i.e. you specify a fixed space under the paragraph, and any elements you add to the document after that paragraph, will go under that space.
p.setSpacingAfter(x)
Where x being the space you need, in user units or "points".
I'm using iText to create barcodes on a PDF with the same format as this one:
The problem is the the left number, the first zero digits must be smaller, while the rest of the numbers must also be bold. "T.T.C." also has to be even smaller (it doesn't have to be on another line).
I was able to rotate the number with the following code:
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);
The problem is that I cannot find a way online to change the font of certain characters of the String price when it is printed on the PDF.
I have created a small Proof of Concept that results in a PDF that looks like this:
As you can see, it has text in different sizes and styles. It also has a bar code that is rotated.
Take a look at the RotatedText example:
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();
}
This example solves all of your issues:
You want a Paragraph to use different fonts: compose a Paragraph using different Chunk objects.
You want to add extra text on top of a bar code: add the bar code to a PdfTemplate and add the extra text using ColumnText.showTextAligned() (not that you can also compose a Phrase using different Chunk objects if you need more than one font in that extra text).
You want to rotate the bar code: wrap the PdfTemplate inside an Image object and rotate the image.
You can check the result: rotated_text.pdf
I hope this helps.
I use the following code to display a line in a PDF file using iText:
Phrase phraseHeader = new Phrase(18, new Chunk("Registration Form "+registrationForm.getRegistrationDate(), FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD)));
Paragraph paragraph=new Paragraph(phraseHeader);
paragraph.setAlignment("center");
document.add(paragraph);
If I run this code the pdf file will contain the following:
Registration Form 26-Apr-2013 11:58:20
I want to display the "Registration Form" in a larger font and the date/time in a smaller font, but both should be in the same line. How can I am do this?
This should do the trick:
Phrase phraseHeader = new Phrase();
phraseHeader.add(
new Chunk("Registration Form ",
FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD)));
phraseHeader.add(
new Chunk(registrationForm.getRegistrationDate(),
FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD)));
Paragraph paragraph = new Paragraph(phraseHeader);
I am parsing some html code using HTMLWorker in Java and then inserting it to PDF using iText. I create document by calling new Document(PageSize.A4, 40, 40, 40, 40); which should specify margin 40px on all sides, but when I insert parsed html code that contains table wider than page, right margin dont work and table reaches right border of page... All margins are ok except the right one.. any suggestions?
relevant code:
Document document = new Document(PageSize.A4, 40, 40, 40, 40);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(HTMLWorker.FONT_PROVIDER, new MyFontFactory10());
HTMLWorker worker = new HTMLWorker(document);
worker.setProviders(map);
worker.parse(new StringReader(VARIABLE_CONTAINING_HTML_CODE));
It should work.
Can you provide info about iText version and how the HTML code looks like.
Images can make the table expand beyond margins (it´s not pixels by the way).
Try this code and see if the problem still remains. The table should not expand beyond the page margins. Tables used with HTMLWorker always get a width of 100% of its container.
Document document = new Document(PageSize.A4, 40, 40, 40, 40);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:/TEST.pdf"));
document.open();
HTMLWorker worker = new HTMLWorker(document);
String code = "<table border=1><tr><td>Test</td><td>Test</td></tr></table>";
worker.parse(new StringReader(code));
document.close();