0

Source:

writer = PdfWriter.getInstance(document, new FileOutputStream("D://MetaData.pdf"));
document.open();
StringBuffer sb = new StringBuffer(text);
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream("D:\\work\\iTextPDF\\sample1.html"));
PdfContentByte pcb = writer.getDirectContent();
ColumnText ct = new ColumnText(pcb);
ct.setAlignment(Element.ALIGN_JUSTIFIED);           
ct.setText(new Phrase(sb.toString()));
float[] left = { 36, (PageSize.A4.getWidth() / 2) + 18 };
float[] right = { (PageSize.A4.getWidth() / 2) - 18, PageSize.A4.getWidth() - 36 };
int status = ColumnText.NO_MORE_COLUMN;
int column = 0;
while (ColumnText.hasMoreText(status)) {
ct.setSimpleColumn(left[column], 36, right[column], PageSize.A4.getHeight() - 36);
                status = ct.go();
                column++;
                if (column > 1) {
                    column = 0;
                    document.newPage();
                }
            }

in my source code i am using XMLWorkerHelper and ColumnText but XMLWorkerHelper parse the data and write into document but i want parse the data from html and set to the ColumnText after build the document. please help me thank you in advance.

vijay velaga
  • 97
  • 1
  • 1
  • 9
  • I wanted to vote to close this question as a duplicate of [How to use itext to span 2 columnns of a page?](http://stackoverflow.com/questions/18939771) but I couldn't because that question was never accepted or upvoted, so I closed it using another question. In the answer to that question, you'll see thqt you can parse HTML to an `ElementList`: `ElementList list = XMLWorkerHelper.parseToElementList(html, css);` You can then loop over those elements and add them to a `ColumnText` object. – Bruno Lowagie Aug 25 '16 at 11:43
  • Read the answer and my comment before you comment yourself. It is rude to question an answer before checking why that answer is given. – Bruno Lowagie Aug 25 '16 at 11:48
  • `XMLWorkerHelper` contains no method like`parseToElementList(html, css)` – vijay velaga Aug 25 '16 at 11:50
  • Yes it does! Read the [API documentation](http://itextsupport.com/apidocs/itext5/latest/com/itextpdf/tool/xml/XMLWorkerHelper.html#parseToElementList-java.lang.String-java.lang.String-). If you are using an old version of iText, read [How to adjust the page height to the content height?](http://stackoverflow.com/questions/27186661) Avoid contradicting the person who wrote the software. I am the author of iText. If I say there is a method `parseToElementList(html, css)`, you shouldn't pretend you know better. If you knew better, you wouldn't ask any questions, would you? – Bruno Lowagie Aug 25 '16 at 11:54
  • I am sorry **Bruno Lowagie** i am using itextpdf-5.5.3. this version having not method like `parseToElementList(html, css)` new API having this method. **Thank You very much**. – vijay velaga Aug 25 '16 at 11:59
  • OK, in [this answer](http://stackoverflow.com/a/27211629/1622493), you will find a method `parseHtml` that returns an `ElementList`. That method can be used for versions older than 5.5.4. – Bruno Lowagie Aug 25 '16 at 12:06
  • Now i am using 5.5.8-versionCan you please explain in detail. regarding `parseToElementList(html, css)` how to pass 'html' and 'css' file – vijay velaga Aug 25 '16 at 12:36
  • That's a strange question. I don't understand your problem. You have some HTML as a `String` and you have some CSS as a `String`. You pass those `String`s and you get a list of elements. Read the [official documentation](http://developers.itextpdf.com/itext-search?keys=parseToElementList&field_itext_version_tid%5B%5D=909) and save yourself **and me** some time by trying some of the many examples that are available. – Bruno Lowagie Aug 25 '16 at 12:45
  • Ok. Thank you very much. – vijay velaga Aug 25 '16 at 12:51

0 Answers0