no message
parent
d55de53ff8
commit
0238d8219d
@ -0,0 +1,16 @@
|
||||
package cokr.xit.fims.cmmn.pdf.print;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||
|
||||
import cokr.xit.foundation.AbstractComponent;
|
||||
|
||||
public abstract class PDF extends AbstractComponent {
|
||||
/**pdf 파일(*.pdf)의 mime type */
|
||||
public static final String MIME_TYPE = "application/pdf";
|
||||
|
||||
protected PDDocument doc;
|
||||
protected PDPageContentStream contentStream;
|
||||
protected PDRectangle paperSize;
|
||||
}
|
@ -1,5 +1,119 @@
|
||||
package cokr.xit.fims.cmmn.pdf.print;
|
||||
|
||||
public class PDFWriter {
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPage;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||
import org.apache.pdfbox.pdmodel.font.PDType0Font;
|
||||
import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
|
||||
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
||||
import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
public class PDFWriter extends PDF {
|
||||
|
||||
public PDFWriter(){
|
||||
this.doc = new PDDocument();
|
||||
}
|
||||
|
||||
public PDFWriter paper(String type){
|
||||
if(type.equals("A4")) {
|
||||
this.paperSize = PDRectangle.A4;
|
||||
} else if(type.equals("LETTER")) {
|
||||
this.paperSize = PDRectangle.LETTER;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public PDRectangle getPaperSize(){
|
||||
return this.paperSize;
|
||||
}
|
||||
|
||||
public PDType0Font font(String path) throws IOException{
|
||||
return PDType0Font.load(doc, new ClassPathResource(path).getInputStream());
|
||||
}
|
||||
|
||||
public PDFWriter beginPage() throws IOException {
|
||||
PDPage blankPage = new PDPage(paperSize);
|
||||
doc.addPage(blankPage);
|
||||
|
||||
// 작업 페이지 설정
|
||||
PDPage page = doc.getPage(doc.getNumberOfPages()-1);
|
||||
// 컨텐츠 스트림 열기
|
||||
contentStream = new PDPageContentStream(doc, page);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PDFWriter setLineDashPattern(float[] pattern, float phase) throws IOException{
|
||||
contentStream.setLineDashPattern(pattern, phase);
|
||||
return this;
|
||||
}
|
||||
public PDFWriter moveTo(float x, float y) throws IOException {
|
||||
contentStream.moveTo(x, y);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PDFWriter lineTo(float x, float y) throws IOException {
|
||||
contentStream.lineTo(x, y);
|
||||
return this;
|
||||
}
|
||||
public PDFWriter stroke() throws IOException {
|
||||
contentStream.stroke();
|
||||
return this;
|
||||
}
|
||||
public PDFWriter beginText() throws IOException {
|
||||
contentStream.beginText();
|
||||
return this;
|
||||
}
|
||||
public PDFWriter setFont(PDType0Font font, int fontSize) throws IOException {
|
||||
contentStream.setFont(font, fontSize);
|
||||
return this;
|
||||
}
|
||||
public PDFWriter setRenderingMode(RenderingMode fontStyle) throws IOException {
|
||||
contentStream.setRenderingMode(fontStyle);
|
||||
return this;
|
||||
}
|
||||
public PDFWriter setNonStrokingColor(PDColor fontColor) throws IOException {
|
||||
contentStream.setNonStrokingColor(fontColor);
|
||||
return this;
|
||||
}
|
||||
public PDFWriter newLineAtOffset(float tx, float ty) throws IOException {
|
||||
contentStream.newLineAtOffset(tx, ty);
|
||||
return this;
|
||||
}
|
||||
public PDFWriter showText(String text) throws IOException {
|
||||
contentStream.showText(text);
|
||||
return this;
|
||||
}
|
||||
public PDFWriter endText() throws IOException {
|
||||
contentStream.endText();
|
||||
return this;
|
||||
}
|
||||
|
||||
public PDFWriter image(InputStream imageStream, float x, float y, float width, float height) throws IOException {
|
||||
PDImageXObject image = PDImageXObject.createFromByteArray(doc, imageStream.readAllBytes(), "temp");
|
||||
contentStream.drawImage(image, x, y, width, height);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PDFWriter endPage() throws IOException {
|
||||
contentStream.close();
|
||||
contentStream = null;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PDFWriter saveDocument(String path) throws IOException{
|
||||
doc.save(path);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PDFWriter closeDocument() throws IOException{
|
||||
doc.close();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue