Java Tutorial/PDF/Introduction
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
Create Empty page
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("2.pdf"));
document.open();
writer.setPageEmpty(true);
document.newPage();
writer.setPageEmpty(false);
document.newPage();
document.add(new Paragraph("Hello World"));
document.newPage();
writer.setPageEmpty(true);
document.newPage();
document.close();
}
}
Create your first Pdf document with Java
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class GeneratesPDFFileWithText {
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
document.open();
document.add(new Paragraph("Hello World"));
document.close();
}
}
inspects a PDF file
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Iterator;
import com.lowagie.text.pdf.PRStream;
import com.lowagie.text.pdf.PRTokeniser;
import com.lowagie.text.pdf.PdfArray;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfIndirectReference;
import com.lowagie.text.pdf.PdfLister;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfObject;
import com.lowagie.text.pdf.PdfReader;
public class MainClass {
public static void main(String[] args) throws Exception {
PdfReader reader = new PdfReader("2.pdf");
PrintStream list = new PrintStream(new FileOutputStream("2.txt"));
PdfLister lister = new PdfLister(new PrintStream(list));
PdfDictionary trailer = reader.getTrailer();
lister.listDict(trailer);
PdfIndirectReference info = (PdfIndirectReference) trailer.get(PdfName.INFO);
lister.listAnyObject(info);
lister.listAnyObject(reader.getPdfObject(info.getNumber()));
PdfDictionary root = reader.getCatalog();
lister.listDict(root);
PdfDictionary outlines = (PdfDictionary) reader.getPdfObject(((PdfIndirectReference) root
.get(PdfName.OUTLINES)).getNumber());
lister.listDict(outlines);
PdfObject first = reader.getPdfObject(((PdfIndirectReference) outlines.get(PdfName.FIRST))
.getNumber());
lister.listAnyObject(first);
PdfDictionary pages = (PdfDictionary) reader.getPdfObject(((PdfIndirectReference) root
.get(PdfName.PAGES)).getNumber());
lister.listDict(pages);
PdfArray kids = (PdfArray) pages.get(PdfName.KIDS);
PdfIndirectReference kid_ref;
PdfDictionary kid = null;
for (Iterator i = kids.getArrayList().iterator(); i.hasNext();) {
kid_ref = (PdfIndirectReference) i.next();
kid = (PdfDictionary) reader.getPdfObject(kid_ref.getNumber());
lister.listDict(kid);
}
PdfIndirectReference content_ref = (PdfIndirectReference) kid.get(PdfName.CONTENTS);
PRStream content = (PRStream) reader.getPdfObject(content_ref.getNumber());
lister.listDict(content);
byte[] contentstream = PdfReader.getStreamBytes(content);
list.println(new String(contentstream));
PRTokeniser tokenizer = new PRTokeniser(contentstream);
while (tokenizer.nextToken()) {
if (tokenizer.getTokenType() == PRTokeniser.TK_STRING) {
list.println(tokenizer.getStringValue());
}
}
}
}
PdfDictionary
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PRIndirectReference;
import com.lowagie.text.pdf.PRStream;
import com.lowagie.text.pdf.PRTokeniser;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfReader;
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(PageSize.A6);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.beginText();
cb.setFontAndSize(bf, 12);
cb.moveText(88.66f, 367);
cb.showText("ld");
cb.endText();
PdfTemplate tmp = cb.createTemplate(250, 25);
tmp.beginText();
tmp.setFontAndSize(bf, 12);
tmp.moveText(0, 7);
tmp.showText("Hello People");
tmp.endText();
cb.addTemplate(tmp, 36, 343);
document.close();
PdfReader reader = new PdfReader("2.pdf");
PdfDictionary page = reader.getPageN(1);
PRIndirectReference objectReference = (PRIndirectReference) page.get(PdfName.CONTENTS);
PRStream stream = (PRStream) PdfReader.getPdfObject(objectReference);
byte[] streamBytes = PdfReader.getStreamBytes(stream);
PRTokeniser tokenizer = new PRTokeniser(streamBytes);
while (tokenizer.nextToken()) {
if (tokenizer.getTokenType() == PRTokeniser.TK_STRING) {
System.out.println(tokenizer.getStringValue());
}
}
}
}
PRTokeniser
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PRIndirectReference;
import com.lowagie.text.pdf.PRStream;
import com.lowagie.text.pdf.PRTokeniser;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document(PageSize.A6);
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
document.add(new Paragraph("Hello World"));
document.add(new Paragraph("Hello People"));
document.close();
PdfReader reader = new PdfReader("2.pdf");
PdfDictionary page = reader.getPageN(1);
PRIndirectReference objectReference = (PRIndirectReference) page.get(PdfName.CONTENTS);
PRStream stream = (PRStream) PdfReader.getPdfObject(objectReference);
byte[] streamBytes = PdfReader.getStreamBytes(stream);
String contentStream = new String(streamBytes);
System.out.println(contentStream);
PRTokeniser tokenizer = new PRTokeniser(streamBytes);
while (tokenizer.nextToken()) {
if (tokenizer.getTokenType() == PRTokeniser.TK_STRING) {
System.out.println(tokenizer.getStringValue());
}
}
StringBuffer buf = new StringBuffer();
int pos = contentStream.indexOf("Hello World") + 11;
buf.append(contentStream.substring(0, pos));
buf.append("Hello");
buf.append(contentStream.substring(pos));
String hackedContentStream = buf.toString();
document = new Document(PageSize.A6);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
"HelloWorldStreamHacked.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.setLiteral(hackedContentStream);
document.close();
}
}
Read Pdf document to string
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document(PageSize.A6);
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
document.add(new Paragraph("Hello World"));
document.add(new Paragraph("Hello People"));
document.close();
PdfReader reader = new PdfReader("2.pdf");
byte[] streamBytes = reader.getPageContent(1);
String contentStream = new String(streamBytes);
System.out.println(contentStream);
StringBuffer buf = new StringBuffer();
int pos = contentStream.indexOf("Hello World") + 11;
buf.append(contentStream.substring(0, pos));
buf.append(", Hello ");
buf.append(contentStream.substring(pos));
String hackedContentStream = buf.toString();
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("HelloWorldStreamHack.pdf"));
reader.setPageContent(1, hackedContentStream.getBytes());
stamper.close();
}
}
Tagged PDF
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfStructureElement;
import com.lowagie.text.pdf.PdfStructureTreeRoot;
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.setTagged();
document.open();
PdfStructureTreeRoot root = writer.getStructureTreeRoot();
PdfStructureElement eTop = new PdfStructureElement(root, new PdfName("Everything"));
root.mapRole(new PdfName("Everything"), new PdfName("Sect"));
PdfStructureElement e1 = new PdfStructureElement(eTop, PdfName.P);
PdfStructureElement e2 = new PdfStructureElement(eTop, PdfName.P);
PdfStructureElement e3 = new PdfStructureElement(eTop, PdfName.P);
PdfContentByte cb = writer.getDirectContent();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false);
cb.setLeading(16);
cb.setFontAndSize(bf, 12);
cb.beginText();
cb.setTextMatrix(50, 700);
for (int k = 0; k < 2; ++k) {
cb.beginMarkedContentSequence(e2);
cb.newlineShowText("text2");
cb.endMarkedContentSequence();
}
cb.endText();
document.newPage();
cb.setLeading(16);
cb.setFontAndSize(bf, 12);
cb.beginText();
cb.setTextMatrix(50, 804);
cb.beginMarkedContentSequence(e2);
cb.newlineShowText("text2");
cb.endMarkedContentSequence();
cb.endText();
cb.beginMarkedContentSequence(e3);
cb.endMarkedContentSequence();
document.close();
}
}
Update a Pdf document
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document.rupress = false;
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
document.add(new Paragraph("Hello World"));
document.close();
PdfReader reader = new PdfReader("HelloWorld.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("updated.pdf"), "\0", true);
PdfContentByte cb = stamper.getOverContent(1);
cb.beginText();
cb.showTextAligned(Element.ALIGN_LEFT, "Hello People", 36, 770, 0);
cb.endText();
stamper.close();
}
}