Java/PDF RTF/Bookmarks
Версия от 18:01, 31 мая 2010; (обсуждение)
Adding Bookmarks for PDF document
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDestination;
import com.lowagie.text.pdf.PdfOutline;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
public class BookmarksPDF extends PdfPageEventHelper {
public void onParagraph(PdfWriter writer, Document document, float position) {
PdfContentByte cb = writer.getDirectContent();
new PdfOutline(cb.getRootOutline(), new PdfDestination(PdfDestination.FITH, position), "paragraph at position: " +position);
}
public static void main(String[] args) {
Document document = new Document(PageSize.A6);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("BookmarksPDF.pdf"));
writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
document.open();
writer.setPageEvent(new BookmarksPDF());
document.add(new Paragraph("Text", new Font(Font.HELVETICA, 12)));
document.newPage();
document.add(new Paragraph("Text Text", new Font(Font.HELVETICA, 24)));
document.newPage();
document.add(new Paragraph("Text Text Text", new Font(Font.HELVETICA, 36)));
document.newPage();
document.add(new Paragraph("Text Text Text Text", new Font(Font.HELVETICA, 48)));
} catch (Exception de) {
de.printStackTrace();
}
document.close();
}
}
Adding Bookmarks with outline for PDF document
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDestination;
import com.lowagie.text.pdf.PdfOutline;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
public class Bookmarks1PDF{
public static void main(String[] args) {
Document document = new Document(PageSize.A6);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("Bookmarks1PDF.pdf"));
writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
document.open();
document.add(new Paragraph("Text.", new Font(Font.HELVETICA, 12)));
PdfContentByte cb = writer.getDirectContent();
PdfDestination destination = new PdfDestination(PdfDestination.FITH, 0);
PdfOutline outline = new PdfOutline(cb.getRootOutline(), destination, "paragraph " + 1);
document.add(new Paragraph("Text.", new Font(Font.HELVETICA, 12)));
document.add(new Paragraph("Text.", new Font(Font.HELVETICA, 12)));
cb = writer.getDirectContent();
destination = new PdfDestination(PdfDestination.FITH, 1);
outline = new PdfOutline(cb.getRootOutline(), destination, "paragraph " + 2);
document.add(new Paragraph("Text.", new Font(Font.HELVETICA, 12)));
} catch (Exception de) {
de.printStackTrace();
}
document.close();
}
}