Java Tutorial/PDF/PDF Writer
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
Add page event listener to PDF writer
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPageEventHelper;
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"));
MyPdfPageEventHelper tracker = new MyPdfPageEventHelper();
writer.setPageEvent(tracker);
document.open();
document.add(createParagraph("Fox", "Hello lazy dog."));
document.newPage();
HashMap lines = tracker.getLines();
for (Iterator i = lines.keySet().iterator(); i.hasNext();) {
String speaker = (String) i.next();
Integer count = (Integer) lines.get(speaker);
document.add(new Paragraph(speaker + ": " + count.intValue() + " lines."));
}
document.close();
}
private static Paragraph createParagraph(String speaker, String line) {
Paragraph p = new Paragraph(18);
Chunk s = new Chunk(speaker + ": ", new Font(Font.HELVETICA, 12, Font.BOLD));
s.setGenericTag(speaker);
p.add(s);
p.add(line);
return p;
}
}
class MyPdfPageEventHelper extends PdfPageEventHelper {
protected HashMap lines = new HashMap();
public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
Integer count = (Integer) lines.get(text);
if (count == null) {
lines.put(text, new Integer(1));
} else {
lines.put(text, new Integer(count.intValue() + 1));
}
}
public HashMap getLines() {
return lines;
}
}
Generates a PDF file (version ith the text "Hello World"
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document(new Rectangle(14400, 14400));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("HelloWorld_1_6.pdf"));
writer.setPdfVersion(PdfWriter.VERSION_1_6);
document.open();
document.add(new Paragraph("Hello World"));
document.close();
}
}
Output to console with PdfWriter
import com.lowagie.text.Document;
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, System.out);
document.open();
document.add(new Paragraph("Hello World"));
document.close();
}
}
Pause and resume a writer
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();
}
}
PdfWriter.AllowCopy | PdfWriter.AllowPrinting
import java.io.FileOutputStream;
import com.lowagie.text.Document;
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 writer = PdfWriter.getInstance(document, new FileOutputStream("1.pdf"));
writer.setEncryption(PdfWriter.STRENGTH128BITS, "Hello", "World", PdfWriter.AllowCopy
| PdfWriter.AllowPrinting);
document.open();
document.add(new Paragraph("Hello World"));
document.close();
}
}
PdfWriter.NO_SPACE_CHAR_RATIO
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
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(PageSize.A8.rotate());
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
String text = "this is a test";
Paragraph paragraph = new Paragraph(text);
paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
document.add(paragraph);
document.newPage();
writer.setSpaceCharRatio(PdfWriter.NO_SPACE_CHAR_RATIO);
document.add(paragraph);
document.close();
}
}
Set Userunit
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document(new Rectangle(14400, 14400));
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("2.pdf"));
writer.setPdfVersion(PdfWriter.VERSION_1_6);
writer.setUserunit(75000f);
document.open();
document.add(new Paragraph("Hello World"));
document.close();
}
}