use of com.lowagie.text.Anchor in project OpenPDF by LibrePDF.
the class SAXiTextHandler method addImage.
protected void addImage(Image img) throws EmptyStackException {
// if there is an element on the stack...
Element current = stack.pop();
// added directly
if (current instanceof Section || current instanceof Cell) {
((TextElementArray) current).add(img);
stack.push(current);
} else // ... if it is a Phrase, we have to wrap the Image in a new Chunk
if (current instanceof Phrase) {
((TextElementArray) current).add(new Chunk(img, 0, 0));
stack.push(current);
} else // ...if not, we need to to a lot of stuff
{
Stack<Element> newStack = new Stack<>();
while (!(current instanceof Section || current instanceof Cell)) {
newStack.push(current);
if (current instanceof Anchor) {
img.setAnnotation(new Annotation(0, 0, 0, 0, ((Anchor) current).getReference()));
}
current = stack.pop();
}
((TextElementArray) current).add(img);
stack.push(current);
while (!newStack.empty()) {
stack.push(newStack.pop());
}
}
}
use of com.lowagie.text.Anchor in project OpenPDF by LibrePDF.
the class HtmlBookmarks method createBookmark.
/**
* Adds a line with the title and an anchor.
* @param pdf the link to the PDF file
* @param section the section that gets the line
* @param bookmark the bookmark that has the data for the line
* @return a subsection of section
*/
private static Section createBookmark(String pdf, Section section, Map<String, Object> bookmark) {
Section s;
Paragraph title = new Paragraph((String) bookmark.get("Title"));
System.out.println((String) bookmark.get("Title"));
String action = (String) bookmark.get("Action");
if ("GoTo".equals(action)) {
if (bookmark.get("Page") != null) {
String page = (String) bookmark.get("Page");
StringTokenizer tokens = new StringTokenizer(page);
String token = tokens.nextToken();
Anchor anchor = new Anchor(" page" + token);
anchor.setReference(pdf + "#page=" + token);
title.add(anchor);
}
} else if ("URI".equals(action)) {
String url = (String) bookmark.get("URI");
Anchor anchor = new Anchor(" Goto URL");
anchor.setReference(url);
title.add(anchor);
} else if ("GoToR".equals(action)) {
String remote = (String) bookmark.get("File");
Anchor anchor = new Anchor(" goto " + remote);
if (bookmark.get("Named") != null) {
String named = (String) bookmark.get("Named");
remote = remote + "#nameddest=" + named;
} else if (bookmark.get("Page") != null) {
String page = (String) bookmark.get("Page");
StringTokenizer tokens = new StringTokenizer(page);
String token = tokens.nextToken();
anchor.add(new Chunk(" page " + token));
remote = remote + "#page=" + token;
}
anchor.setReference(remote);
title.add(anchor);
}
if (section == null) {
s = new Chapter(title, 0);
} else {
s = section.addSection(title);
}
s.setNumberDepth(0);
return s;
}
use of com.lowagie.text.Anchor in project OpenPDF by LibrePDF.
the class HelloWorldPdf method testHelloWorld.
/**
* Generates simple PDF, RTF and HTML files using only one Document object.
*/
@Test
void testHelloWorld() throws Exception {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
// step 1: creation of a document-object
Document document = new Document();
// step 2:
// we create 3 different writers that listen to the document
PdfWriter pdf = PdfWriter.getInstance(document, baos);
// step 3: we open the document
document.open();
// step 4: we add a paragraph to the document
document.add(new Paragraph("Hello World"));
// we make references
Anchor pdfRef = new Anchor("see Hello World in PDF.");
pdfRef.setReference("./HelloWorldPdf.pdf");
Anchor rtfRef = new Anchor("see Hello World in RTF.");
rtfRef.setReference("./HelloWorldRtf.rtf");
// we add the references, but only to the HTML page:
pdf.pause();
document.add(pdfRef);
document.add(Chunk.NEWLINE);
document.add(rtfRef);
pdf.resume();
// step 5: we close the document
document.close();
assertNotEquals(0, baos.size());
}
}
use of com.lowagie.text.Anchor in project OpenPDF by LibrePDF.
the class JavaScriptAction method main.
/**
* Creates a document with Named Actions.
*
* @param args no arguments needed
*/
public static void main(String[] args) {
System.out.println("Open Application");
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
HtmlWriter.getInstance(document, new FileOutputStream("JavaScriptAction.html"));
// step 3: we add Javascript as Metadata and we open the document
StringBuilder javaScriptSection = new StringBuilder();
javaScriptSection.append("\t\tfunction load() {\n");
javaScriptSection.append("\t\t alert('Page has been loaded.');\n");
javaScriptSection.append("\t\t}\n");
javaScriptSection.append("\t\tfunction unload(){\n");
javaScriptSection.append("\t\t alert('Page has been unloaded.');\n");
javaScriptSection.append("\t\t}\n");
javaScriptSection.append("\t\tfunction sayHi(){\n");
javaScriptSection.append("\t\t alert('Hi !!!');\n");
javaScriptSection.append("\t\t}");
document.add(new Header(HtmlTags.JAVASCRIPT, javaScriptSection.toString()));
document.setJavaScript_onLoad("load()");
document.setJavaScript_onUnLoad("unload()");
document.open();
// step 4: we add some content
Phrase phrase1 = new Phrase("There are 3 JavaScript functions in the HTML page, load(), unload() and sayHi().\n\n" + "The first one will be called when the HTML page has been loaded by your browser.\n" + "The second one will be called when the HTML page is being unloaded,\n" + "for example when you go to another page.\n");
document.add(phrase1);
// add a HTML link <A HREF="...">
Anchor anchor = new Anchor("Click here to execute the third JavaScript function.");
anchor.setReference("JavaScript:sayHi()");
document.add(anchor);
} catch (Exception de) {
de.printStackTrace();
}
// step 5: we close the document
document.close();
}
use of com.lowagie.text.Anchor in project OpenPDF by LibrePDF.
the class AHref method main.
/**
* Demonstrates some Anchor functionality.
*
* @param args no arguments needed here
*/
public static void main(String[] args) {
System.out.println("the Anchor object");
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
PdfWriter.getInstance(document, new FileOutputStream("AHref.pdf"));
HtmlWriter.getInstance(document, new FileOutputStream("AHref.html"));
// step 3: we open the document
document.open();
// step 4:
Paragraph paragraph = new Paragraph("Please visit my ");
Anchor anchor1 = new Anchor("website (external reference)", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE, new Color(0, 0, 255)));
anchor1.setReference("https://github.com/LibrePDF/OpenPDF");
anchor1.setName("top");
paragraph.add(anchor1);
paragraph.add(new Chunk(".\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"));
document.add(paragraph);
Anchor anchor2 = new Anchor("please jump to a local destination", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 255)));
anchor2.setReference("#top");
document.add(anchor2);
} catch (DocumentException | IOException de) {
System.err.println(de.getMessage());
}
// step 5: we close the document
document.close();
}
Aggregations