<iText>
(http://www.lowagie.com/iText/ : iText Home
http://www.lowagie.com/iText/tutorial/ch01.html : iText tutorial)
iText는 공개 프로젝트로 제가 찾은 것은 단순히 JAVA를 이용한 프로그래밍만 가능하지만,
.net 프로그래밍도 가능하답니다..
iText는 텍스트 파일을 xml 형태로 바꾸어서 pdf를 변환하는 것을 사용하고 있는 것 같습니다. 따라서 html 형식의 파일을 생성할 수 있습니다.
또한 별도의 글꼴을 사용할 수 있는데, 글꼴을 사용하기 위해서는 각 글꼴에 대한 Font를 받아 설치하여 사용을 해야합니다.
-For JAVA-
iText를 사용하기 위해서는 iText.jar 파일을 가지고 있어야 합니다.(jdk1.1이상 사용가능.)
기본적인 예제는 HelloWorld 입니다...^^;
====================================================================
simple 예제......1
--------------------------------------------------------------------
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
public class HelloPdf {
public static void main(String[] args) {
try {
Rectangle pageSize = new Rectangle(144, 720);
//page의 설정을 잡아준다.
pageSize.setBackgroundColor(new java.awt.Color(0xFF, 0xFF, 0xDE)); //page의 배경색을 잡아준다.
Document document = new Document(pageSize);
//iText를 사용하기 위해서는 document를 생성해서 사용을 해야함.
//또한 여기서의 Document는 com.lowagie.text.Document임.
System.out.println("hi");
PdfWriter.getInstance(document, new FileOutputStream("Chap0101.pdf")); //지정된 자료를 출력할 수 있는 곳을 지정함.
//앞은 Document, 뒤는 OutputStream임.
document.open();
//pdf의 내용을 작성 시작.
document.add(new Paragraph("Hello World!!!"));
//pdf에 내용을 저장.
document.close();
//document를 닫아준다.
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException : " + e);
} catch (DocumentException e) {
System.out.println("DocumentException : " + e);
}
}
}//end.......simple 예제 끝..
=============================================simple 예제 1 끝=========
다음으로는 font 설정입니다.
====================================================================simple 예제......2
--------------------------------------------------------------------import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
public class Chap0201 {
public static void main(String[] args) {
System.out.println("Chapter 2 example 1: Chunks and fonts");
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileOutputStream("Chap0201.pdf"));
// step 3: we open the document
document.open();
// step 4: we add content to the document
Font[] fonts = new Font[14];
fonts[0] = FontFactory.getFont(FontFactory.COURIER, 12, Font.NORMAL);
fonts[1] = FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD);
fonts[2] = FontFactory.getFont(FontFactory.COURIER, 12, Font.ITALIC);
fonts[3] = FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD | Font.ITALIC);
fonts[4] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL);
fonts[5] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD);
fonts[6] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC);
fonts[7] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD | Font.ITALIC);
fonts[8] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL);
fonts[9] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD);
fonts[10] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.ITALIC);
fonts[11] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD | Font.ITALIC);
fonts[12] = FontFactory.getFont(FontFactory.SYMBOL, 12, Font.NORMAL);
fonts[13] = FontFactory.getFont(FontFactory.ZAPFDINGBATS, 12, Font.NORMAL);
for (int i = 0; i < 14; i++) {
Chunk chunk = new Chunk("This is some", fonts[i]);
document.add(new Phrase(chunk));
//font를 설정하고 해당폰트를 Phrase를 이용하여 작성한다.
document.add(new Phrase(new Chunk(" font. ",
fonts[i]).setTextRise((i % 2 == 0) ? -6 : 6)));
}
document.add(new Phrase(new Chunk("This text is underlined",
FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE))));
document.add(new Phrase(new Chunk("This font is of type ITALIC | STRIKETHRU",
FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC | Font.STRIKETHRU))));
Chunk ck = new Chunk("This text has a yellow background color", FontFactory.getFont(FontFactory.HELVETICA, 12));
ck.setBackground(new Color(0xFF, 0xFF, 0x00));
document.add(new Phrase(ck));
}
catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
}
}
====================================================================Font는 기본적으로 몇가지를 제공하고 있으나, 다른 Font를 사용하려면(한글 코드) 별도의 폰트를 받아서 사용해야 합니다.
다음은 문단(Paragraph) 를 나누는 방법에 대해서 표현하는 법입니다.
== simple 예제 ...3 ================================================================
Paragraph p1 = new Paragraph(new Chunk("This is my first paragraph.", FontFactory.getFont(FontFactory.HELVETICA, 12)));
p1.add("you can add strings, ");
p1.add(new Chunk("you can add chunks "));
p1.add(new Phrase("or you can add phrases."));
Paragraph p2 = new Paragraph(new Phrase("This is my second paragraph.", FontFactory.getFont(FontFactory.HELVETICA, 12)));
Paragraph p3 = new Paragraph("This is my third paragraph.", FontFactory.getFont(FontFactory.HELVETICA, 12));
document.add(p1);
document.add(p2);
document.add(p3);
====================================================================
다음은 List를 출력하는 것으로
List(numbered,symbollndent)
numbered - a boolean
symbolIndent - the indentation that has to be used for the listsymbol
형식으로 사용할 수 있으며, 아래는 사용 예 입니다.
=== simple 예제 ...4 ===============================================================
List list = new List(true, 20);
list.add(new ListItem("First line"));
list.add(new ListItem("The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?"));
list.add(new ListItem("Third line"));
위의 소스는 아래와 같이 출력됩니다.
1. First line
2. The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?
3. Third line
====================================================================
XML 파일을 PDF나 HTML 로 전환하는 방법.
====================================================================
simple 예제 ... 5
--------------------------------------------------------------------
import java.io.FileOutputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.html.HtmlWriter;
import com.lowagie.text.xml.*;
public class Chap0702 {
public static void main(String[] args) {
System.out.println("Chapter 7 example 2: parsing the result of example 1");
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
PdfWriter.getInstance(document, new FileOutputStream("Chap0702.pdf")); // 출력할 파일을 선택한다.(PDF로..)
HtmlWriter.getInstance(document, new FileOutputStream("Chap0702.html")); // 출력할 파일을 선택한다.(HTML로..)
// step 3: we create a parser and set the document handler
SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); //SAXParser를 초기화 한다.
// step 4: we parse the document
parser.parse("Chap0701.xml", new SAXiTextHandler(document));
//불러올 XML을 선택한다.
}
catch(Exception e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
}
}
====================================================================
CSS 를 HTML 에 적용을 하고 그것을 출력하는 방법
====================================================================
simple 예제 ... 6
--------------------------------------------------------------------
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
HtmlWriter.getInstance(document, new FileOutputStream("Chap0708.html"));
document.add(new Header(MarkupTags.STYLESHEET, "myStyles.css"));
//출력 파일을 지정하고
//해당 Document 에 필요한 CSS를 Header를 이용 설정을 잡아준다.
document.open();
...........
......
...
====================================================================
(X)HTML 을 Parsing 하기.
====================================================================
simple 예제 ... 7
--------------------------------------------------------------------
import java.io.FileOutputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.html.SAXmyHtmlHandler;
public class Chap0706 {
public static void main(String[] args) {
System.out.println("Chapter 7 example 6: parsing the HTML from example 2");
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 80, 50, 30, 65);
try {
// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
PdfWriter.getInstance(document, new FileOutputStream("Chap0706.pdf"));
// step 3: we create a parser and set the document handler
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
// step 4: we parse the document
parser.parse("Chap0702.html", new SAXmyHtmlHandler(document));
}
catch(Exception e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
}
}
====================================================================
====================================================================
simple 예제 ... 8
--------------------------------------------------------------------
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.html.HtmlParser;
public class Chap0707 {
public static void main(String[] args) {
System.out.println("Chapter 7 example 7: parsing the HTML from example 2");
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 80, 50, 30, 65);
try {
// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
PdfWriter.getInstance(document, new FileOutputStream("Chap0707.pdf"));
// step 3: we parse the document
HtmlParser.parse(document, "Chap0702.html");
}
catch(Exception e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
}
}
====================================================================
여기서 중요한 점은 아직 완성된 버전이 아니라서 많은 테그를 가지고 있는 HTML문서는 파싱이 어렵고 에러가 날 수도 있다고 합니다.
결론.
지금까지 간단하게 iText 를 사용하는 방법을 알아봤습니다. 단순히 프로그램상에서 PDF 파일을 만들어내는 것이 전부가 아니라, HTML을 만들어 낼 수 있고 XML 이나 HTML을 Parsing 할 때 SAX 이나 DOM을 이용하여 번거로운 파싱 작업이 필요없다는 것은 강점으로 보입니다.
보다 자세한 사항을 알기 위해서는
(http://www.lowagie.com/iText/ : iText Home
http://www.lowagie.com/iText/tutorial/ch01.html : iText tutorial)
'JAVA야' 카테고리의 다른 글
JDK 7.0 API DOC (0) | 2012.10.04 |
---|---|
[펌]JSP-쿠키와 세션- 쿠키처리를 위한 유틸리티 클래스 (0) | 2010.02.18 |
[복사되는블로그 꼬마갱이] java에서 procedure호출 (0) | 2010.01.13 |
p6spy - 쿼리 로그 보기 (0) | 2009.12.24 |
[펌]replaceAll (0) | 2009.12.23 |