Вы находитесь на странице: 1из 5

package com.pb.saas.payments.ariamoveoff.

invoice;

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

import lombok.extern.slf4j.Slf4j;

/**
* Utility for iText.
*/
@Component

@Slf4j
public class InvoiceHelper {

/** The Constant clazzName. */


private static final String clazzName = InvoiceHelper.class.getName();

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


double totalAmt = 0.00;
int row = 1;
File outputFile = new File("outputFile.pdf");
FileOutputStream fos = new FileOutputStream(outputFile);
// step 1
Document document = new Document(PageSize.A4);
// step 2
PdfWriter.getInstance(document, fos);
// step 3
document.open();
// step 4
Resource resource = new ClassPathResource("/images/pb_logo.jpg");

BaseFont bfBold = BaseFont.createFont(BaseFont.HELVETICA_BOLD,


BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,
BaseFont.NOT_EMBEDDED);

Image jpg =
Image.getInstance("C:\\Users\\nex7dkq\\Desktop\\pb_logo.jpg");
jpg.scaleAbsolute(175, 50);
jpg.setAlignment(Image.LEFT);
document.add(jpg);

Chunk companyName = new Chunk("Pitney Bowes Software Ltd.", new


Font(bfBold, 18));
Paragraph headerParagraph = new Paragraph();
headerParagraph.add(jpg);
headerParagraph.add(companyName);
headerParagraph.setAlignment(Element.ALIGN_RIGHT);
document.add(headerParagraph);

String date1 = String.format("20202002");


Paragraph date = new Paragraph("Date: "+date1.substring(6,
8)+"/"+date1.substring(4, 6)+"/"+date1.substring(0, 4));
Paragraph phrase = new Paragraph(20);
phrase.add("Selling");

phrase.setAlignment(Element.ALIGN_RIGHT);
date.setAlignment(Element.ALIGN_RIGHT);
document.add(date);
document.add(phrase);

Phrase phrase1 = new Phrase(30);


phrase1.add("Bank Name : "+ "Wells Fargo");
Paragraph title1 = new Paragraph();
title1.add(phrase1);
title1.setAlignment(Element.ALIGN_LEFT);
document.add(title1);

Phrase phrase2 = new Phrase(30);


phrase2.add("Branch Name : "+ "123123123");
Paragraph title2 = new Paragraph();
title2.add(phrase2);
title2.setAlignment(Element.ALIGN_LEFT);
document.add(title2);

Phrase phrase3 = new Phrase(50);


phrase3.add("Customer Name : "+ "jeff hardy");
Paragraph title3 = new Paragraph();
title3.add(phrase3);
title3.setAlignment(Element.ALIGN_LEFT);
document.add(title3);

Phrase phrase4 = new Phrase(50);


phrase4.add("NRC / PP No. : "+ "11111111111111");
Paragraph title4 = new Paragraph();
title4.add(phrase4);
title4.setAlignment(Element.ALIGN_LEFT);
document.add(title4);

Phrase phrase6 = new Phrase(50);


phrase6.add("Txn Ref No. : EX"+ "txn1111111111111111111111111");
Paragraph title6 = new Paragraph();
title6.add(phrase6);
title6.setAlignment(Element.ALIGN_LEFT);
document.add(title6);

Phrase phrase5 = new Phrase(50);


phrase5.setFont(new Font(Font.HELVETICA, 18, Font.BOLDITALIC, new
Color(0, 0, 255)));
phrase5.add("Address : "+ "Viman Nagar addr1");
phrase5.add("\n");
phrase5.add(" "+ "Addr 2");
phrase5.add("\n");
phrase5.add(" "+ "Addr 3");
Paragraph title5 = new Paragraph();
title5.add(phrase5);
title5.setAlignment(Element.ALIGN_LEFT);
title5.setFont(new Font(Font.HELVETICA, 100, Font.BOLDITALIC, new
Color(0, 0, 255)));
document.add(title5);

document.add( Chunk.NEWLINE );

float[] widths = { 5f, 50f, 20f,25f };


PdfPTable table1 = new PdfPTable(widths);

table1.addCell(new PdfPCell(new Phrase("Sl No.", new Font(bfBold))));


table1.addCell(new PdfPCell(new Phrase("Item Description", new
Font(bfBold))));
table1.addCell(new PdfPCell(new Phrase("Refill Date", new
Font(bfBold))));
table1.addCell(new PdfPCell(new Phrase("Refill Amount", new
Font(bfBold))));

document.add(table1);
int j = 0;
for (int i = 0; i < row; i++) {

float[] widths1 = { 5f, 50f, 20f,25f };


PdfPTable table = new PdfPTable(widths1);
PdfPCell cell = new PdfPCell();
cell.setColspan(7);
table.addCell(new Phrase(String.valueOf(j=j+1) + ".", new
Font(bfBold)));
table.addCell(new PdfPCell(new Phrase("(Fund_Postage (order #
184864149))", new Font(bf))));
table.addCell(new PdfPCell(new Phrase("02/20/2020", new
Font(bf))));
PdfPCell custamtcell = new PdfPCell(new Phrase("10.00", new
Font(bf)));
totalAmt = Double.valueOf(totalAmt) + Double.valueOf("10.00");
custamtcell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(custamtcell);
document.add(table);
}

for (int i = 0; i < row; i++) {

float[] widths1 = { 5f, 50f, 20f,25f };


PdfPTable table = new PdfPTable(widths1);
PdfPCell cell = new PdfPCell();
cell.setColspan(7);
table.addCell(new Phrase(String.valueOf(j=j+1) + ".", new
Font(bfBold)));
table.addCell(new PdfPCell(new Phrase("((CC Admin Fee)", new
Font(bf))));
table.addCell(new PdfPCell(new Phrase("02/20/2020", new
Font(bf))));
PdfPCell custamtcell = new PdfPCell(new Phrase("0.35", new
Font(bf)));
totalAmt = Double.valueOf(totalAmt) + Double.valueOf("0.35");
custamtcell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(custamtcell);
document.add(table);
}

float[] widths2 = { 75f,25f };


PdfPTable table2 = new PdfPTable(widths2);
PdfPCell totalAmountCell = new PdfPCell(new Phrase("Total", new
Font(bfBold)));
table2.addCell(totalAmountCell);
PdfPCell custtotamtcell = new PdfPCell(new
Phrase(String.valueOf(totalAmt), new Font(bfBold)));
custtotamtcell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table2.addCell(custtotamtcell);

document.add(table2);

document.add( Chunk.NEWLINE );
document.add( Chunk.NEWLINE );
document.add( Chunk.NEWLINE );
document.add( Chunk.NEWLINE );
document.add( Chunk.NEWLINE );
document.add( Chunk.NEWLINE );
document.add( Chunk.NEWLINE );
document.add( Chunk.NEWLINE );
document.add( Chunk.NEWLINE );
document.add( Chunk.NEWLINE );
document.add( Chunk.NEWLINE );

PdfPTable footer1 = new PdfPTable(3);


footer1.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
PdfPCell footercell = new PdfPCell();
footercell.setColspan(3);
footer1.addCell("");
footer1.addCell("");
footer1.addCell("");
document.add(footer1);

PdfPTable footer2 = new PdfPTable(3);


footer2.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
PdfPCell footercell2 = new PdfPCell();
footercell2.setColspan(3);
footer2.addCell("Cashier");
footer2.addCell("Manager");
footer2.addCell("Customer Signature");
document.add(footer2);

document.addTitle("Title TBD");
document.addAuthor("ABC Corp");
document.addSubject("Invoice for Jeff");
document.addCreator("ABC Corp");

// step 5
document.close();
}

Вам также может понравиться