当Excel文件中包含图片时转pdf
excel文件转pdf时,有很多工具可以使用,但是我在实际运用过程中发现,如果excel文件包含图片时,并不能是吸纳很好的效果。我的解决思路是:excel中的非图片(文字)部分和图片部分分开来转。以下是我的代码,需要用到的jar包为:pdfbox-app-2.0.22.jar、Spire.Xls.jar
import java.awt.Dimension;
import java.awt.geom.Dimension2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import com.spire.xls.ExcelPicture;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import com.spire.xls.collections.PicturesCollection;
public class textMain {
/*
* excelFilePath 为excel文件名称,不包含路径
* tempPath 为文件夹路径
*/
public void formatPDF(String excelFilePath, String tempPath) throws Exception {
List<String> files = new ArrayList<String>();
try {
if(excelFilePath != null && !excelFilePath.equals("")) {
Workbook workbook = new Workbook();
workbook.loadFromFile(tempPath+excelFilePath);
Worksheet sheet = workbook.getWorksheets().get(0);
PicturesCollection pcs = sheet.getPictures();
String pdfFileName = excelFilePath.replace("xlsx", "pdf");
if(pcs == null || pcs.size()<=0) {//不包含图片的情况
sheet.saveToPdf(tempPath+pdfFileName);
files.add(tempPath+pdfFileName);
}else {
int pcsSize = pcs.size();
ExcelPicture pic = null;
BufferedImage loImage = null;
String uuid = UUID.randomUUID().toString();
for(int i=0;i<pcsSize;i++) {
pic = sheet.getPictures().get(i);
loImage = pic.getPicture();
ImageIO.write(loImage,"png",new File(tempPath+uuid+"_"+i+".png"));//保存图片
pic.remove();//excel删除图片
files.add(tempPath+uuid+"_"+i+".png");
}
String removeImagePath = tempPath+uuid+"-removeImage.xlsx";
files.add(removeImagePath);
//保存到excel,去除图片
workbook.saveToFile(removeImagePath);
workbook.dispose();
//将删除图片的excel文件转为pdf
workbook = new Workbook();
workbook.loadFromFile(removeImagePath);
workbook.getConverterSetting().setSheetFitToPage(true);
sheet = workbook.getWorksheets().get(0);
removeImagePath = removeImagePath.replace("xlsx", "pdf");
sheet.saveToPdf(removeImagePath);
files.add(removeImagePath);
workbook.dispose();
//pdf插入图片
File file = new File(removeImagePath);
PDDocument document = PDDocument.load(file);
PDPage page = document.getPage(0);
PDRectangle pdr = page.getMediaBox();
float pdrH = pdr.getHeight();
float pdrW = pdr.getWidth();
PDRectangle pdrI = null;
PDPageContentStream contents = null;
for(int i=0;i<pcsSize;i++) {
PDImageXObject pdImage = PDImageXObject.createFromFile(tempPath+uuid+"_"+i+".png",document);
pdrI = new PDRectangle(pdImage.getWidth(),pdImage.getHeight());
page = new PDPage();
page.setMediaBox(pdrI);
contents = new PDPageContentStream(document, page);
contents.drawImage(pdImage, 0, 0);
contents.close();
document.addPage(page);
}
document.save(tempPath+pdfFileName);
document.close();
}
}
}catch(Exception e) {
e.printStackTrace();
}finally {
//操作完成后,将图片和中间excel删除
File f = null;
for(int i=0;i<files.size();i++) {
f = new File(files.get(i));
if(f.exists()) {
f.delete();
}
}
}
}
}