itextpdf 测试用例
Posted KutsuHaku
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了itextpdf 测试用例相关的知识,希望对你有一定的参考价值。
public class test1 {
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 test1() {
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 void abort() {
}
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;
}
}
}
以上是关于itextpdf 测试用例的主要内容,如果未能解决你的问题,请参考以下文章
JMeter:逻辑控制器_模块控制器(Module Controller)