I am trying to create a pdf using itext java library like:
Where I create highlighter icon(the arrow sign) like:
Phrase ph=new Phrase("รค", FontFactory.getFont(FontFactory.ZAPFDINGBATS, 14f));
Now I want that all the text of the advertisement ie.
Now own a Farm Plot nr Jigani in 5yrs installment option #INR450/sqft
with 5acre Club House.www.goldeneraproperty.com M-9999999999
should automatically wrap around the icon which I created above using FontFactory.ZAPFDINGBATS.
However I am stuck here.Please could anyone help me resolve it.
I tried creating a table of one column and one cell and put the icon in that cell but it did not help as the text did not wrap around the table.
Please suggest how could i create a pdf where the text would automatically wrap around the icon.Thanks in Advance!!!
I have designed it to a similar state. Please use you own alignment, fonts, sizes, etc. inside you PDF file.
PFB the code for above design:
package com.itext_dummy;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class Hello {
/** Path to the resulting PDF file. */
public static final String RESULT
= "src/hello.pdf";
public static void main(String[] args)
throws DocumentException, IOException {
new Hello().createPdf(RESULT);
}
public void createPdf(String filename)
throws DocumentException, IOException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
Image img = Image.getInstance("src/Arrow.png");
img.scaleAbsolute(50f, 50f);
img.setAlignment(Image.LEFT | Image.TEXTWRAP);
document.add(img);
Paragraph para = new Paragraph();
para.add("Now own a Farm Plot nr Jigani in 5yrs installment option #INR450/sqft with 5acre Club House.www.goldeneraproperty.com M-99999999994444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444");
document.add(para);
document.close();
}
}
Please do your own alignment on image size and paragraph text sizes and alignments.
Also PFB the icon image that I used:
I used the image since, that is the only way the wrapping could be done.
PFB my result screenshot:
Related
I am wondering if by any mean, i can set the 'Slide sized for ' as On-ScreenShow (16:9). i mean is there any method in master object in apache poi hslf? I couldn't find it. I have added the image for the reference.
You can only have one page size per file.
To set the page dimension call SlideShow.setPageSize().
To find out which page dimensions 4:3, 16:9 or any other formats are, just create a PPT manually via Powerpoint and check its dimension - or use a Cross-multiplication:
import java.io.File;
import java.io.IOException;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.sl.usermodel.SlideShowFactory;
public class SlideSizes {
public static void main(String[] args) throws IOException {
String files[] = { "dim_4_3.ppt", "dim_16_9.ppt" };
for (String f : files) {
SlideShow<?,?> ppt = SlideShowFactory.create(new File(f));
System.out.println(ppt.getPageSize());
}
}
}
There is a web site developed entirely in PHP with GD library that mainly deals with printing text over a random image.
The text has to be printed in any of about 100 prescribed TTF fonts (residing in a directory near the .php files) in a dozen of prescribed colors and there is a requirement to specify location of the text according to any of several prescribed algorithms.
This is solved by using imagettfbox() which returns geometry of the text, which is then mapped onto the image using imagettftext().
What can I use in Java to achieve the same functionality with servlets/beans?
I can put the TTF fonts anywhere I have to. Registering them in Windows is undesirable, but if that's the only option we'd go for it (currently they are not).
#LexLythius:
From your link and some other resources I pieced together a working solution to drawing text over graphics:
// file is pointed at the image
BufferedImage loadedImage = ImageIO.read(file);
// file is pointed at the TTF font
Font font = Font.createFont(Font.TRUETYPE_FONT, file);
Font drawFont = font.deriveFont(20); // use actual size in real life
Graphics graphics = loadedImage.getGraphics();
FontMetrics metrics = graphics.getFontMetrics(drawFont);
Dimension size = new Dimension(metrics.getHeight(), metrics.stringWidth("Hello, world!"));
graphics.setFont(drawFont);
graphics.setColor(new Color(128, 128, 128); // use actual RGB values in real life
graphics.drawString("Hello, world!", 10,10); // use Dimension to calculate position in real life
What is remaining is displaying that image in a servlet and deciding what functionality goes where - to a servlet or bean.
You can get an OutputStream from the servlet response, and pass that OutputStream to ImageIO.write. Example based on the code from here (not my code):
http://www.avajava.com/tutorials/lessons/how-do-i-return-an-image-from-a-servlet-using-imageio.html
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ImageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("image/jpeg"); // change this if you are returning PNG
File f = new File( /* path to your file */ );
BufferedImage bi = ImageIO.read(f);
// INSERT YOUR IMAGE MANIPULATION HERE
OutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out); // or "png" if you prefer
out.close(); // may not be necessary; containers should do this for you
}
Depending on how much memory you have, pre-loading the fonts and keeping a pool of them may save a bit of time.
I want to convert several .jpg files (taken using the device camera) to ONE .pdf file.
I saw a lot of tools like iText, mupdf, PDFjet, pdjBox.
Is there something more simple? (like an API ready for android?)
Thanks.
using iText Library to convert the text to pdf. Use this to convert image to pdf.
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class imagesPDF
{
public static void main(String arg[])throws Exception
{
Document document=new Document();
PdfWriter.getInstance(document,new FileOutputStream("YourPDFHere.pdf"));
document.open();
Image image = Image.getInstance ("yourImageHere.jpg");
document.add(new Paragraph("Your Heading for the Image Goes Here"));
document.add(image);
document.close();
}
}
How add an image to an existing pdf file from a location,
and should save back to same location, I have used following code but it generates a new pdf with existing jpeg. But how to deal with existing pdf file and save resultant pdf image?
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class imagesPDF
{
public static void main(String arg[])throws Exception
{
Document document=new Document();
PdfWriter.getInstance(document,new FileOutputStream("imagesPDF.pdf"));
document.open();
Image image = Image.getInstance ("devi.jpg");
document.add(new Paragraph("textdata"));
document.add(image);
document.close();
}
}
Is there any way to convert generated .ppt file using apache poi to .pdf file?
OR
Any way to convert PPT file to PDF file using JAVA?
Gagravarr, thanks you for your comment with following approach: PPT -> images -> PDF. It gave me clues for further solutions.
Recently, I've faced the same task: convert PPT reports into PDF reports using Java facilities. PPT reports are generated via Apache POI lib, and I intended to reuse ready created PPT structure.
I developed two solutions, each has its own advantages/disadvantages. And both of them using iText library with version 2.1.7.(which is free to use, and which is great)). Both of them do support Japanese symbols after additional enhancement.
1. Apache POI Slide -> image -> PDF
Demonstration code example:
package com.test.pdf;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
public class PPTtoImageToPDFexample {
public static void main(String[] args) throws IOException, DocumentException {
//load any ppt file
FileInputStream inputStream = new FileInputStream("d:/temp/initialPPT.ppt");
SlideShow ppt = new SlideShow(inputStream);
inputStream.close();
Dimension pgsize = ppt.getPageSize();
//take first slide and save it as an image
Slide slide = ppt.getSlides()[0];
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,
pgsize.height));
slide.draw(graphics);
FileOutputStream out = new FileOutputStream("d:/temp/slideImage.png");
javax.imageio.ImageIO.write(img, "png", out);
out.close();
//get saved slide-image and save it into pdf
Image slideImage = Image.getInstance("d:/temp/slideImage.png");
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("d:/temp/PPTtoImageTest.pdf"));
document.setPageSize(new Rectangle(slideImage.getWidth(), slideImage.getHeight()));
document.open();
slideImage.setAbsolutePosition(0, 0);
document.add(slideImage);
document.close();
}
}
2. This approach works on-fly: take Apache POI Slide -> get awt.Graphics2 instance from it -> pass this interface to the iText
draw engine.
Demonstration code example:
package com.test.pdf;
import java.awt.Dimension;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfGraphics2D;
import com.lowagie.text.pdf.PdfWriter;
public class PPTtoPDFdirectly {
public static void main(String[] args) throws IOException, DocumentException {
//load any ppt file
FileInputStream inputStream = new FileInputStream("d:/temp/initialPPT.ppt");
SlideShow ppt = new SlideShow(inputStream);
inputStream.close();
Dimension pgsize = ppt.getPageSize();
//take first slide and draw it directly into PDF via awt.Graphics2D interface.
Slide slide = ppt.getSlides()[0];
Document document = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("d:/temp/PPTtoPDF.pdf"));
document.setPageSize(new Rectangle((float)pgsize.getWidth(), (float) pgsize.getHeight()));
document.open();
PdfGraphics2D graphics = (PdfGraphics2D) pdfWriter.getDirectContent().createGraphics((float)pgsize.getWidth(), (float)pgsize.getHeight());
slide.draw(graphics);
graphics.dispose();
document.close();
}
}
One option is to use POI to convert each slide into an image, then use something like Apache PDFBox to place each image onto it's own PDF page. This should work well for simpler PPT files, but the code to render slides is still a WIP. So, if you have a very complex slide, you may find some bits missing/incorrect, do send in patches if you fix any of these gaps!
Otherwise, your other option is to use the Java bindings for OpenOffice, and have that do the conversion for you