how to convert jpg to pdf in Android java - java

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();
}
}

Related

itext Create pdf with text wrapped around the icon

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:

How add an image to an existing pdf file from a location and should save back to same location?

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();
}
}

Apache poi slide show to pdf conversion

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

Java convert GIF image to PNG format

I have to build a Java servlet that receives an image and returns that image converted to PNG format. How can I achieve this?
By converting I don't mean changing the file extension, like some examples suggest.
Thanks in advance!
Try this out:
package demo;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main( String [] args ) throws IOException {
File input = new File("input.gif");
File output = new File("output.png");
ImageIO.write( ImageIO.read( input ), "png", ouput);
}
}
Read ImageIO.
Of course, you may want to read and write from an stream instead.
ImageIO.write(ImageIO.read(new File("img.gif")), "png", new File("img.png"));
Use ImageIo to save an Image in any format your want.

How to read pdf form fields using Java?

I've a requirement where user are going to fill lots of fields ( text field, check box, radio button ) on pdf form and they will mail us. I need to read each fields on pdf form and insert into oracle table.
Edit1: I'm trying following code, It generates pdf but when I double click it says "invalid format". What's wrong ?
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
public class pdfGentest{
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("c:\\HelloWorld.pdf"));
document.open();
document.add(new Paragraph("Hello World"));
}
catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
}
}
Fixed: Due to I've not closed the document..Adding document.close(); fixed the problem
You can use IText library to do that. Link =>http://itextpdf.com/
Sadly I don't have java code example for that as I am using iTextSharp library for C#.NET buts pretty straightforward.
You might want to check itextpdf.com/book/examples.php for examples. Also check the following link for some example on reading field values,
http://itext-general.2136553.n4.nabble.com/Problem-Reading-Interactive-Form-Values-Acro-Fields-from-PDF-using-iText-td2171900.html
You can use PDF Box api, which will support to extract the fields information more clearly.

Categories