Created
May 19, 2016 07:37
-
-
Save syakovyn/8a28ab096bf5fa160f0baf0c6559a24a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.lowagie.text.Chunk; | |
import com.lowagie.text.Document; | |
import com.lowagie.text.DocumentException; | |
import com.lowagie.text.Element; | |
import com.lowagie.text.List; | |
import com.lowagie.text.ListItem; | |
import com.lowagie.text.Paragraph; | |
import com.lowagie.text.html.simpleparser.HTMLWorker; | |
import com.lowagie.text.html.simpleparser.StyleSheet; | |
import com.lowagie.text.pdf.PdfPCell; | |
import com.lowagie.text.pdf.PdfPTable; | |
import com.lowagie.text.pdf.PdfWriter; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.StringReader; | |
public class Main { | |
public static final String HTML = "<p>Food:</p>\n" + | |
"<ul>\n" + | |
"<li>Drink</li>\n" + | |
"<li>Fruit</li>\n" + | |
"<li>Vegetables</li>\n" + | |
"</ul>\n" + | |
"<p>Furniture:</p>\n" + | |
"<ul>\n" + | |
"<li>Table</li>\n" + | |
"<li>Chair</li>\n" + | |
"</ul>\n" + | |
"<p>Relatives:</p>\n" + | |
"<ul>\n" + | |
"<li>Sister</li>\n" + | |
"<li>Brother</li>\n" + | |
"<li>Nephew</li>\n" + | |
"</ul>"; | |
public static void main(String args[]) throws DocumentException, IOException { | |
FileOutputStream out = new FileOutputStream("test.pdf"); | |
Document document = new Document(); | |
PdfWriter pdfWriter = PdfWriter.getInstance(document, out); | |
document.open(); | |
PdfPTable table = getPdfPTable(); | |
document.add(table); | |
document.close(); | |
} | |
private static PdfPTable getPdfPTable() throws IOException { | |
PdfPTable table = new PdfPTable(1); | |
table.addCell(getGoodPdfPCell()); | |
table.addCell(getBadPdfPCell()); | |
table.addCell(getBestPdfPCell()); | |
return table; | |
} | |
/* | |
* Shows a case when an unordered list is parsed and add to a cell. | |
* The issue is no space between bullet and a text item. | |
*/ | |
private static PdfPCell getGoodPdfPCell() throws IOException { | |
PdfPCell cell = new PdfPCell(); | |
java.util.List<Element> list = HTMLWorker.parseToList(new StringReader(HTML), new StyleSheet()); | |
list.forEach(cell::addElement); | |
return cell; | |
} | |
/* | |
* Everything is fine. There is a space between a list item bullet (hyphen) and a text item. | |
*/ | |
private static PdfPCell getBestPdfPCell() throws IOException { | |
PdfPCell cell = new PdfPCell(); | |
java.util.List<Element> list = HTMLWorker.parseToList(new StringReader(HTML), new StyleSheet()); | |
list.forEach((element) -> { | |
if (element instanceof List) { | |
List newList = new List(); | |
((List) element).getItems().forEach((item) -> newList.add(new ListItem((Chunk) ((ListItem) item).get(0)))); | |
element = newList; | |
} | |
cell.addElement(element); | |
}); | |
return cell; | |
} | |
/* | |
* Shows a case when a paragraph containing lists is added to a cell. The issue is no bullets. | |
*/ | |
private static PdfPCell getBadPdfPCell() throws IOException { | |
PdfPCell cell = new PdfPCell(); | |
final Paragraph innerParagraph = new Paragraph(); | |
java.util.List<Element> list = HTMLWorker.parseToList(new StringReader(HTML), new StyleSheet()); | |
list.forEach(innerParagraph::add); | |
cell.addElement(innerParagraph); | |
return cell; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment