Java Tutorial/PDF/PDF Writer
Содержание
Add page event listener to PDF writer
<source lang="java">
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; }
}</source>
Generates a PDF file (version ith the text "Hello World"
<source lang="java">
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(); }
}</source>
Output to console with PdfWriter
<source lang="java">
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(); }
}</source>
Pause and resume a writer
<source lang="java">
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(); }
}</source>
PdfWriter.AllowCopy | PdfWriter.AllowPrinting
<source lang="java">
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(); }
}</source>
PdfWriter.NO_SPACE_CHAR_RATIO
<source lang="java">
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(); }
}</source>
Set Userunit
<source lang="java">
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(); }
}</source>