float property not working in HtmlWorker(iText) - java

I am trying to apply float property to a div in itext. The content is an HTML content
Document document = new Document(PageSize.A4, 36, 72, 108, 180);
PdfWriter.getInstance(document, servletOutputStream);
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
StyleSheet style = new StyleSheet();
style.loadStyle("class-name", "float", "left");
htmlWorker.setStyleSheet(style);
htmlWorker.parse(new StringReader(stringBuilder.toString()));
The above code doesn't work for float, but for other properties like size,color,etc.
What could be the possible solution.

HTMLWorker has been discontinued in favor of XML Worker.
The project :
http://sourceforge.net/projects/xmlworker/
The demo :
http://demo.itextsupport.com/xmlworker/
The documentation :
http://demo.itextsupport.com/xmlworker/itextdoc/index.html

Related

iText Chunks are written over each other

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

How To Set Different Footers For Different Pages In Itext Pdf Document

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.

Conversion of HTML( with inline css) to PDF using itext 2.1.7

I want to convert one html page to pdf using itext 2.1.7. I have used HTMLWorker to convert the html file, but it not taking the inline css which I have used in the html. Below is my code snippet . Can anyone help to fix this issue..
PdfWriter pdfWriter = PdfWriter.getInstance(document, new
FileOutputStream("D:/testpdf.pdf"));
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader(htmlContent));
document.close();
Thanks in Advance !
Use itext7-7.0.2 because iText 2.1.7 didn't support inline CSS.
String htmlContent = "<html><body style='color:red'> PDF project </body></html>";
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(new File("C:\\testpdf.pdf")));
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader(htmlContent));
document.close();

iText, order of creating and adding content

I am creating a PDF file, but have stumbled upon a problem. Below are the iText commands that are causing it.
document.Header = header;
document.Open();
PdfContentByte cb = writer.DirectContentUnder; (getDirectContentUnder() for java)
document is of type Document; writer is a PdfWriter and header is of type HeaderFooter.
iText have same conditions for order of those commands and those are:
PdfWriter cannot be used before opening the document
header of the PDF will not be shown at all, if I open the document before I add the header to it
That would be fine and I would write it in the order just like in the snippet above. But I need the PdfWriter for creating the header. And now I am in a loop:
to create the header, I need to use PdfWriter
to use PdfWriter, I need to open the document first
for the header to be visible, I would need to create it before opening the document
Also, I found sources both for recommending the header to be created as the last thing before opening the document, as well as for recommending the header to be created after the document has been opened. Which is right and how to solve my problem?
Update: Code
BaseFont bf16 = BaseFont.CreateFont( "c:\\windows\\fonts\\Arial.ttf", BaseFont.CP1250, false );
Font fnt10Bold = new Font( bf16, 10, Font.BOLD, new Color( 100, 100, 100 ) );
System.Drawing.Bitmap headerBitmap = Properties.Resources.doc_header;
Image headerImg = Image.GetInstance( headerBitmap, Color.BLACK );
Document document = new Document( PageSize.A4, 36, 40, 20, 20 );
PdfWriter writer = PdfWriter.GetInstance( document, new FileStream( path, FileMode.Create ) );
for ( int i = 0; i < DocList.Count; i++ )
{
PdfPTable tblHeader = new PdfPTable( new float[] { 40, 60 } );
tblHeader.WidthPercentage = 100;
PdfContentByte cb = writer.DirectContentUnder;
Image img = getWatermarkedImage( cb, headerImg, "Watermark", font10Bold );
PdfPCell cellHeader = new PdfPCell( img, true ) { Colspan = 2
HorizontalAlignment = Element.ALIGN_LEFT,
VerticalAlignment = Element.ALIGN_BOTTOM };
tblHeader.AddCell( cellHeader );
Phrase headerPhrase = new Phrase();
headerPhrase.Add( tblHeader );
document.Header = new HeaderFooter( headerPhrase, false );
if ( i == 0 )
document.Open();
else
document.NewPage();
}
document.Close();
getWatermarkedImage is the method from this answer by Bruno Lowagie.

HTML table in PDF using iText

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();

Categories

Resources