Java Tutorial/PDF/Jump
Содержание
Go to page 2
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDestination;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
document.add(new Paragraph("1"));
document.newPage();
document.add(new Paragraph("2"));
PdfContentByte cb = writer.getDirectContent();
cb.localDestination("page2", new PdfDestination(PdfDestination.XYZ, -1, 10000, 0));
writer.setOpenAction("page2");
document.close();
}
}
Link to a Pdf file
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter writerA = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
PdfWriter writerB = PdfWriter.getInstance(document, new FileOutputStream("3.pdf"));
document.open();
Font font = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE, new Color(0, 0, 255));
Paragraph p1 = new Paragraph("1");
Chunk chunk = new Chunk(" 2", font);
chunk.setRemoteGoto("dog.pdf", "2");
p1.add(chunk);
Paragraph p4 = new Paragraph("jump to a ");
p4.add(new Chunk("specific page on another document", font).setRemoteGoto("3.pdf", 3));
writerB.pause();
document.add(p1);
writerB.resume();
writerA.pause();
document.add(p4);
writerB.pause();
document.newPage();
document.add(new Paragraph("page 2"));
document.newPage();
document.add(new Paragraph("page 3"));
writerB.resume();
document.close();
}
}
Remote Goto
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter writerA = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
PdfWriter writerB = PdfWriter.getInstance(document, new FileOutputStream("3.pdf"));
document.open();
Font font = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE, new Color(0, 0, 255));
Paragraph p1 = new Paragraph("1");
Chunk chunk = new Chunk(" 2", font);
chunk.setRemoteGoto("dog.pdf", "2");
p1.add(chunk);
Paragraph p2 = new Paragraph("test");
writerB.pause();
document.add(p1);
writerB.resume();
document.add(p2);
writerA.resume();
writerB.pause();
document.newPage();
document.add(new Paragraph("page 2"));
document.newPage();
document.add(new Paragraph("page 3"));
writerB.resume();
document.close();
}
}
Set Local Destination for chunk
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
Paragraph p1 = new Paragraph("this is a test");
p1.add(new Chunk(" another test ", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL,
new Color(0, 0, 255))).setLocalGoto("jump"));
p1.add("the lazy dog.");
Paragraph p2 = new Paragraph("test");
Paragraph p3 = new Paragraph("test 2");
p3.add(new Chunk(" has jumped ", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL,
new Color(0, 255, 0))).setLocalDestination(" cat "));
p3.add(" dog.");
document.add(p1);
document.add(p2);
document.add(p2);
document.add(p2);
document.add(p2);
document.add(p2);
document.add(p2);
document.add(p2);
document.add(p3);
document.close();
}
}
Set up PdfDestination
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDestination;
import com.lowagie.text.pdf.PdfOutline;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate template = cb.createTemplate(25, 25);
template.moveTo(0, 13);
template.lineTo(50, 13);
template.stroke();
cb.addTemplate(template, 87, 87);
PdfDestination d1 = new PdfDestination(PdfDestination.XYZ, 300, 800, 0);
PdfDestination d2 = new PdfDestination(PdfDestination.FITH, 500);
PdfDestination d4 = new PdfDestination(PdfDestination.FITBV, 100);
PdfDestination d5 = new PdfDestination(PdfDestination.FIT);
PdfOutline root = cb.getRootOutline();
PdfOutline out1 = new PdfOutline(root, d1, "root", true);
PdfOutline out2 = new PdfOutline(out1, d2, "sub 1", false);
new PdfOutline(out2, d4, "sub 2.1");
new PdfOutline(out2, d5, "sub 2.2");
document.close();
}
}
Use SimpleNamedDestination
import java.io.FileOutputStream;
import java.util.HashMap;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.SimpleNamedDestination;
public class MainClass {
public static void main(String[] args) throws Exception {
Document remote = new Document();
PdfWriter.getInstance(remote, new FileOutputStream("remote.pdf"));
remote.open();
remote.add(new Paragraph("another"));
remote.newPage();
Paragraph p = new Paragraph("This paragraph contains a ");
p.add(new Chunk("local destination").setLocalDestination("test"));
remote.add(p);
remote.close();
PdfReader reader = new PdfReader("remote.pdf");
HashMap map = SimpleNamedDestination.getNamedDestination(reader, false);
SimpleNamedDestination.exportToXML(map, new FileOutputStream("remote.xml"), "ISO8859-1", true);
}
}