<%@page import="java.io.FileOutputStream,
java.util.ArrayList,
java.util.List,
com.lowagie.text.Document,
com.lowagie.text.pdf.PRAcroForm,
com.lowagie.text.pdf.PdfCopy,
com.lowagie.text.pdf.PdfImportedPage,
com.lowagie.text.pdf.PdfReader,
com.lowagie.text.pdf.SimpleBookmark" %><%
try {
int pageOffset = 0;
ArrayList master = new ArrayList();
int f = 0;
String outFile = "/tmp/ouputPDF.pdf";
Document document = null;
PdfCopy writer = null;
while (f < 2) {
// we create a reader for a certain document
PdfReader reader = null;
if( f == 0) reader = new PdfReader(application.getRealPath("/Page1.pdf"));
else
reader = new PdfReader(application.getRealPath("/Page2.pdf"));
reader.consolidateNamedDestinations();
// we retrieve the total number of pages
int n = reader.getNumberOfPages();
List bookmarks = SimpleBookmark.getBookmark(reader);
if (bookmarks != null) {
if (pageOffset != 0)
SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);
master.addAll(bookmarks);
}
pageOffset += n;
if (f == 0) {
// step 1: creation of a document-object
document = new Document(reader.getPageSizeWithRotation(1));
// step 2: we create a writer that listens to the document
writer = new PdfCopy(document, new FileOutputStream(outFile));
// step 3: we open the document
document.open();
}
// step 4: we add content
PdfImportedPage ppage;
for (int i = 0; i < n; ) {
++i;
ppage = writer.getImportedPage(reader, i);
writer.addPage(ppage);
}
PRAcroForm form = reader.getAcroForm();
if (form != null)
writer.copyAcroForm(reader);
f++;
}
if (master.size() > 0)
writer.setOutlines(master);
// step 5: we close the document
document.close();
}
catch(Exception e) {
e.printStackTrace();
}
%>
Monday, February 27, 2006
Concatenate Two PDFs in a JSP using iText
This is an adaptation of the stand alone java file on the iText website to JSP:
Subscribe to:
Post Comments (Atom)
Labels
- Java (34)
- Oracle (27)
- javascript (24)
- NIX administration (19)
- Reporting (18)
- XML (17)
- Web Graphics (10)
- perl (10)
- CSS (9)
- Tomcat (8)
- Android (7)
- Matlab (7)
- XSL (7)
- HTML (6)
- SQL (6)
- XForms (6)
- browser quirks (6)
- Orbeon XForms (5)
- Solaris (5)
- AJAX (4)
- Mirth Project (4)
- PHP (4)
- Video (4)
- Arduino (3)
- Eclipse (3)
- JPA (3)
- JSP (3)
- JSTL (3)
- LAMPS (3)
- SSH (3)
- SVN (3)
- Hibernate (2)
- Netbeans (2)
- Networking (2)
- Python (2)
- Windows (2)
- Wordpress (2)
- XHTML (2)
- Alfresco (1)
- Architecture (1)
- ArduPilot (1)
- Arduino Yun (1)
- Arduplane (1)
- Audio Recording (1)
- Betaflight (1)
- CouchDB (1)
- DIY (1)
- Design (1)
- FPV (1)
- JSON (1)
- JUnit (1)
- Mobile Development (1)
- Printing (1)
- RC Airplane (1)
- REST (1)
- Scalability (1)
- Struts (1)
- Tools (1)
- Virtualization (1)
- Web services (1)
- camera (1)
- canon (1)
- gphoto2 (1)
- jQuery (1)
- ubuntu (1)
- unix (1)
No comments:
Post a Comment