iText
Set Zoom Size
public ActionForward showGraphic(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws IOException, DocumentException {
String[] imgPaths = (request.getParameter("imgPath") != null)
? request.getParameter("imgPath").split(",") : null;
String imgarea = request.getParameter("imgarea");
Float height = Float.valueOf(request.getParameter("height"));
Float width = Float.valueOf(request.getParameter("width"));
Rectangle rectangle;
rectangle = new Rectangle(Float.valueOf(width), Float.valueOf(height));
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
Document document = new Document(rectangle);
PdfWriter pdfWriter = PdfWriter.getInstance(document, baosPDF);
pdfWriter.setViewerPreferences(pdfWriter.PageModeUseOutlines);
document.open();
for (String imgPath : imgPaths) {
File file = new File(request.getSession().getServletContext().getRealPath("").replace("editor", "webdav")
+ imgPath.split("webdav")[1]);
if(file.exists()) {
Image image = Image.getInstance(file.getPath());
image.scaleToFit(("BP".equals(imgarea)) ? width - 100f : width - 80f, height);
document.add(image);
}
else {
logger.error("Editor -> Graphic Preview -> File not found : " + file.getPath());
Paragraph paragraph = new Paragraph();
Chunk chunk = new Chunk("Image not found!");
paragraph.add(chunk);
document.add(paragraph);
}
}
float zoom = 0f;
if ("AP".equals(imgarea)) {
zoom = 0.53f;
}
else if ("BP".equals(imgarea)) {
zoom = 0.51f;
}
else {
zoom = 0.52f;
}
PdfAction ac = PdfAction.gotoLocalPage(1, new PdfDestination(PdfDestination.XYZ, -1, -1, zoom), pdfWriter);
pdfWriter.setOpenAction(ac);
document.close();
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "inline; filename=preview.pdf");
OutputStream out = response.getOutputStream();
out.write(baosPDF.toByteArray());
out.flush();
out.close();
return null;
}
References
http://www.coderanch.com/t/494982/open-source/iText-PDF-export
http://www.opensubscriber.com/message/itext-questions@lists.sourceforge.net/3903218.html
http://itextpdf.com/examples/iia.php?id=17
PdfReader pdf = new PdfReader("abc.pdf");
PdfStamper stp = new PdfStamper(pdf, new
FileOutputStream("abcout.pdf"));
PdfWriter writer = stp.getWriter();
PdfAction ac = PdfAction.gotoLocalPage(1, new
PdfDestination(PdfDestination.XYZ, -1, -1, 0.1f), writer);
writer.setOpenAction(ac);
stp.close();
Set PDF on the Fly
public ActionForward showGraphic(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws IOException, DocumentException {
String[] imgPaths = (request.getParameter("imgPath") != null)
? request.getParameter("imgPath").split(",") : null;
String imgarea = request.getParameter("imgarea");
Float height = Float.valueOf(request.getParameter("height"));
Float width = Float.valueOf(request.getParameter("width"));
Rectangle rectangle;
rectangle = new Rectangle(Float.valueOf(width), Float.valueOf(height));
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
Document document = new Document(rectangle);
PdfWriter pdfWriter = PdfWriter.getInstance(document, baosPDF);
pdfWriter.setViewerPreferences(pdfWriter.PageModeUseOutlines);
document.open();
for (String imgPath : imgPaths) {
File file = new File(request.getSession().getServletContext().getRealPath("").replace("editor", "webdav")
+ imgPath.split("webdav")[1]);
if(file.exists()) {
Image image = Image.getInstance(file.getPath());
image.scaleToFit(("BP".equals(imgarea)) ? width - 100f : width - 80f, height);
document.add(image);
}
else {
logger.error("Editor -> Graphic Preview -> File not found : " + file.getPath());
Paragraph paragraph = new Paragraph();
Chunk chunk = new Chunk("Image not found!");
paragraph.add(chunk);
document.add(paragraph);
}
}
float zoom = 0f;
if ("AP".equals(imgarea)) {
zoom = 0.53f;
}
else if ("BP".equals(imgarea)) {
zoom = 0.51f;
}
else {
zoom = 0.52f;
}
PdfAction ac = PdfAction.gotoLocalPage(1, new PdfDestination(PdfDestination.XYZ, -1, -1, zoom), pdfWriter);
pdfWriter.setOpenAction(ac);
document.close();
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "inline; filename=preview.pdf");
OutputStream out = response.getOutputStream();
out.write(baosPDF.toByteArray());
out.flush();
out.close();
return null;
}
References
http://www.coderanch.com/t/494982/open-source/iText-PDF-export
http://www.opensubscriber.com/message/itext-questions@lists.sourceforge.net/3903218.html
http://itextpdf.com/examples/iia.php?id=17
Labels: iText
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home