栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

PDF头部报错:Evaluation Warning : The document was created with Spire.PDF for Java.

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PDF头部报错:Evaluation Warning : The document was created with Spire.PDF for Java.

问题描述

今天生成PDF缝骑章的时候遇到一个问题,那就是每一个文件第一页都会有这个错

会在页面的第一页加上Evaluation Warning : The document was created with Spire.PDF for Java.一段文字

该备注只会标记再报表的第一页的顶部。我们可以新增一页,并删掉第一页即可

解决思路

最终通过网上找例子找到了解决办法,因为这段文字只出现在第一页,所以这里的处理方式是在文档创建时先添加一个空白页,最后再把空白页去掉

代码实现

核心代码

 //添加一个空白页,目的为了删除jar包添加的水印,后面再移除这一页
        pdf.getPages().add();
        //创建字体
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("宋体", Font.PLAIN, 10),true);
        //遍历文档中的页
        for (int i = 0; i < pdf.getPages().getCount(); i++) {
            Dimension2D pageSize = pdf.getPages().get(i).getSize();
            float y = (float) pageSize.getHeight() - 40;
            //初始化页码域
            PdfPageNumberField number = new PdfPageNumberField();
            //初始化总页数域
            PdfPageCountField count = new PdfPageCountField();
            //创建复合域
            PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.getBlack(), "第{0}页 共{1}页", number, count);
            //设置复合域内文字对齐方式
            compositeField.setStringFormat(new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top));
            //测量文字大小
            Dimension2D textSize = font.measureString(compositeField.getText());
            //设置复合域的在PDF页面上的位置及大小
            compositeField.setBounds(new Rectangle2D.Float(((float) pageSize.getWidth() - (float) textSize.getWidth())/2, y, (float) textSize.getWidth(), (float) textSize.getHeight()));
            //将复合域添加到PDF页面
            compositeField.draw(pdf.getPages().get(i).getCanvas());
        }
        //移除第一个页
        pdf.getPages().remove(pdf.getPages().get(pdf.getPages().getCount()-1));

完整代码

package dmyz.util;

import com.spire.pdf.*;
import com.spire.pdf.automaticfields.PdfCompositeField;
import com.spire.pdf.automaticfields.PdfPageCountField;
import com.spire.pdf.automaticfields.PdfPageNumberField;
import com.spire.pdf.graphics.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;



public class AcrossPageSeal {
    public static void main(String[] args) throws IOException {
        //要生成的文件模板
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("D:\File\test\wyh\3页.pdf");
        //添加一个空白页,目的为了删除jar包添加的水印,后面再移除这一页
        pdf.getPages().add();
        //创建字体
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("宋体", Font.PLAIN, 10),true);
        //遍历文档中的页
        for (int i = 0; i < pdf.getPages().getCount(); i++) {
            Dimension2D pageSize = pdf.getPages().get(i).getSize();
            float y = (float) pageSize.getHeight() - 40;
            //初始化页码域
            PdfPageNumberField number = new PdfPageNumberField();
            //初始化总页数域
            PdfPageCountField count = new PdfPageCountField();
            //创建复合域
            PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.getBlack(), "第{0}页 共{1}页", number, count);
            //设置复合域内文字对齐方式
            compositeField.setStringFormat(new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top));
            //测量文字大小
            Dimension2D textSize = font.measureString(compositeField.getText());
            //设置复合域的在PDF页面上的位置及大小
            compositeField.setBounds(new Rectangle2D.Float(((float) pageSize.getWidth() - (float) textSize.getWidth())/2, y, (float) textSize.getWidth(), (float) textSize.getHeight()));
            //将复合域添加到PDF页面
            compositeField.draw(pdf.getPages().get(i).getCanvas());
        }
        //移除第一个页
        pdf.getPages().remove(pdf.getPages().get(pdf.getPages().getCount()-1));
        //获取分割后的印章图片
        BufferedImage[] images = GetImage(pdf.getPages().getCount());
        float x = 0;
        float y = 0;
        //实例化PdfUnitConvertor类
        PdfUnitConvertor convert = new PdfUnitConvertor();
        PdfPageBase pageBase;
        //将图片绘制到PDF页面上的指定位置
        for (int i = 0; i < pdf.getPages().getCount(); i++)
        {
            BufferedImage image= images[ i ];
            pageBase = pdf.getPages().get(i);
            x = (float)pageBase.getSize().getWidth() - convert.convertUnits(image.getWidth(), PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel) + 40;
            y = (float) pageBase.getSize().getHeight()/ 2;
            pageBase.getCanvas().drawImage(PdfImage.fromImage(image), new Point2D.Float(x, y));
        }
        System.out.println("x = " + x);
        System.out.println("y = " + y);
        //最终生成缝骑章   的结果
        pdf.saveToFile("D:\File\test\wyh\Result.pdf");

    }

    //定义GetImage方法,根据PDF页数分割印章图片
    static BufferedImage[] GetImage(int num) throws IOException {
        String originalImg = "D:\File\test\wyh\魏一鹤的测试印章.png";
        BufferedImage image = ImageIO.read(new File(originalImg));
        int rows = 1;
        int cols = num;
        int chunks = rows * cols;
        int chunkWidth = image.getWidth() / cols;
        int chunkHeight = image.getHeight() / rows;
        int count = 0;
        BufferedImage[] imgs = new BufferedImage[ chunks ];
        for (int x = 0; x < rows; x++) {
            for (int y = 0; y < cols; y++) {
                imgs[ count ] = new BufferedImage(chunkWidth, chunkHeight, image.getType());
                Graphics2D gr = imgs[ count++ ].createGraphics();
                gr.drawImage(image, 0, 0, chunkWidth, chunkHeight,
                        chunkWidth * y, chunkHeight * x,
                        chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, Color.WHITE,null);
                gr.dispose();
            }
        }
        return imgs;
    }

}
处理前

处理后

错误已经消息

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/996235.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号