package com.platform.frame.util;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import fr.opensagres.xdocreport.itext.extension.font.ITextFontRegistry;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import java.util.List;
/**
* 导出PDF文件转换工具
*/
@Component
public class PDFUtil {
private static final Logger LOGGER = LogManager.getLogger(PDFUtil.class);
private static String tempPath;
private static String imgTempPath;
@Value("${FILE.TEMPPATH}")
public void setTempPath(String tempPath) {
PDFUtil.tempPath = tempPath;
}
@Value("${IMG.FILEUPLOADPATH}")
public void setImgPath(String imgTempPath) {
PDFUtil.imgTempPath = imgTempPath;
}
/**
* 生成 PDF 文件
*
* @param html HTML文本
*/
public static String createPDF(String html) {
return createPDF(new ByteArrayInputStream(html.getBytes()));
}
/**
* 生成 PDF 文件
*
* @param html HTML流
*/
public static String createPDF(InputStream html) {
String dir = tempPath + UserUtils.getUuid() + ".pdf";
try {
FileUtils.fileExists(dir);
File file = new File(dir);
FileOutputStream out = new FileOutputStream(file);
Document document = new Document(PageSize.A4);
PdfWriter pdfWriter = PdfWriter.getInstance(document, out);
document.open();
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
worker.parseXHtml(pdfWriter, document, html, Charset.forName("UTF-8"), new AsianFontProvider());
document.close();
} catch (Exception e) {
}
return dir;
}
/**
* 根据模板出pdf
*
* @param filePath 模板地址
* @param mapData
* @throws Exception
*/
public static String templetToPdf(String filePath, Map<String, Object> mapData, Map<String, PdfPTable> tableInsert, Map<String, String> imgInsert, Map<String, List<String>> imgPathList) {
String outFile = "";
try {
ClassPathResource resource = new ClassPathResource(filePath);
System.out.println("模板文件绝对路径:" + resource.getPath());
PdfReader reader = new PdfReader(resource.getInputStream());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfStamper ps = new PdfStamper(reader, bos);
/*使用中文字体 */
// 仿宋
// Resource fileRource = new ClassPathResource("pdf/font/simfang.ttf");
// InputStream stream = fileRource.getInputStream();
// byte[] st1 = FileCopyUtils.copyToByteArray(stream);
// BaseFont baseSimfang = BaseFont.createFont("simfang2.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, BaseFont.NOT_CACHED,
// st1, st1);
// 黑体 字体统一用仿宋
// fileRource = new ClassPathResource("pdf/font/simhei.ttf");
// path = fileRource.getFile().getAbsolutePath();
// BaseFont baseHei = BaseFont.createFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
// fontList.add(baseSimfang);
fontList.add(bfChinese);
AcroFields s = ps.getAcroFields();
s.setSubstitutionFonts(fontList);
for (Map.Entry<String, Object> entry : mapData.entrySet()) {
s.setField(entry.getKey(), null == entry.getValue() ? "" : entry.getValue().toString());
System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
}
// 插入文字
if (null != tableInsert && !tableInsert.isEmpty()) {
insertTable(ps, s, tableInsert);
}
// 插入图片
if (null != imgInsert && !imgInsert.isEmpty()) {
insertImg(ps, s, reader, imgInsert);
}
// 往pdf末尾插入图片(文字作为水印在图片上)
/*if (null != imgPathList && !imgPathList.isEmpty()) {
addImageEnd(ps, reader, imgPathList);
}
*/
// 往pdf末尾插入图片(文字标题在图片上)
if (null != imgPathList && !imgPathList.isEmpty()) {
addImageEndTextOnFirst(ps, reader, bfChinese, imgPathList);
}
// 如果为false那么生成的PDF文件还能编辑,一定要设为true
ps.setFormFlattening(true);
ps.close();
String fileName = UserUtils.getUuid() + ".pdf";
outFile = tempPath + fileName;
System.out.println("导出pdf地址:" + outFile);
FileUtils.checkExist(outFile);
FileOutputStream fos = new FileOutputStream(outFile);
fos.write(bos.toByteArray());
fos.close();
String markOutName = UserUtils.getUuid() + ".pdf";
String markOut = tempPath + markOutName;
waterMark(outFile, markOut, "中国科技核心期刊");
// 删除原先不带水印文件
File f = new File(outFile);
f.delete();
System.out.println("返回导出pdf的名称:" + markOutName);
return markOutName;
} catch (Exception e) {
LOGGER.error(e);
e.printStackTrace();
}
return "";
}
/**
* 插入文本
*
* @author vio
* @date 2019/08/20
*/
public static void insertTable(PdfStamper ps, AcroFields s, Map<String, PdfPTable> tableInsert) {
for (Map.Entry<String, PdfPTable> entry : tableInsert.entrySet()) {
// 获取key对应参数
List<AcroFields.FieldPosition> list = s.getFieldPositions(entry.getKey());
if (null == list || null == list.get(0)) {
continue;
}
int pageNo = list.get(0).page;
PdfContentByte cb = ps.getOverContent(pageNo);
Rectangle rect = list.get(0).position;
// 获取域的高度
float filedHeight = rect.getHeight();
PdfPTable table = entry.getValue();
// 获取表格的高度
float tableHeight = table.getTotalHeight();
float totalWidth = rect.getRight() - rect.getLeft() - 1;
table.setTotalWidth(totalWidth);
float y = table.writeSelectedRows(0, -1, rect.getLeft(), rect.getTop(), cb);
System.out.println(y);
System.out.println("表格的高度:" + tableHeight);
System.out.println("域高度:" + filedHeight);
// if (tableHeight > filedHeight) {
// System.out.println("表格的宽度超标了");
// } else {
// for (PdfPRow row : table.getRows()) {
// System.out.println(row.getMaxHeights());
// }
// float totalWidth = rect.getRight() - rect.getLeft() - 1;
// // 设定成A4纸张的宽度 590.0F
// table.setTotalWidth(totalWidth);
// float y = table.writeSelectedRows(0, -1, rect.getLeft(), rect.getTop(), cb);
// System.out.println(y);
// }
}
}
/**
* 插入图片
*
* @author vio
* @date 2019/08/20
*/
public static void insertImg(PdfStamper ps, AcroFields s, PdfReader reader, Map<String, String> imgInsert) {
for (Map.Entry<String, String> entry : imgInsert.entrySet()) {
// 获取key对应参数
List<AcroFields.FieldPosition> list = s.getFieldPositions(entry.getKey());
if (null == list || null == list.get(0)) {
return;
}
int pageNo = list.get(0).page;
PdfContentByte cb = ps.getOverContent(pageNo);
Rectangle rect = list.get(0).position;
// float x = rect.getLeft();
// float y = rect.getBottom();
// 读图片
try {
Image image = Image.getInstance(entry.getValue());
// 根据域的大小缩放图片
image.scaleToFit(rect.getWidth(), rect.getHeight());
System.out.println(image.getHeight());
System.out.println(image.getScaledHeight());
float x = 10;
// 获取页面的高度
Rectangle rectPage = reader.getPageSize(1);
float y = rectPage.getHeight() - image.getScaledHeight() - 10;
// 添加图片
image.setAbsolutePosition(x, y);
cb.addImage(image);
} catch (Exception e) {
}
}
}
public static void addImageEnd(PdfStamper ps, PdfReader reader, List<Map<String, String>> imgPathList) throws Exception {
float imgHeightTotal = 0;
float pageHeightTotal = 0;
boolean isFisrt = true;
if (null != imgPathList && imgPathList.size() > 0) {
for (Map pathMap : imgPathList) {
// 获取默认页面高度
String imgPath = pathMap.get("src") + "";
String content = pathMap.get("content") + "";
String newImgPath = imgTempPath + UUID.randomUUID().toString().replace("-", "");
PDFUtil.waterMark(content, imgPath, newImgPath, 0, new Color(0, 0, 0), "JPG");
Rectangle rect = new Rectangle(reader.getPageSize(1));
// 获取默认页面高度
float rectHeight = rect.getHeight();
float rectWidth = rect.getWidth();
int n = reader.getNumberOfPages();
if (isFisrt) {
n = n + 1;
ps.insertPage(n, rect);
// 获取总页面高度
pageHeightTotal = pageHeightTotal + rectHeight;
isFisrt = false;
}
Image img = Image.getInstance(newImgPath);
// 获取图片高度
float imgHeight = img.getHeight();
// img.scaleToFit(400,220);
// 如果图片高度过大 裁剪
if (imgHeight > rectHeight) {
img.scaleToFit(rectWidth, rectHeight - 11);
imgHeight = rectHeight - 11;
} else if (img.getWidth() > rectWidth) {
img.scaleToFit(rectWidth, imgHeight);
imgHeight = img.getScaledHeight();
}
// 获取图片高度
imgHeightTotal = imgHeightTotal + imgHeight + 10;
if (pageHeightTotal > imgHeightTotal) {
PdfContentByte over = ps.getOverContent(n);
// 图片放在顶部
img.setAbsolutePosition(0, pageHeightTotal - imgHeightTotal);
img.setAlignment(Image.ALIGN_LEFT);
over.addImage(img);
} else {
n = n + 1;
ps.insertPage(n, rect);
PdfContentByte over = ps.getOverContent(n);
pageHeightTotal = rectHeight;
imgHeightTotal = imgHeight + 10;
// 图片放在顶部
img.setAbsolutePosition(0, pageHeightTotal - imgHeightTotal);
img.setAlignment(Image.ALIGN_LEFT);
over.addImage(img);
}
File imgFile = new File(newImgPath);
if (imgFile.exists()) {
imgFile.delete();
}
}
}
}
public static void addImageEndTextOnFirst(PdfStamper ps, PdfReader reader, BaseFont bf, Map<String, List<String>> imgPathList) throws Exception {
float imgHeightTotal = 0;
float pageHeightTotal = 0;
String text = "";
List<String> imgList = null;
boolean isFirst = false;
for (Map.Entry<String, List<String>> entry : imgPathList.entrySet()) {
text = entry.getKey();
imgList = entry.getValue();
if (null != imgList && imgList.size() > 0) {
for (String imgSrc : imgList) {
// 获取默认页面高度
Rectangle rect = new Rectangle(reader.getPageSize(1));
// 获取默认页面高度
float rectHeight = rect.getHeight();
float rectWidth = rect.getWidth();
int n = reader.getNumberOfPages();
n = n + 1;
ps.insertPage(n, rect);
Image img = Image.getInstance(imgSrc);
// 获取图片高度
float imgHeight = img.getHeight();
// img.scaleToFit(400,220);
// 如果图片高度过大 裁剪
if (imgHeight > rectHeight) {
img.scaleToFit(rectWidth, rectHeight - 11);
imgHeight = rectHeight - 11;
} else if (img.getWidth() > rectWidth) {
img.scaleToFit(rectWidth, imgHeight);
imgHeight = img.getScaledHeight();
}
PdfContentByte over = ps.getOverContent(n);
if (!isFirst) {
//加载文本
over.beginText();
//文字加粗
//设置文本描边宽度
over.setLineWidth(0.5);
//设置文本为描边模式
over.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
//设置文本为中文、和字体大小
over.setFontAndSize(bf, 18);
//设置文本绝对坐标
over.setTextMatrix(0, rectHeight-20);
//设置文本
over.showText(text);
//结束加载文本
over.endText();
isFirst = true;
}
// 图片放在顶部
img.setAbsolutePosition(0, rectHeight - imgHeight-30);
img.setAlignment(Image.ALIGN_LEFT);
over.addImage(img);
}
isFirst = false;
}
}
}
public static PdfPCell getCell(String type, String cellValue, int colspan, int rowSpan, Font font) {
PdfPCell cell = new PdfPCell();
try {
cell = new PdfPCell(new Phrase(cellValue, font));
cell.setBorder(Rectangle.BOX);
cell.setPaddingTop(2f);
cell.setRowspan(rowSpan);
cell.setMinimumHeight(20);
cell.setColspan(colspan);
if ("2".equals(type)) {
cell.setUseAscender(true);
cell.setHorizontalAlignment(cell.ALIGN_CENTER); // 设置水平居中
cell.setVerticalAlignment(cell.ALIGN_MIDDLE); // 设置垂直居中
} else {
cell.setUseAscender(true);
cell.setHorizontalAlignment(cell.ALIGN_LEFT); // 设置水平居左
cell.setVerticalAlignment(cell.ALIGN_MIDDLE); // 设置垂直居中
}
} catch (Exception ex) {
}
return cell;
}
/**
* 合并pdf
*
* @param pdfFilenames pdf文件列表
* @return
*/
public static String mergePdf(List<String> pdfFilenames) {
if (pdfFilenames.size() == 0) {
return "";
}
String outPath = tempPath + UserUtils.getUuid() + ".pdf";
String outPathWatermark = tempPath + "watermark" + File.separator + UserUtils.getUuid() + ".pdf";
try {
FileUtils.checkExist(outPath);
FileUtils.checkExist(outPathWatermark);
PdfReader reader = null;
Document doc = new Document();
PdfCopy pdfCopy = new PdfCopy(doc, new FileOutputStream(outPath));
int pageCount = 0;
doc.open();
System.out.println("--------------开始合并pdf--------------");
for (String file : pdfFilenames) {
System.out.println("合并文件:" + file);
reader = new PdfReader(file);
pageCount = reader.getNumberOfPages();
for (int j = 1; j <= pageCount; ++j) {
pdfCopy.addPage(pdfCopy.getImportedPage(reader, j));
}
}
doc.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("--------------结束合并pdf--------------");
System.out.println("合并的pdf路径:" + outPath);
System.out.println("--------------加水印开始--------------");
// waterMark(outPath, outPathWatermark, "中国科技核心期刊");
System.out.println("合并的pdf加水印路径:" + outPathWatermark);
FileUtils.deleteFile(outPath);
System.out.println("合并的pdf删除!");
System.out.println("--------------加水印结束--------------");
return outPathWatermark;
}
/**
* @param inputFile 你的PDF文件地址
* @param outputFile 添加水印后生成PDF存放的地址
* @param waterMarkName 你的水印
* @return
*/
public static boolean waterMark(String inputFile, String outputFile, String waterMarkName) {
try {
FileUtils.checkExist(outputFile);
PdfReader reader = new PdfReader(inputFile);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
outputFile));
//这里的字体设置比较关键,这个设置是支持中文的写法
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
int total = reader.getNumberOfPages() + 1;
PdfContentByte under;
Rectangle pageRect = null;
for (int i = 1; i < total; i++) {
pageRect = stamper.getReader().
getPageSizeWithRotation(i);
// 获得PDF最顶层
under = stamper.getOverContent(i);
under.saveState();
// set Transparency
PdfGState gs = new PdfGState();
// 设置透明度为0.2
gs.setFillOpacity(0.1f);
under.setGState(gs);
under.restoreState();
under.beginText();
under.setFontAndSize(base, 30);
under.setTextMatrix(90, 90);
under.setColorFill(BaseColor.LIGHT_GRAY);
for (int y = 0; y < 10; y++) {
for (int x = 0; x < 8; x++) {
// 水印文字成45度角倾斜
under.showTextAligned(Element.ALIGN_LEFT
, waterMarkName, 20 + 300 * x, 300 * y, 45);
}
}
// 添加水印文字
under.endText();
//文字加粗
under.setLineWidth(1f);
under.stroke();
}
stamper.close();
reader.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @param logoText 要写入的文字
* @param srcImgPath 源图片路径
* @param newImagePath 新图片路径
* @param degree 旋转角度
* @param color 字体颜色
* @param formaName 图片后缀
* @return
*/
public static void waterMark(String logoText, String srcImgPath, String newImagePath, Integer degree, Color color, String formaName) {
InputStream is = null;
OutputStream os = null;
try {
// 1、源图片
java.awt.Image srcImg = ImageIO.read(new File(srcImgPath));
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
// 2、得到画笔对象
Graphics2D g = buffImg.createGraphics();
// 3、设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), java.awt.Image.SCALE_SMOOTH), 0, 0, null);
// 4、设置水印旋转
if (null != degree) {
g.rotate(Math.toRadians(degree), buffImg.getWidth() / 2, buffImg.getHeight() / 2);
}
g.setColor(color);
// 6、设置水印文字Font
g.setFont(new java.awt.Font("宋体", java.awt.Font.BOLD, 15));
// 7、设置水印文字透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.45f));
// 8、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)
g.drawString(logoText, 10, 15);
// 9、释放资源
g.dispose();
// 10、生成图片
os = new FileOutputStream(newImagePath);
ImageIO.write(buffImg, formaName, os);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is)
is.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static String wordToPdf(String pdfFilePath) {
try {
String docPath = "E:\\JournalPlatform\\1.docx";
String pdfPath = "E:\\JournalPlatform\\1.pdf";
InputStream doc = new FileInputStream(docPath);
XWPFDocument document = new XWPFDocument(doc);
PdfOptions options = PdfOptions.create();
// 中文字体处理
options.fontProvider(new ITextFontRegistry() {
public com.lowagie.text.Font getFont(String familyName, String encoding, float size, int style, Color color) {
try {
Resource fileRource = new ClassPathResource("pdf/font/simfang.ttf");
String path = fileRource.getFile().getAbsolutePath();
com.lowagie.text.pdf.BaseFont bfChinese = com.lowagie.text.pdf.BaseFont.createFont(path, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
com.lowagie.text.Font fontChinese = new com.lowagie.text.Font(bfChinese, size, style, color);
if (familyName != null)
fontChinese.setFamily(familyName);
return fontChinese;
} catch (Throwable e) {
e.printStackTrace();
return ITextFontRegistry.getRegistry().getFont(familyName, encoding, size, style, color);
}
}
});
OutputStream out = new FileOutputStream(pdfPath);
PdfConverter.getInstance().convert(document, out, options);
doc.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static void main(String[] args) {
// wordToPdf("");
tempPath = "E:\\JournalPlatform";
List<String> list = new ArrayList<String>();
list.add("E:\\JournalPlatform\\1.pdf");
Map mapData = new HashMap();
mapData.put("number", "2018020015120024-001510");
mapData.put("name", "核心期刊申报");
mapData.put("rel", "王小二");
mapData.put("mail", "34145421212@qq.com");
mapData.put("time", "2019年2月2日");
PdfPTable table = new PdfPTable(3);//为了做外圈的边框
table.setWidthPercentage(100);
int[] widths = new int[]{20, 50, 80};
BaseFont bfChinese;
Font fontChinese = null;
try {
table.setWidths(widths);
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
fontChinese = new Font(bfChinese);
} catch (Exception e) {
e.printStackTrace();
}
/*
table.addCell(getCell("07.13123123123123123123", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("07.13123123123123123123", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("07.13123123123123123123", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("07.13123123123123123123", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.131", 1, 1, fontChinese));
table.addCell(getCell("07.13", 1, 1, fontChinese));
table.addCell(getCell("多云", 1, 1, fontChinese));
table.addCell(getCell("07.131", 1, 1, fontChinese));*/
Map mapDataI = new HashMap();
mapDataI.put("table", table);
List l = new ArrayList();
l.add("E:\\JournalPlatform\\1.jpg");
l.add("E:\\JournalPlatform\\1.jpg");
l.add("E:\\JournalPlatform\\2.png");
l.add("E:\\JournalPlatform\\1.jpg");
l.add("E:\\JournalPlatform\\3.png");
l.add("E:\\JournalPlatform\\2.png");
l.add("E:\\JournalPlatform\\3.png");
/* templetToPdf("", mapData, mapDataI, null, l);*/
//String s = "<div class=\"WordSection1\"><p> <span style=\"font-weight: bold; font-size: xx-large;\">填表说明</span><br><br><span style=\"font-weight: bold;\"> 一、申请条件:</span><br>2015年及以前取得国内连续出版物唯一编号(即CN号)、2016年和2017年全年完整出版的学术期刊,刊载内容以科学发现和技术创新成果为主。</p><p><br><span style=\"font-weight: bold;\"> 二、提交申请时间:</span><br>填写完成的《申请表》须在2018年3月1日前,通过电子邮件提交到cstpcd@istic.ac.cn。</p><p><br><span style=\"font-weight: bold;\">三、纸质版申请表提交方式:</span><br>将《申请表》和附件各项内容用A4打印纸打印、左侧装订制作成1份申请材料,经主编亲笔签名和主办单位及编辑部签章后,连同申请期刊2017出版的全年样刊1套,快递寄送到中国科学技术信息研究所科学计量与评价研究中心(北京海淀区复兴路15号100038,联系电话 010-58882027)</p><p><br><span style=\"font-weight: bold;\">四、请提交以下内容请作为附件</span><br>附件1:期刊出版许可证复印件。<br>附件2:能反映审稿流程的近期期刊审稿单复印件。<br>附件3:能反映期刊学术水平的2篇近期已发表的论文。<br>附件4:期刊获奖、收录和推荐材料(可选)。<br>附件5:其他材料(可选)。 </p></div>";
//
// String s1 = "<!DOCTYPE html>\n" +
// "<html lang=\"en\">\n" +
// "<head>\n" +
// "</head>\n" +
// "\n" +
// "<body>\n"
// + s
// +"</body>"
// + "</html>";
// createPDF(s1);
// String s = "<!DOCTYPE html>\n" +
// "<html lang=\"en\">\n" +
// "<head>\n" +
// " <style type=\"text/css\">\n" +
// "\t\tspan {\n" +
// " background-color:black;\n" +
// " }\n" +
// "\t</style>\n" +
// "</head>\n" +
// "\n" +
// "<body style=\"width:100%;height:100%; background-color:red; overflow-x: hidden; overflow-y: hidden\">\n" +
// "<div>\n" +
// " <span onclick=\"move()\">A</span>\n" +
// " <span>B</span>\n" +
// "</div>\n" +
// "\n" +
// "</body>"
// + "</html>";
// try {
// createPDF(s);
// } catch (Exception e) {
// e.printStackTrace();
// }
}
}
class AsianFontProvider extends XMLWorkerFontProvider {
@Override
public Font getFont(final String fontname, String encoding, float size, final int style) {
try {
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
return new Font(bfChinese, size, style);
} catch (Exception e) {
}
return super.getFont(fontname, encoding, size, style);
}
}
java生成pdf(插入图片,插入表格,插入文字)的工具类
最新推荐文章于 2024-03-19 10:29:56 发布