求个将图片转成pdf文档的java程序,最好有注解,我用pdfbox实现这一要求时,图片不知道为什么没了
上代码:packagecom.pdf.text;importjava.io.File;importjava.io.FileInputStream;importjava.i...
上代码:
package com.pdf.text;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.io.RandomAccessFile;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDCcitt;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
public class ImageToPDF
{
public void createPDFFromImage(String file, String image) throws IOException,COSVisitorException
{
PDDocument doc = null;
try
{
doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDXObjectImage ximage = null;
if (image.toLowerCase().endsWith(".jpg"))
{
ximage = new PDJpeg(doc, new FileInputStream(image));
System.out.println(ximage);
}
else if (image.toLowerCase().endsWith(".tif") || image.toLowerCase().endsWith(".tiff"))
{
ximage = new PDCcitt(doc, new RandomAccessFile(new File(image),"r"));
}
else
{
throw new IOException( "Image type not supported:" + image );
}
//将文档流化
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
//在(20,20)坐标处画图
contentStream.drawImage( ximage, 20, 20 );
doc.save(file);
System.out.println(file + " created!");
}
finally
{
if( doc != null )
{
doc.close();
}
}
}
public static void main(String[] args)
{
ImageToPDF app = new ImageToPDF();
try
{
if( args.length != 2 )
{
System.out.println(args.length);
app.usage();
}
else
{
app.createPDFFromImage( args[0], args[1] );
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* This will print out a message telling how to use this example.
*/
private void usage()
{
System.err.println( "usage: " + this.getClass().getName() + " <output-file> <image>" );
}
} 展开
package com.pdf.text;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.io.RandomAccessFile;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDCcitt;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
public class ImageToPDF
{
public void createPDFFromImage(String file, String image) throws IOException,COSVisitorException
{
PDDocument doc = null;
try
{
doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDXObjectImage ximage = null;
if (image.toLowerCase().endsWith(".jpg"))
{
ximage = new PDJpeg(doc, new FileInputStream(image));
System.out.println(ximage);
}
else if (image.toLowerCase().endsWith(".tif") || image.toLowerCase().endsWith(".tiff"))
{
ximage = new PDCcitt(doc, new RandomAccessFile(new File(image),"r"));
}
else
{
throw new IOException( "Image type not supported:" + image );
}
//将文档流化
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
//在(20,20)坐标处画图
contentStream.drawImage( ximage, 20, 20 );
doc.save(file);
System.out.println(file + " created!");
}
finally
{
if( doc != null )
{
doc.close();
}
}
}
public static void main(String[] args)
{
ImageToPDF app = new ImageToPDF();
try
{
if( args.length != 2 )
{
System.out.println(args.length);
app.usage();
}
else
{
app.createPDFFromImage( args[0], args[1] );
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* This will print out a message telling how to use this example.
*/
private void usage()
{
System.err.println( "usage: " + this.getClass().getName() + " <output-file> <image>" );
}
} 展开
1个回答
展开全部
给你一个用IText写的吧
// 写PDF文件.
BufferedImage img = ImageIO.read(new File(imgPath));
FileOutputStream fos = new FileOutputStream(pdfFile);
// 创建PDF文档
Document doc = new Document(null, 0, 0, 0, 0);
// 设置尺寸为图片尺寸
doc.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
Image image = Image.getInstance(imgPath);
PdfWriter.getInstance(doc, fos);
doc.open();
doc.add(image);
doc.close();
// 写PDF文件.
BufferedImage img = ImageIO.read(new File(imgPath));
FileOutputStream fos = new FileOutputStream(pdfFile);
// 创建PDF文档
Document doc = new Document(null, 0, 0, 0, 0);
// 设置尺寸为图片尺寸
doc.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
Image image = Image.getInstance(imgPath);
PdfWriter.getInstance(doc, fos);
doc.open();
doc.add(image);
doc.close();
追问
itext是什么
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询