HTML to PDF using iText, formatting issues - java

I'm working with iText in java to write PDF files. I'm trying to write a paragraph like heading and then text start very after the heading in the same line like
Heading: this a para now ...
Heading is bold and para is in normal text but I'm unable to do this using iText. I tried to use:
fonts[2] = new Font(Font.HELVETICA, 8, Font.BOLD);
Paragraph paranumber = new Paragraph(
fonts[2].getCalculatedLeading(1),
headingText.trim()
+ " ", fonts[0]);
Paragraph para = new Paragraph(
fonts[0].getCalculatedLeading(1), contentText.trim(), fonts[0]);
para.setAlignment(Element.ALIGN_JUSTIFIED);
para.setSpacingAfter(3f);
//Now adding the para to paraNumber that is having the heading and expecting
//that it will be added very after the heading, but this does not show correct
//result, formatting issue.
paranumber.add(para);
mct.addElement(paranumber);
I also tried to create a new paragraph and added both paras(heading para and normal text para) to that new one, but that is also not showing proper result. please see below chunk for that.
Paragraph newPara = new Paragraph();
newPara.add(paranumber);
newPara.add(para);
but this also not show proper formatting.
Or if anyone can advise me to use some other way to create PDF from HTML that will be good too, so that i may rewrite the module to create required PDF. Please advise.

Paragraphs typically use concepts like indentation and increased leading to set them apart visually. They are block level elements, not inline.
It doesn't make sense to add a paragraph inside another paragraph. The added paragraph would typically start on a new line, essentially making it a separate paragraph anyway.
To get a paragraph with different fonts, like your example, you can use Chunks in iText. A Chunk is basically a piece of text with an associated font.
Font fontbold = new Font(BaseFont.createFont(BaseFont.HELVETICA_BOLD,
BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 12);
Font fontregular = new Font(BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 12);
Chunk header = new Chunk("Heading: ", fontbold);
Chunk content = new Chunk("this is a para now ...", fontregular);
Paragraph paragraph = new Paragraph();
paragraph.add(header);
paragraph.add(content);
document.add(paragraph);
The result looks like this:
It's not clear from your question and code sample how HTML is involved. I assume you are somehow parsing HTML input and converting the parsed content to PDF using iText Elements. This is a valid approach. Alternatively, you can look into iText XML Worker, which does XHTML (+CSS) to PDF conversion.

Related

Html to docx with Docx4j break table when page skip

I using docx4j to generated a docx from html string, works great but the tables broken when the page skip.
This the code.
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
//File is a htmlStringFile
wordMLPackage.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert(file, null) );
File fileDos = new File(urlWord);
I´m using a docx4j 8.3.2 with Java 8.
Somebody know how to change all properties in the general document ?
Example : space between paragraph, space after the line,
Another problem, in some case the table cell add diferent space between cell. I don´t know why

PDF Box flatten PDF causes weird spacing

I'm having an issue with PDF box flattening a PDF generated by Adobe Acrobat DC.
The Adobe Acrobat text field I created is absolutely the default text field.
In my example below, I have a PatientName field with the text value "Douglas McDouggelman".
When I flatten the PDF, here's what it looks like:
Anyone know what's up with this bizarre spacing?
It appears that the space + next character are combined. This is what it looks like when you try to select that character.
Code:
try (PDDocument document = PDDocument.load(pdfFormInputStream)) {
PDDocumentCatalog catalog = document.getDocumentCatalog();
PDAcroForm acroForm = catalog.getAcroForm();
acroForm.getField("PatientName").setValue("Douglas McDouggelman");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
if (flattenPdfs) {
acroForm.flatten();
}
document.save(byteArrayOutputStream);
}
I realized this PDF was from some other group who made it and who knows what they did. So I found the source word document, repeated the creation of the form from Adobe DC, added the fields back to the document, then it was totally fine.
PDF box was not the problem... it was some unknown incorrect step that the person who originally prepared the pdf did.

How do i add heading level tags in structure tree for pdf?

I am using the below code to add heading level tags for the screen reader to read:
Paragraph para = new Paragraph("A guide to key terms").setFontSize(18).setBold();
para.getAccessibilityProperties().setRole(StandardRoles.H1);
document.add(para);
But I am getting the following error:
incompatible types : Paragraph cannot be converted to element.
I was able to add heading level tags by setting the role in chunk and then adding it to paragraph.
Chunk heading = new Chunk("A guide to key terms\n " , font);
heading.setRole(PdfName.H1);
Paragraph paragraph = new Paragraph(new Chunk(heading));
document.add(paragraph);

showing emoji in pdf or excel

I have the data containing emoji in database. I want to display in the generated document such as pdf or in excel format.
I am using spring boot application. Please suggest any java library for generating either PDF or excel which supports emoji.
iText supports this. Assuming
your emoji is a unicode character
you use a font that contains the correct glyph for this unicode character
Best way to test this is to try it.
This is how to get started with iText:
https://developers.itextpdf.com/content/itext-7-jump-start-tutorial/installing-itext-7
And this is a small code-snippet that adds text to a document with different fonts:
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf);
PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
PdfFont bold = PdfFontFactory.createFont(FontConstants.TIMES_BOLD);
Text title =
new Text("The Strange Case of Dr. Jekyll and Mr. Hyde").setFont(bold);
Text author = new Text("Robert Louis Stevenson").setFont(font);
Paragraph p = new Paragraph().add(title).add(" by ").add(author);
document.add(p);
document.close();
For more information check out the tutorials.
https://developers.itextpdf.com/content/itext-7-building-blocks/chapter-1

How to increase the width of PdfPTable in itext Pdf

I am trying to export records from database into pdf using itext pdf library in java..But i am getting following problems in alignment of pdf table inside pdf file..
1.Table is not showing in the full pdf page .It is leaving spaces from left and right of the pdf page.
2.Every page is showing values in half of the page only .Means pdf table is showing in half of the pdf pages..
Here is my code..
Document document = new Document();
PdfWriter.getInstance(document, fos);
PdfPTable table = new PdfPTable(10);
table.setWidthPercentage(100);
table.setSpacingBefore(0f);
table.setSpacingAfter(0f);
PdfPCell cell = new PdfPCell(new Paragraph("DateRange"));
cell.setColspan(10);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPadding(5.0f);
cell.setBackgroundColor(new BaseColor(140, 221, 8));
table.addCell(cell);
table.addCell("Calldate");
table.addCell("Calltime");
table.addCell("Source");
table.addCell("DialedNo");
table.addCell("Extension");
table.addCell("Trunk");
table.addCell("Duration");
table.addCell("Calltype");
table.addCell("Callcost");
table.addCell("Site");
while (rs.next()) {
table.addCell(rs.getString("date"));
table.addCell(rs.getString("time"));
table.addCell(rs.getString("source"));
table.addCell(rs.getString("destination"));
table.addCell(rs.getString("extension"));
table.addCell(rs.getString("trunk"));
table.addCell(rs.getString("dur"));
table.addCell(rs.getString("toc"));
table.addCell(rs.getString("callcost"));
table.addCell(rs.getString("Site"));
}
table.setSpacingBefore(5.0f); // Space Before table starts, like margin-top in CSS
table.setSpacingAfter(5.0f); // Space After table starts, like margin-Bottom in CSS
document.open();//PDF document opened........
document.add(Chunk.NEWLINE); //Something like in HTML :-)
document.add(new Paragraph("TechsoftTechnologies.com"));
document.add(new Paragraph("Document Generated On - " + new Date().toString()));
document.add(table);
document.add(Chunk.NEWLINE); //Something like in HTML :-)
document.newPage(); //Opened new page
//In the new page we are going to add list
document.close();
fos.close();
I had to read your question multiple times before I understood that you wanted to suppress the margins. I copy/pasted (and adapted) your example, and I thought I couldn't reproduce your problem because I (and many other developers) am used to the fact that pages have margins.
Anyway, this is my version of your example: FullPageTable and it creates the following PDF: full_page_table.pdf
I've made some minor changes, for instance: it doesn't make sense to pass a ´Paragraph´ as a parameter for a PdfPCell because that Paragraph will be treated as a Phrase, but I think the line you're looking for is:
Document document = new Document(PageSize.A4, 0, 0, 0, 0);
The zeros define the width of the margin, and if I understand your question correctly, you want to suppress those margins.
As for your allegation Every page is showing values in half of the page only .Means pdf table is showing in half of the pdf pages, I think you're causing that yourself by introducing document.newPage(); otherwise your allegation doesn't make sense ;-)

Categories

Resources