zoukankan      html  css  js  c++  java
  • itextpdf 和 pdfbox 测试用例

    itextpdf

    
    
    public class Itext {
        private TPrinterOrientation orientation;
        private int pageHeight;
        private int pageWidth;
        private int pageNumber;
        public static int nLineSpace;
        private String textPath;
        private Document document;
        private static PdfWriter writer;
        private static Paragraph elements;
        private BaseFont baseFont;
        private Canvas canvas;
        private String title;
    
    
        public Itext() {
            canvas = new Canvas();
        }
    
        public void beginDoc() {
            String returnVal = "";
            String path = null;
            try {
                path = ResourceUtils.getURL("classpath:").getPath();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            File pdfMkdir = new File(path + "static/pdf/");
            if (!pdfMkdir.exists()) {
                pdfMkdir.mkdir();
            }
            Date dt = new Date();
            this.setTextPath(dt.getTime() + "toPrinter.pdf");
            returnVal = path + "static/pdf/" + this.getTextPath();
            File file = new File(returnVal);
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            this.setDocument(new Document(this.paperOrientation(), 0, 36, 69, 36));
            try {
                writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            nLineSpace = CommonUtil.div(pageHeight, 24);
            this.getDocument().open();
        }
    
        private Rectangle paperOrientation() {
            Rectangle pagesize = PageSize.A4;
            TPrinterOrientation orientation = this.getOrientation();
            if (orientation.getName().equals("vertical")) {
                pagesize = PageSize.A4;
            }
            if (orientation.getName().equals("horizontal")) {
                pagesize = PageSize.A4.rotate();
            }
            return pagesize;
        }
        public void newPage() {
            this.getDocument().newPage();
            this.setPageNumber(this.getPageNumber() + 1);
        }
        public void endDoc() {
            this.getDocument().close();
            writer.close();
            Report report = new Report();
            report.getValue().add(getTextPath());
            HttpSession session = WebUtils.getSession();
            if (session != null) {
                Report reports = (Report) session.getAttribute(Report.KEY);
                if (reports != null) {
                    reports.getValue().addAll(report.getValue());
                    report.setValue(reports.getValue());
                }
                session.setAttribute(Report.KEY, report);
            }
        }
        public TPrinterOrientation getOrientation() {
            return orientation;
        }
        public void setOrientation(TPrinterOrientation orientation) {
            this.orientation = orientation;
        }
    
        public BaseFont getBaseFont() {
            return baseFont;
        }
    
        public void setBaseFont(BaseFont baseFont) {
            this.baseFont = baseFont;
        }
    
        public Document getDocument() {
            return document;
        }
    
        public void setDocument(Document document) {
            this.document = document;
        }
    
        public String getTextPath() {
            return textPath;
        }
    
        public void setTextPath(String textPath) {
            this.textPath = textPath;
        }
    
        public Canvas getCanvas() {
            return canvas;
        }
    
        public void setCanvas(Canvas canvas) {
            this.canvas = canvas;
        }
    
        public int getPageWidth() {
            return pageWidth;
        }
    
        public void setPageWidth(int pageWidth) {
            this.pageWidth = pageWidth;
        }
    
        public int getPageHeight() {
            return pageHeight;
        }
    
        public void setPageHeight(int pageHeight) {
            this.pageHeight = pageHeight;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public int getPageNumber() {
            return pageNumber;
        }
    
        public void setPageNumber(int pageNumber) {
            this.pageNumber = pageNumber;
        }
    
        public class Canvas {
            public ItextFont font;
            public Canvas() {
                font = new ItextFont();
            }
            public void textOut(int num, int top, String data) {
                String fontpath = SysUtils.getResourceFile("/config/font/" + getFont().getFontName()).toString();
                fontpath += ",1";
                try {
                    setBaseFont(BaseFont.createFont(fontpath, "shift-jis", BaseFont.EMBEDDED));
                } catch (DocumentException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                elements = new Paragraph(data, new Font(getBaseFont(), getFont().getFontSize()));
                elements.setLeading(35f);
                try {
                    getDocument().add(elements);
                } catch (DocumentException e) {
                    e.printStackTrace();
                }
            }
    
            public TextExtent textExtent(String data) {
                return new TextExtent();
            }
    
            public ItextFont getFont() {
                return font;
            }
    
            public void setFont(ItextFont font) {
                this.font = font;
            }
    
        }
    
        public class ItextFont {
            private String fontName;
            private int fontSize;
    
            public String getFontName() {
                return fontName;
            }
    
            public void setFontName(String fontName) {
                this.fontName = fontName;
            }
    
            public int getFontSize() {
                return fontSize;
            }
    
            public void setFontSize(int fontSize) {
                this.fontSize = fontSize;
            }
        }
    
    }
    
    

    pdfbox

            <dependency>
                <groupId>org.apache.pdfbox</groupId>
                <artifactId>pdfbox</artifactId>
                <version>2.0.23</version>
            </dependency>
    
    
    
    public class PDFbox {
    
        private TPrinterOrientation orientation;
        private int pageHeight;
        private int pageWidth;
        private int pageNumber;
        private int nLineSpace;
        private String textPath;
        private PDDocument document;
        private PDPage page;
        private PDPageContentStream con;
        private Canvas canvas;
        private String title;
    
    
        public PDFbox() {
    
        }
    
        public void beginDoc() {
            document = new PDDocument();
            canvas = new Canvas();
            newPage();
    
            nLineSpace = CommonUtil.div(pageHeight, 24);
        }
    
    
        public void newPage() {
    
            TPrinterOrientation orientation = this.getOrientation();
    
            if (orientation.getName().equals("vertical")) {
                page = new PDPage(PDRectangle.A4);
            }
    
            if (orientation.getName().equals("horizontal")) {
                page = new PDPage(new PDRectangle(PDRectangle.A4.getHeight(), PDRectangle.A4.getWidth()));
            }
    
            this.document.addPage(page);
    
            try {
                if (con != null) {
                    con.endText();
                    con.close();
                }
                con = new PDPageContentStream(document, page);
                con.beginText();
                con.newLineAtOffset(0, 773);
                con.setLeading(35f);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
        public void endDoc() {
    
            String path;
            try {
                path = ResourceUtils.getURL("classpath:").getPath();
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
            File pdfMkdir = new File(path + "static/pdf/");
            if (!pdfMkdir.exists()) {
                pdfMkdir.mkdir();
            }
    
            String pdfname = new Date().getTime() + "toPrinter.pdf";
            this.setTextPath(path + "static/pdf/" + pdfname);
    
    
            try {
                con.endText();
                con.close();
                con = null;
                canvas = null;
                document.save(getTextPath());
                document.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
    
    
            Report report = new Report();
            report.getValue().add(pdfname);
            HttpSession session = WebUtils.getSession();
            if (session != null) {
                Report reports = (Report) session.getAttribute(Report.KEY);
                if (reports != null) {
                    reports.getValue().addAll(report.getValue());
                    report.setValue(reports.getValue());
                }
                session.setAttribute(Report.KEY, report);
            }
    
        }
    
        public void abort() {
    
        }
    
        public TPrinterOrientation getOrientation() {
            return orientation;
        }
    
        public void setOrientation(TPrinterOrientation orientation) {
            this.orientation = orientation;
        }
    
        public PDDocument getDocument() {
            return document;
        }
    
        public void setDocument(PDDocument document) {
            this.document = document;
        }
    
        public String getTextPath() {
            return textPath;
        }
    
        public void setTextPath(String textPath) {
            this.textPath = textPath;
        }
    
        public Canvas getCanvas() {
            return canvas;
        }
    
        public void setCanvas(Canvas canvas) {
            this.canvas = canvas;
        }
    
        public int getPageWidth() {
            return pageWidth;
        }
    
        public void setPageWidth(int pageWidth) {
            this.pageWidth = pageWidth;
        }
    
        public int getPageHeight() {
            return pageHeight;
        }
    
        public void setPageHeight(int pageHeight) {
            this.pageHeight = pageHeight;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public int getPageNumber() {
            return pageNumber;
        }
    
        public void setPageNumber(int pageNumber) {
            this.pageNumber = pageNumber;
        }
    
        public int getnLineSpace() {
            return nLineSpace;
        }
    
        public void setnLineSpace(int nLineSpace) {
            this.nLineSpace = nLineSpace;
        }
    
        public class Canvas {
    
            private PdfBoxFont font;
    
            public Canvas() {
                font = new PdfBoxFont();
            }
    
            public void textOut(int num, int top, String data) {
                try {
    
                    con.setFont(getFont().getFontType(), getFont().getFontSize());
                    con.newLine();
                    con.showText(data);
    
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
    
            public TextExtent textExtent(String data) {
                return new TextExtent();
            }
    
            public PdfBoxFont getFont() {
                return font;
            }
    
            public void setFont(PdfBoxFont font) {
                this.font = font;
            }
        }
    
        public class PdfBoxFont {
    
            private String fontName;
    
            private int fontSize;
    
            private PDFont fontType;
    
            public void setFontName(String fontName) {
                String fn = fontName;
    
                if (fn.equals("MS Pゴシック")) {
                    fn = "msgothic.ttc";
                }
    
                if (fn.equals("MS ゴシック")) {
                    fn = "mspgothic.ttc";
                }
    
                String fontpath = SysUtils.getResourceFile("/config/font/" + fn).toString();
    
                try {
                    TrueTypeCollection trueTypeCollection = new TrueTypeCollection(new File(fontpath));
                    TrueTypeFont ttFont = trueTypeCollection.getFontByName("MS-PGothic");
                    setFontType(PDType0Font.load(document, ttFont, true));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
    
            }
    
            public String getFontName() {
                return fontName;
            }
    
            public int getFontSize() {
                return fontSize;
            }
    
            public void setFontSize(int fontSize) {
                this.fontSize = fontSize;
            }
    
            public PDFont getFontType() {
                return fontType;
            }
    
            public void setFontType(PDFont fontType) {
                this.fontType = fontType;
            }
        }
    
    }
    
    
    
    
  • 相关阅读:
    未让换行符弄错了数据
    REPLICATE
    内存 商业智能
    sql
    PageMethods介绍
    在ASP.NET AJAX中如何判断浏览器及计算其宽高
    用JavaScript实现网页图片等比例缩放
    js技巧收集(200多个)(转自:asp.net中文俱乐部)
    C#调用ORACLE存储过程返回结果集及函数
    Using PageMethods to access Session data
  • 原文地址:https://www.cnblogs.com/kutsu/p/14861508.html
Copyright © 2011-2022 走看看