最近闲着没事做,就想着如何优化这个PDF添加页码f的执行效率,我尝试了之前提供的思路,发现不能从根本来解决问题。
首先贴出我电脑的配置 我的用的是华硕的电脑 处理器是AMD R5900HX 内存是16G 3200
我开启多线程来处理这个问题,无论我怎样设置都会出现这个 内存溢出 Java heap space的报错
于是我开始调试jvm
我这是idea 2022版的 下面是对jvm的设置
发现还是无法从根本解决这个问题,于是我思考是不是代码本身造成了内存过多。经过一些调试后发现真的是代码本身的问题。这段代码也是我直接在网上找的一段别人写的代码。
最后得出一个结论就是自己强大才是真的强大!
好了不啰嗦了,上代码 当然这是测试代码,写法不优雅,这里只是为了解决问题而写的,问题就解决了,思路给出来了其他的交给小伙伴们去解决 如果到这里还不能理解的可以百度一波堆栈的区别。(学无止境)
package com.example.demo.test;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.automaticfields.PdfCompositeField;
import com.spire.pdf.automaticfields.PdfPageCountField;
import com.spire.pdf.automaticfields.PdfPageNumberField;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
/**
* @Author long
* @Date 2022/1/13 16:59
* @Version 1.0
*/
public class HeaderFooter {
private static Font fon = new Font("黑体", Font.PLAIN, 8);
private static PdfPageNumberField number = new PdfPageNumberField();
private static PdfPageCountField count = new PdfPageCountField();
private static PdfPen pen = new PdfPen(PdfBrushes.getGray(), 0.5f);
private static PdfTrueTypeFont font = new PdfTrueTypeFont(fon, true);
private static PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.getBlack(), "第{0}页 共{1}页", number, count);
private static PdfStringFormat pdfStringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top);
private static Rectangle2D.Float rfloat = new Rectangle2D.Float();
public static void main(String[] args) {
//System.out.println(Runtime.getRuntime().availableProcessors());
doPages("C:\\test\\pdf0.pdf", "C:\\test\\temp\\pdf3.pdf");
}
//添加页码
public static void doPages(String inPath, String outPath) {
//创建pdfDocument对象,并加载PDF文档
PdfDocument doc = new PdfDocument();
doc.loadFromFile(inPath);
//删除有红色字体的一页: Evaluation Warning : The document was created with Spire.PDF for Java.
doc.getPages().add();
doc.getPages().remove(doc.getPages().get(doc.getPages().getCount() - 1));
//添加页眉
//drawHeader(doc);
//添加页脚
drawFooter(doc);
//保存文档
doc.saveToFile(outPath);
}
//添加页眉
public static void drawHeader(PdfDocument doc) {
//获取页面尺寸
Dimension2D pageSize = doc.getPages().get(0).getSize();
//定义两个float变量
float x = 90;
float y = 20;
for (int i = 0; i < doc.getPages().getCount(); i++) {
//添加图片到指定位置
PdfImage headerImage = PdfImage.fromFile("C:\\Users\\long\\Desktop\\logo.png");
float width = headerImage.getWidth() / 2;
float height = headerImage.getHeight() / 2;
doc.getPages().get(i).getCanvas().drawImage(headerImage, x, y, width, height);
//doc.getPages().get(i).getCanvas().drawLine(pen, x, y + height + 1, pageSize.getWidth() - x, y + height + 1);
}
}
//添加页脚
public static void drawFooter(PdfDocument doc) {
//获取页面大小
Dimension2D pageSize = doc.getPages().get(0).getSize();
//定义两个float变量
float x = 90;
float y = (float) pageSize.getHeight() - 72;
long startTime = System.currentTimeMillis();
// Font fon = new Font("黑体", Font.PLAIN, 8);
// PdfPageNumberField number = new PdfPageNumberField();
// PdfPageCountField count = new PdfPageCountField();
// PdfPen pen = new PdfPen(PdfBrushes.getGray(), 0.5f);
// PdfTrueTypeFont font = new PdfTrueTypeFont(fon, true);
// PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.getBlack(), "第{0}页 共{1}页", number, count);
// PdfStringFormat pdfStringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top);
// Rectangle2D.Float rfloat = new Rectangle2D.Float();
int pagecCount = doc.getPages().getCount();
for (int i = 0; i < pagecCount; i++) {
doc.getPages().get(i).getCanvas().drawLine(pen, x, y, pageSize.getWidth() - x, y);
compositeField.setStringFormat(pdfStringFormat);
Dimension2D fontSize = font.measureString(compositeField.getText());
rfloat.setRect((pageSize.getWidth() - x - fontSize.getWidth()),y,fontSize.getWidth(),fontSize.getHeight());
compositeField.setBounds(rfloat);
compositeField.draw(doc.getPages().get(i).getCanvas());
}
long endTime = System.currentTimeMillis();
System.out.println("运行时长:" + (endTime - startTime));
}
}
这是用静态的