Java Tutorial/PDF/PDF Stamper — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:22, 1 июня 2010
Содержание
Copy a selection of pages to new PDF files
import java.io.FileOutputStream;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class MainClass {
public static void main(String[] args) throws Exception {
PdfReader reader = new PdfReader("HelloWorldMultiplePages.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("HelloSelectedOdd.pdf"));
reader.selectPages("o");
stamper.close();
reader = new PdfReader("HelloWorldMultiplePages.pdf");
stamper = new PdfStamper(reader, new FileOutputStream("HelloSelectedEven.pdf"));
reader.selectPages("e");
stamper.close();
reader = new PdfReader("HelloWorldMultiplePages.pdf");
stamper = new PdfStamper(reader, new FileOutputStream("HelloSelected12379.pdf"));
reader.selectPages("1-3, 7-9, !8");
stamper.close();
}
}
Create PdfImportedPage
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfAnnotation;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.SimpleBookmark;
public class MainClass {
public static void main(String[] args) throws Exception {
PdfReader reader = new PdfReader("test.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("my.pdf"));
stamper.insertPage(1, PageSize.A4);
PdfContentByte cb = stamper.getOverContent(1);
PdfImportedPage p = stamper.getImportedPage(reader, 2);
cb.addTemplate(p, 0.4f, 0f, 0f, 0.4f, 36f, 450);
p = stamper.getImportedPage(reader, 3);
cb.addTemplate(p, 0.4f, 0f, 0f, 0.4f, 300f, 450);
p = stamper.getImportedPage(reader, 4);
cb.addTemplate(p, 0.4f, 0f, 0f, 0.4f, 36f, 100);
stamper.addAnnotation(PdfAnnotation.createText(stamper.getWriter(), new Rectangle(300f, 350f,
500f, 450f), "inserted page", "This page contains a copy of the next three pages.", true,
null), 1);
List list = SimpleBookmark.getBookmark(reader);
HashMap<String, String> map = new HashMap<String, String>();
map.put("Title", "title content");
map.put("Action", "GoTo");
map.put("Page", "1 FitH 806");
list.add(0, map);
stamper.setOutlines(list);
stamper.close();
}
}
Import page
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfReader;
public class MainClass {
public static void main(String[] args) throws Exception {
PdfReader reader = new PdfReader("HelloWorldMultiplePages.pdf");
reader.selectPages("o");
int pages = reader.getNumberOfPages();
Document document = new Document();
PdfCopy copy = new PdfCopy(document, new FileOutputStream("HelloWorldSelectPagesOdd.pdf"));
document.open();
for (int i = 0; i < pages;) {
++i;
copy.addPage(copy.getImportedPage(reader, i));
}
document.close();
}
}
Stamping PDF file
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.GrayColor;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfCopyFields;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.TextField;
public class MainClass{
public static void main(String[] args) throws Exception {
createPdf("1.pdf", "field1", "value");
createPdf("2.pdf", "field2", "value");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfCopyFields copy = new PdfCopyFields(baos);
copy.addDocument(new PdfReader("1.pdf"));
copy.addDocument(new PdfReader("2.pdf"));
copy.close();
PdfReader reader = new PdfReader(baos.toByteArray());
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
"HelloWorldCopyStamp.pdf"));
AcroFields form = stamper.getAcroFields();
form.setField("field1", "World");
form.setField("field2", "People");
stamper.setFormFlattening(true);
stamper.close();
}
private static void createPdf(String filename, String field, String value)
throws Exception {
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
filename));
document.open();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI,
BaseFont.NOT_EMBEDDED);
PdfContentByte cb = writer.getDirectContent();
cb.beginText();
cb.setFontAndSize(bf, 12);
cb.setLeading(16);
cb.moveText(36f, 788f);
cb.showText("Hi");
cb.newlineShowText("this is a atest");
cb.endText();
TextField tf = new TextField(writer, new Rectangle(64, 785, 340, 800),
field);
tf.setFontSize(12);
tf.setFont(bf);
tf.setText(value);
tf.setTextColor(new GrayColor(0.5f));
writer.addAnnotation(tf.getTextField());
document.close();
}
}