• 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));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    完整代码

    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;
    
    /**
     * @Author  魏一鹤
     * @Description  骑缝章生成
     * @Date 17:03 2022/6/27
    **/
    
    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;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101

    处理前

    在这里插入图片描述

    处理后

    错误已经消息
    在这里插入图片描述

  • 相关阅读:
    艾美捷ProSci丨ProSci I kappa B 激酶检测套装解决方案
    基于Java+SpringBoot+Thymeleaf+Mysql疫情疫苗预约系统学习系统设计与实现
    intellij idea的快速配置详细使用
    【MyBatis框架】核心配置文件讲解
    ProtocolBuffers(protobuf)详解
    MyEclipse 用tomcat部署SSM项目后,项目名称和当前项目不一致
    图像特征算法---ORB算法的python实现
    Vue——axios的二次封装
    Android 9.0 蓝牙功能之一:蓝牙设置
    misc类设备驱动0——板载蜂鸣器驱动测试
  • 原文地址:https://blog.csdn.net/weixin_46713508/article/details/125488915