修改PDF

package bean.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileUtil {

    private static Map<String,String> files = new HashMap<>();

    public static Map<String,String> getAllFiles(String path) {
        System.out.println(getFile(path));
        return files;
    }

    public static String getFile(String path) {
        File rootDir = new File(path);
        if (!rootDir.isDirectory()) {
            System.out.println("find file: " + rootDir.getName());
            files.put(rootDir.getName(),rootDir.getAbsolutePath());
        } else {
            String[] fileList = rootDir.list();
            for (int i = 0; i < fileList.length; i++) {
                path = rootDir.getAbsolutePath() + "\\" + fileList[i];
                getFile(path);
            }
        }
        return "success";
    }


    public static void main(String[] args) {
        Map<String, String> allFiles = getAllFiles("Z:\\default\\Desktop\\TestPdf");
        System.out.println(allFiles);
    }
}
package bean;

import bean.utils.FileUtil;
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 com.spire.pdf.widget.PdfPageCollection;

import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
import java.util.List;
import java.util.Map;


public class PdfTest {

    private static String rootPath = "Z:\\default\\Desktop\\TestPdf";
    private static String originDir = "cp";
    private static String resultDir = "result";

    public static void main(String[] args) {
//        //创建pdfDocument对象,并加载PDF文档
//        PdfDocument doc = new PdfDocument();
//        doc.loadFromFile("Z:\\default\\Desktop\\TestPdf\\124501-9-3-9.pdf");
//        String filename = "167500-9-3-2";
//
//        //添加页眉
        drawHeader(doc);
//        //添加页脚
//        drawHeader(doc, filename);
//        //保存文档
//        doc.saveToFile("Z:\\default\\Desktop\\TestPdf\\result.pdf");
//        System.out.println("success");

        String s = dealAllFile(rootPath + "\\" + originDir);
        System.out.println(s);

    }

    public static String dealAllFile(String path) {
        Map<String, String> allFiles = FileUtil.getAllFiles(path);
        allFiles.forEach((filename, filepath) -> {
            System.out.println("filename: " + filename + "    filepath: " + filepath);
//            String p = filepath.replaceAll("\\", "\\\\");
//            System.out.println(p);
            int index = filename.indexOf(".");
            filename = filename.substring(0,index);
            System.out.println(filename);
            PdfDocument doc = new PdfDocument();
            doc.loadFromFile(filepath);
            drawHeader(doc, filename);
            doc.saveToFile(rootPath + "\\" + resultDir + "\\" + filename + ".pdf");
        });
        return "success";
    }


    public static void drawHeader(PdfDocument doc, String filename) {
        //获取页面尺寸
//        Dimension2D pageSize = doc.getPages().get(0).getSize();
        //定义两个float变量
        float x = (float) pageSize.getWidth() - 240;
        float y = 20;

        PdfPageCollection pages = doc.getPages();
        for (int i = 0; i < doc.getPages().getCount(); i++) {
            Dimension2D pageSize = pages.get(i).getSize();
//            float x = (float) pageSize.getWidth() - 240;
//            float y = 20;
            float x = (float) pageSize.getWidth() - 20;
            float y = (float)pageSize.getHeight() - 240;
            //添加横线到指定位置
//            PdfPen pen = new PdfPen(PdfBrushes.getGray(), 0.5f);
//            doc.getPages().get(i).getCanvas().drawLine(pen, x, y, x, y + 240);
            //添加文本到指定位置
//            y = y + 8;
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Cambria", Font.PLAIN, 24), true);
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
//            String footerText = filename + "/" + (i + 1);
            String footerText = "1\n2\n3\n";
            doc.getPages().get(i).getCanvas().drawString(footerText, font, PdfBrushes.getBlack(), x, y, format);
            doc.getPages().get(i).getCanvas().translateTransform();
            System.out.println("success pages: " + (i + 1));

        }
    }



    public static void drawFooter(PdfDocument doc) {
        //获取页面大小
        Dimension2D pageSize = doc.getPages().get(0).getSize();
        //定义两个float变量
        float x = 90;
        float y = (float) pageSize.getHeight() - 72;
        for (int i = 0; i < doc.getPages().getCount(); i++) {
            //添加横线到指定位置
            PdfPen pen = new PdfPen(PdfBrushes.getGray(), 0.5f);
            doc.getPages().get(i).getCanvas().drawLine(pen, x, y, pageSize.getWidth() - x, y);
            //添加文本到指定位置
            y = y + 8;
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Cambria", Font.PLAIN, 8), true);
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
            String footerText = "姓名:Lily\n联系电话:0101112222\n网站:www.Lilyhome.com";
            doc.getPages().get(i).getCanvas().drawString(footerText, font, PdfBrushes.getBlack(), x, y, format);
            //添加页码
//            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 fontSize = font.measureString(compositeField.getText());
//            compositeField.setBounds(new Rectangle2D.Float((float) (pageSize.getWidth() - x - fontSize.getWidth()), y
//                    , (float) fontSize.getWidth(), (float) fontSize.getHeight()));
//            compositeField.draw(doc.getPages().get(i).getCanvas());
            System.out.println("success pages: " + (i + 1));
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值