itext7 研究 结果 实在研究不下去了没有itext5用着顺手 好的一点是 字体支持小数

package online.difei.itext7;



import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.color.DeviceCmyk;
import com.itextpdf.kernel.color.DeviceRgb;
import com.itextpdf.kernel.color.Separation;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.function.PdfFunction;
import com.itextpdf.kernel.utils.PdfMerger;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.property.TextAlignment;

import java.io.*;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class HelloItext7 {
    public static final String DEST = "C:\\Users\\Administrator\\Desktop\\hello.pdf";
    public static Color color = new DeviceCmyk(100,0,0,0);
    public static String imgPath = "C:\\Users\\Administrator\\Desktop\\123.png";
    public static String fontPath = "D:\\font\\msyh.ttf";

    public static void main(String args[]) throws IOException {
        final PdfDocument pdf = new PdfDocument(new PdfWriter(DEST));
        Document document = new Document(pdf);

        pdf.addNewPage();
        pdf.addNewPage();
        pdf.addNewPage();

        PdfPage page = pdf.getPage(1);
        final PdfCanvas pdfCanvas = new PdfCanvas(page);

        //---------------------------------------画文字-------------------------------------------
        addText(pdfCanvas,pdf,color,fontPath,12.5f,"jialan75",10,25,TextAlignment.LEFT,1f);

        //-----------------------------------------线条-------------------------------------
        List<List<Float>> pointList = new ArrayList<List<Float>>();
        pointList.add(Arrays.asList(new Float[]{0f,100f}));
        pointList.add(Arrays.asList(new Float[]{100f,100f}));
        pointList.add(Arrays.asList(new Float[]{100f,200f}));
        pointList.add(Arrays.asList(new Float[]{50f,50f}));
        addLine(pdfCanvas,color,pointList,0.1f);
        //-----------------------------------------图片-------------------------------------

        /**
         * 插入一个图片  宽度150  高度 按比例  坐标 800,800
         */
        addImage(pdfCanvas,pdf,imgPath,150,400,400);
        /**
         * 插入一个图片 在第二页 宽度150 高度 按比例 坐标 10,10
         */
        addImage(document,imgPath,2,150,10,10);
        /**
         * 插入一个图片 在第三页 宽度100 高度 按比例 坐标 200,200
         */
        addImage(document,imgPath,3,100,200,200);
        document.close();
        pdf.close();

        //----------------------------------合并-----------------------------------------------
        List<String> paths = new ArrayList<String>();
        paths.add("C:\\Users\\Administrator\\Desktop\\hello4.pdf");
        paths.add("C:\\Users\\Administrator\\Desktop\\hello5.pdf");
        pdfMerger(paths,"C:\\Users\\Administrator\\Desktop\\hello3.pdf");
    }

    /**
     * 画图片(高度按比例)
     * @param document  PDF文档
     * @param imgPath   图片路径
     * @param pageIndex 页码
     * @param w         图片宽度
     * @param x         图片X坐标
     * @param y         图片Y坐标
     * @return          ture添加成功  false添加失败
     * @date 2021年11月4日22:50:25
     * @author jialan75
     */
    public static boolean addImage(Document document,String imgPath,int pageIndex,float w,float x,float y) throws MalformedURLException {
        Image image  = new Image(ImageDataFactory.create(imgPath));
        if(image==null)return false;
        image.setFixedPosition(pageIndex,x,y,w);
        document.add(image);
        return true;
    }

    /**
     * 画图片(高度按比例)
     * @param pdfCanvas PdfDocument
     * @param pdf       PdfCanvas
     * @param imgPath   图片路径
     * @param w         图片宽度
     * @param x         图片X坐标
     * @param y         图片Y坐标
     * @return          ture添加成功  false添加失败
     * @throws MalformedURLException
     * @date 2021年11月4日22:50:25
     * @author jialan75
     */
    public static boolean addImage(PdfCanvas pdfCanvas,PdfDocument pdf,String imgPath,float w,float x,float y) throws MalformedURLException {
        Image image = new Image(ImageDataFactory.create(imgPath),x,y,w);
        if(image==null)return false;
        Canvas canvas = new Canvas(pdfCanvas,pdf,new Rectangle(0,0));
        canvas.add(image);
        return true;
    }

    /**
     * 画线条
     * @param pdfCanvas pdfCanvas
     * @param color     线条颜色
     * @param pointList 坐标点
     * @param lineWidth 线宽
     * @date 2021年11月4日22:50:25
     * @author jialan75
     */
    public static void addLine(PdfCanvas pdfCanvas,Color color,List<List<Float>> pointList,float lineWidth) {
        if (pointList.isEmpty()) {
            return;
        }
        if(pointList.size()<2){
            return;
        }
        List<Float> firstPoint = pointList.get(0);
        pdfCanvas.moveTo(firstPoint.get(0),firstPoint.get(1));
        for (int i = 1; i < pointList.size(); i++) {
            List<Float> point = pointList.get(i);
            pdfCanvas.lineTo(point.get(0),point.get(1));
        }
        pdfCanvas.setLineWidth(lineWidth);
        pdfCanvas.setStrokeColor(color);
        pdfCanvas.stroke();

    }

    /**
     * 添加文字
     * @param pdfCanvas PdfCanvas
     * @param pdf       PdfDocument
     * @param color     文字颜色
     * @param fontPath  字体路径
     * @param fontSize  字号
     * @param content   内容
     * @param x         X坐标
     * @param y         Y坐标
     * @param alignment 对齐方式
     * @param opacity   透明度
     * @return
     * @throws IOException
     * @date 2021年11月4日22:50:25
     * @author jialan75
     */
    public static boolean addText(PdfCanvas pdfCanvas,PdfDocument pdf,Color color,String fontPath,float fontSize,String content,float x,float y,TextAlignment alignment,float opacity) throws IOException {
        if(!new File(fontPath).exists()){
            return false;
        }
        PdfFont font = PdfFontFactory.createFont(fontPath, PdfEncodings.IDENTITY_H, true);
        if(null == font){
            return false;
        }
        Canvas canvas = new Canvas(pdfCanvas,pdf,new Rectangle(pdf.getFirstPage().getPageSize()));
        canvas.setFontColor(color,opacity);
        canvas.setFontSize(fontSize);
        canvas.setFont(font);
        canvas.showTextAligned(content,x,y,alignment);
        return true;
    }

    /**
     * 获取专色根据 RGB色值
     * @param name  专色名称
     * @param r
     * @param g
     * @param b
     * @param tint
     * @date 2021年11月4日22:50:25
     * @author jialan75
     * @return
     */
    public static Separation createSpotColorRGB(String name, int r,int g,int b,float tint){
        DeviceRgb alternateSpace = new DeviceRgb(r, g, b);
        PdfArray one =new PdfArray(new float[]{0.0f, 1f});
        PdfArray two = null;
        PdfArray three =  new PdfArray(new float[] { 1f, 1f, 1f });
        PdfArray four = new PdfArray(new float[] { alternateSpace.getColorValue()[0], alternateSpace.getColorValue()[1], alternateSpace.getColorValue()[2]});
        PdfNumber five =new PdfNumber(1f);
        PdfFunction tintTransform = new PdfFunction.Type2(one,two,three,four,five);
        Separation spot = new Separation(name, alternateSpace.getColorSpace(), tintTransform, tint);
        return spot;
    }

    /**
     * 创建专色根据 CMYK值
     * @param name  专色名称
     * @param c
     * @param m
     * @param y
     * @param k
     * @param tint
     * @return
     * @date 2021年11月4日22:50:25
     * @author jialan75
     */
    public static Separation createSpotColorCMYK(String name, int c, int m, int y, int k,float tint){
        DeviceCmyk alternateSpace = new DeviceCmyk(c, m,y,k);
        PdfArray one =new PdfArray(new float[]{0.0f, 1f});
        PdfArray two = null;
        PdfArray three =  new PdfArray(new float[] { 1f, 1f, 1f , 1f });
        PdfArray four = new PdfArray(new float[] { alternateSpace.getColorValue()[0], alternateSpace.getColorValue()[1], alternateSpace.getColorValue()[2], alternateSpace.getColorValue()[3]});
        PdfNumber five =new PdfNumber(1f);
        PdfFunction tintTransform = new PdfFunction.Type2(one,two,three,four,five);
        Separation spot = new Separation(name, alternateSpace.getColorSpace(), tintTransform, tint);
        return spot;
    }
    /**
     *
     * @Title: pdfMerger
     * @Description: PDF合并
     * @param paths 需要合并的所有文件路径(路径+名称)
     * @param outputPath 合并后的文件路径(路径+名称)
     * 	void
     * @author yanghainan
     * @date 2020年7月3日下午3:21:00
     */
    public static boolean pdfMerger(List<String> paths, String outputPath) {

        if (paths == null || paths.isEmpty() || null==outputPath||"".equals(outputPath)) {
            return false;
        }
        // 首页与其他页创建方式不同,所以需要创建两个
        PdfDocument firstSourcePdf = null;
        PdfDocument secondSourcePdf = null;
        // 合并需要的工具类
        PdfMerger merger = null;
        try {
            for (int i = 0; i < paths.size(); i++) {
                if (i == 0) {
                    PdfWriter pdfWriter = new PdfWriter(outputPath);
                    // 启用完全压缩
//					pdfWriter.isFullCompression();
                    firstSourcePdf = new PdfDocument(new PdfReader(paths.get(i)), pdfWriter);
                    merger = new PdfMerger(firstSourcePdf);
                    continue;
                }
                secondSourcePdf = new PdfDocument(new PdfReader(paths.get(i)));
                merger.merge(secondSourcePdf, 1, secondSourcePdf.getNumberOfPages());
                secondSourcePdf.close();
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            if (firstSourcePdf != null) {
                firstSourcePdf.close();
            }
            if (secondSourcePdf != null) {
                secondSourcePdf.close();
            }
            if (merger != null) {
                merger.close();
            }
        }
        return false;
    }

}
上一篇:一文详解常见医学自然语言理解任务和算法


下一篇:【论文收集】几种经典神经网络及其模型