2017-09-17 8 views
0

私はJavaの小さなプロジェクトに取り組んでいます。そこで、データベースから内容を取り出してPDFファイルに書きたいと思います。iTextライブラリを使用して、指定されたフォーマットに従ってPDFを作成します。

グーグルで試してみたところ、iText Libraryがありました。私はそれが私の最初のJavaプロジェクトですJAVA.andするかなり新しいです:

誰もが囲まれた画像computer generated invoice

PSのように見えますPDFを作成するために導くことができます。

+1

この質問はスタックオーバーフローには広すぎます。まず[ドキュメント](https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-4-作成 - レポート - 使用 - pdfhtml)を読んでから(スクロールして、請求書の例が表示されます)。 *特定の技術的問題*がある場合、コーディングを開始し、スタックオーバーフローに戻ります。スタックオーバーフローは、 "私の仕事は私のための"プラットフォームでも、学習プラットフォームでもありません。 –

+1

もちろん、iTextで請求書を作成するための完全な本があります:https://developers.itextpdf.com/content/zugferd-future-invoicing –

+0

フィードバックのための@BrunoLowagieありがとう:)私はちょうどスターター参照を探しています.. –

答えて

2

ほとんどのユースケースをすばやく実装しました。

コードは次のとおりです。
最初に、請求書に1つのレコードとして機能する小さなクラスを定義します。

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 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 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 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(); 
} 
関連する問題