수색…


비고

평범한 텍스트 파일을 PDF 문서로 변환하는 코드는 iText 5 또는 iText 7을 사용하든간에 매우 간단합니다. iText 7에서는 문서의 레벨에서 정렬을 정의 할 수 있다는 이점이 있습니다. iText 5에서는 별도의 Paragraph 객체마다 정렬을 설정해야합니다.

이 두 쌍의 예제에서 iText 5와 iText 7의 실제 차이점을 이해하려면 결과 PDF를 살펴 봐야합니다. iText 5에서는 35 페이지의 텍스트로 끝납니다. iText 7에서는 동일한 텍스트가 38 페이지에 걸쳐 배포됩니다.

레이아웃을 생성 할 때 다른 기본값이 사용되기 때문에 텍스트는 iText 7로 작성 될 때 더 읽기 쉽습니다. iText 5 코드에서 동일한 결과를 얻을 수 있지만 간격과 관련하여 일부 값을 변경해야합니다.

iText 7에서는 iText에 대한 16 년간의 경험을 토대로 기본값이 선택되었습니다. 이렇게하면 적은 코드로 더 나은 결과를 얻을 수 있습니다.

더 알고 싶으십니까?

iText 7 : Building Blocks 튜토리얼의 5 장에 있는 RootElement로 작업하기를 읽으십시오. 무료 전자 책을 받으십시오!

Text2Pdf.java (iText 5)

다음 텍스트 파일이 있다고 가정합니다. jekyll_hyde.txt

다음과 같이 PDF로 변환하려면 어떻게해야합니까?

여기에 이미지 설명을 입력하십시오.

iText 5를 사용할 때 다음 코드를 사용합니다.

public void createPdf(String dest)
throws DocumentException, IOException {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    BufferedReader br = new BufferedReader(new FileReader(TEXT));
    String line;
    Paragraph p;
    Font normal = new Font(FontFamily.TIMES_ROMAN, 12);
    Font bold = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD);
    boolean title = true;
    while ((line = br.readLine()) != null) {
        p = new Paragraph(line, title ? bold : normal);
        p.setAlignment(Element.ALIGN_JUSTIFIED);
        title = line.isEmpty();
        document.add(p);
    }
    document.close();
}

출처 : developers.itextpdf.com

Text2Pdf.java (iText 7)

다음 텍스트 파일이 있다고 가정하십시오. jekyll_hyde.txt

다음과 같이 PDF로 변환하려면 어떻게해야합니까?

여기에 이미지 설명을 입력하십시오.

iText 7을 사용할 때 다음 코드가 필요합니다.

public void createPdf(String dest) throws IOException {
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    Document document = new Document(pdf)
        .setTextAlignment(TextAlignment.JUSTIFIED);
    BufferedReader br = new BufferedReader(new FileReader(TEXT));
    String line;
    PdfFont normal = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
    PdfFont bold = PdfFontFactory.createFont(FontConstants.TIMES_BOLD);
    boolean title = true;
    while ((line = br.readLine()) != null) {
        document.add(new Paragraph(line).setFont(title ? bold : normal));
        title = line.isEmpty();
    }
    document.close();
}

출처 : developers.itextpdf.comiText 7 : Building Blocks 튜토리얼.



Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow