2017-09-17 8 views
0

저는 Java에서 작은 프로젝트를 진행하고 있습니다. 데이터베이스에서 내용을 가져 와서 PDF 파일에 쓰고 싶습니다.iText 라이브러리를 사용하여 지정된 형식에 따라 PDF를 만듭니다.

나는 인터넷 검색을 시도하고 iText Library을 찾았습니다.

사람은 동봉 된 이미지처럼 computer generated invoice

PS 보이는 PDF을 만들 안내 수 : 나는 내 첫 자바 프로젝트의 JAVA.and 꽤 새로운 해요.

+1

이 질문은 스택 오버플로가 너무 광범위합니다. [문서] (https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-4-creating-reports-using-pdfhtml) (아래로 스크롤하면, 인보이스 예제가 표시됩니다.) * 특정 기술적 인 문제가있을 때 코딩을 시작하고 스택 오버플로로 돌아갑니다. 스택 오버플로는 "나를위한 일"플랫폼이 아니며 학습 플랫폼도 아닙니다. –

+1

물론 iText로 인보이스를 작성하는 데 도움이되는 완전한 책이 있습니다. https://developers.itextpdf.com/content/zugferd-future-invoicing –

+0

@BrunoLowagie가 의견을 보내 주셔서 감사합니다 :) 저는 시동기 참조를 찾고 있습니다. .. –

답변

2

대부분의 유스 케이스를 신속하게 구현했습니다.

코드는 다음과 같습니다.
먼저 인보이스에 단일 레코드로 사용되는 작은 클래스를 정의합니다.

static class Article{ 
    int SNO; 
    String description; 
    int quantity; 
    double unitPrice; 
    public Article(int SNO, String description, int quantity, double unitPrice) 
    { 
     this.SNO = SNO; 
     this.description = description; 
     this.quantity = quantity; 
     this.unitPrice = unitPrice; 
    } 
} 

그런 다음 인보이스의 큰 블록 각각에 대해 메소드를 생성했습니다. 제목으로 시작
:

public static void addTitle(Document layoutDocument) 
{ 
    layoutDocument.add(new Paragraph("RETAIL INVOICE").setBold().setUnderline().setTextAlignment(TextAlignment.CENTER)); 
} 

그런 다음 제목 아래 텍스트의 작은 단락 추가 :

public static void addCustomerReference(Document layoutDocument) 
{ 
    layoutDocument.add(new Paragraph("M/s Indian Convent School").setTextAlignment(TextAlignment.LEFT).setMultipliedLeading(0.2f)); 
    layoutDocument.add(new Paragraph("y Pocket-3, Sector-24, Rohini Delhi-110085").setMultipliedLeading(.2f)); 
    layoutDocument.add(new Paragraph("b 011-64660271").setMultipliedLeading(.2f)); 
} 

을 그리고 테이블 추가 : 다음

public void addTable(Document layoutDocument, List<Article> articleList) 
{ 
    Table table = new Table(UnitValue.createPointArray(new float[]{60f, 180f, 50f, 80f, 110f})); 

    // headers 
    table.addCell(new Paragraph("S.N.O.").setBold()); 
    table.addCell(new Paragraph("PARTICULARS").setBold()); 
    table.addCell(new Paragraph("QTY").setBold()); 
    table.addCell(new Paragraph("RATE").setBold()); 
    table.addCell(new Paragraph("AMOUNT IN RS.").setBold()); 

    // items 
    for(Article a : articleList) 
    { 
     table.addCell(new Paragraph(a.SNO+"")); 
     table.addCell(new Paragraph(a.description)); 
     table.addCell(new Paragraph(a.quantity+"")); 
     table.addCell(new Paragraph(a.unitPrice+"")); 
     table.addCell(new Paragraph((a.quantity * a.unitPrice)+"")); 
    } 

    layoutDocument.add(table); 
} 

주요 방법 다음과 같이 표시됩니다.

public static void main(String[] args) throws FileNotFoundException { 

    PdfDocument pdfDocument = new PdfDocument(new PdfWriter("MyFirstInvoice.pdf")); 
    Document layoutDocument = new Document(pdfDocument); 

    // title 
    addTitle(layoutDocument); 

    // customer reference information 
    addCustomerReference(layoutDocument); 
    addTable(layoutDocument, Arrays.asList(
      new Article(1, "Envelopes",2000, 1.70), 
      new Article(2, "Voucher Book", 50, 41))); 

    // articles 
    layoutDocument.close(); 
}