-
Notifications
You must be signed in to change notification settings - Fork 613
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7327d2f
commit 93f9417
Showing
6 changed files
with
195 additions
and
7 deletions.
There are no files selected for viewing
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
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
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
52 changes: 52 additions & 0 deletions
52
pdf-toolbox/src/test/java/com/lowagie/examples/html/ParseHelloHtml.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* $Id: HelloHtml.java 3373 2008-05-12 16:21:24Z xlv $ | ||
* | ||
* This code is part of the 'OpenPDF Tutorial'. | ||
* You can find the complete tutorial at the following address: | ||
* https://github.com/LibrePDF/OpenPDF/wiki/Tutorial | ||
* | ||
* This code is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* | ||
* | ||
*/ | ||
|
||
package com.lowagie.examples.html; | ||
|
||
import com.lowagie.text.Document; | ||
import com.lowagie.text.DocumentException; | ||
import com.lowagie.text.html.HtmlParser; | ||
import com.lowagie.text.pdf.PdfWriter; | ||
|
||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Generates a simple 'Hello World' HTML page. | ||
* | ||
* @author blowagie | ||
*/ | ||
|
||
public class ParseHelloHtml { | ||
|
||
/** | ||
* Generates an HTML page with the text 'Hello World' | ||
* | ||
* @param args no arguments needed here | ||
*/ | ||
public static void main(String[] args) { | ||
System.out.println("Parse Hello World"); | ||
|
||
// step 1: creation of a document-object | ||
try (Document document = new Document()) { | ||
PdfWriter.getInstance(document, new FileOutputStream("parseHelloWorld.pdf")); | ||
// step 2: we open the document | ||
document.open(); | ||
// step 3: parsing the HTML document to convert it in PDF | ||
HtmlParser.parse(document, ParseHelloHtml.class.getClassLoader().getResourceAsStream("com/lowagie/examples/html/parseHelloWorld.html")); | ||
} catch (DocumentException | IOException de) { | ||
System.err.println(de.getMessage()); | ||
} | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
pdf-toolbox/src/test/java/com/lowagie/examples/html/ParseNestedHtmlList.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* $Id: HelloHtml.java 3373 2008-05-12 16:21:24Z xlv $ | ||
* | ||
* This code is part of the 'OpenPDF Tutorial'. | ||
* You can find the complete tutorial at the following address: | ||
* https://github.com/LibrePDF/OpenPDF/wiki/Tutorial | ||
* | ||
* This code is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* | ||
* | ||
*/ | ||
|
||
package com.lowagie.examples.html; | ||
|
||
import com.lowagie.text.DocumentException; | ||
import com.lowagie.text.Element; | ||
import com.lowagie.text.html.simpleparser.HTMLWorker; | ||
import com.lowagie.text.html.simpleparser.StyleSheet; | ||
|
||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Generates a simple 'Hello World' HTML page. | ||
* | ||
* @author blowagie | ||
*/ | ||
|
||
public class ParseNestedHtmlList { | ||
|
||
/** | ||
* Generates an HTML page with the text 'Hello World' | ||
* | ||
* @param args no arguments needed here | ||
*/ | ||
public static void main(String[] args) { | ||
System.out.println("Parse Nested HTML List"); | ||
try { | ||
final String htmlText = | ||
"<html>" | ||
+ "<body>" | ||
+ "<p>What should you say?</p>" | ||
+ "<ul>" | ||
+ " <li>Hello</li>" | ||
+ " <li>World</li>" | ||
+ "</ul>" | ||
+ "<ol>" | ||
+ " <li>Element-1" | ||
+ " <ol>" | ||
+ " <li>Element-1-1</li>" | ||
+ " <li>Element-1-2</li>" | ||
+ " </ol>" | ||
+ " </li>" | ||
+ " <li>Element-2" | ||
+ " <ol>" | ||
+ " <li>Element-2-1" | ||
+ " <ol>" | ||
+ " <li>Element-2-1-1" | ||
+ " <ol>" | ||
+ " <li>Element-2-1-1-1</li>" | ||
+ " <li>Element-2-1-1-2</li>" | ||
+ " </ol>" | ||
+ " </li>" | ||
+ " <li>Element-2-1-2" | ||
+ " <ol>" | ||
+ " <li>Element-2-1-2-1</li>" | ||
+ " <li>Element-2-1-2-2</li>" | ||
+ " </ol>" | ||
+ " </li>" | ||
+ " </ol>" | ||
+ " </li>" | ||
+ " <li>Element-2-2</li>" | ||
+ " </ol>" | ||
+ " </li>" | ||
+ "</ol>" | ||
+ "</body>" | ||
+ "</html>"; | ||
|
||
final StringReader reader = new StringReader(htmlText); | ||
final StyleSheet styleSheet = new StyleSheet(); | ||
final Map<String, Object> interfaceProps = new HashMap<>(); | ||
|
||
final List<Element> elements = HTMLWorker.parseToList(reader, styleSheet, interfaceProps); | ||
printElement("", elements); | ||
|
||
} catch (DocumentException | IOException de) { | ||
System.err.println(de.getMessage()); | ||
} | ||
} | ||
|
||
private static void printElement(String depth, List<Element> elements) { | ||
for (Element element : elements) { | ||
System.out.println(depth + "- element.getClass() = " + element.getClass()); | ||
if (element instanceof com.lowagie.text.List) { | ||
com.lowagie.text.List elementList = (com.lowagie.text.List) element; | ||
printElement(depth + " ", elementList.getItems()); | ||
} else { | ||
System.out.println(depth + " element = " + element.getChunks().get(0).toString()); | ||
} | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
pdf-toolbox/src/test/resources/com/lowagie/examples/html/parseHelloWorld.html
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<html> | ||
<body> | ||
<p>What should you say?</p> | ||
<ul> | ||
<li>Hello</li> | ||
<li>World</li> | ||
</ul> | ||
<ol> | ||
<li>Element-1 | ||
<ol> | ||
<li>Element-1-1</li> | ||
<li>Element-1-2</li> | ||
</ol> | ||
</li> | ||
<li>Element-2 | ||
<ol> | ||
<li>Element-2-1</li> | ||
<li>Element-2-2</li> | ||
</ol> | ||
</li> | ||
</ol> | ||
</body> | ||
</html> |