package com.bohuikeji.frame.module.admin.easypdf.my;

import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import java.io.IOException;

public class TextUtils {

    //public static final  String fontPath = "C:\\Windows\\Fonts\\STSONG.TTF";
    //public static final PDFont font = PDType1Font.SYMBOL;
    public static PDPageContentStream drawRow(PDPageContentStream contents,PDFont font,float fontSize,float x,float y,String text) throws IOException {
        contents.beginText();
        contents.setFont(font, fontSize);
        contents.newLineAtOffset(x, y);
        contents.showText(text);
        contents.endText();
        return contents;

    }
    public static PDPageContentStream drawRows(PDPageContentStream contents,PDFont font,float fontSize,float x,float y,int rowSize,String text) throws IOException {
        int length=text.length();
        //根据计算10号字体一行占用大约15个汉字，如果length正好为15的倍数则循环次数减1
        for(int i=0;i<=((length%rowSize)>0?(length/rowSize):(length/rowSize)-1);i++){
            int end=(i+1)*rowSize;
            if(end>=length){
                end=length;
            }
            //换行绘制收件地址
            drawRow(contents,font,fontSize,x,y-(i*16),
                    text.substring(i*rowSize,end));
        }
        return contents;
    }
}
