Its a sample PDF code that helps you generate a PDF document using PdfContentByte. Most of the code changes are self explainatory.
import java.awt.image.BufferedImage; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Image; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.Chunk; import com.itextpdf.text.FontFactory; import com.itextpdf.text.pdf.ColumnText; public class PharmacyReportGenerator { private static final String FILE_EXTENSION = "demographics.pdf"; static Font font = new Font(Font.FontFamily.HELVETICA, 7, Font.NORMAL); static Font smallfont = new Font(Font.FontFamily.HELVETICA, 6, Font.NORMAL); static Font boldFont = new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD); static Font titleFont = new Font(Font.FontFamily.HELVETICA, 16f, Font.BOLD, BaseColor.GRAY); static final String ADDRESS_STRING = "Big Pharmacy#85748 - \n\n BIG PHARMACY STORE # 12980 \n\n 200 STREET FAMOUS \n\n SACRAMENTO, CA 17319-0000 \n\n (656)993-2782"; static final String TITLE_TEXT = "Customer Verification Form"; static final String SUB_TITLE_TEXT = "Please review the information listed below and update the fields if necessary and return to pharmacy."; static final String MULTIPLE_INSURANCES = "PLEASE CHECK THE INSURANCES"; public void generateDemographicsReport() { try { Document document = new Document(PageSize.A4.rotate()); PharmacyReportGenerator report = new PharmacyReportGenerator(); String fileName = AppParam.BOATEMPDIR()+customerNumber+FILE_EXTENSION; PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName)); document.open(); PdfContentByte cb = writer.getDirectContent(); document.add(report.addTitleTable()); addHorizontalSeperatorLine(cb, 485f); report.addPersonalInformationSection(cb); String fullName = DataDefunkifier.formatPersonName(demographicsData.getLName(), demographicsData.getFName(), null); report.printTextValueFromSystem(cb, 154, 454, fullName); report.printTextValueFromSystem(cb, 154, 440, demographicsData.getAddress1()); report.printTextValueFromSystem(cb, 155, 424, demographicsData.getAddress2()); report.printTextValueFromSystem(cb, 155, 410, demographicsData.getState()); report.printTextValueFromSystem(cb, 155, 394, demographicsData.getZipCode()); report.printTextValueFromSystem(cb, 155, 380, demographicsData.getPhnNbr()); report.printTextValueFromSystem(cb, 155, 364, demographicsData.getEmailId()); report.printTextValueFromSystem(cb, 155, 350, demographicsData.getDOB()); report.printTextValueFromSystem(cb, 155, 334, demographicsData.getSexCde()); report.printTextValueFromSystem(cb, 155, 320, demographicsData.getPcpName()); addHorizontalSeperatorLine(cb, 312f); report.addAllergiesSection(cb, demographicsData.getAllergies()); addHorizontalSeperatorLine(cb, 274f); report.addConditionsSection(cb, demographicsData.getConditions()); addHorizontalSeperatorLine(cb, 238f); report.addInsuranceSection(cb); // report.addInsuranceValues(cb, customerNumber); addHorizontalSeperatorLine(cb, 162f); report.addPreferencesSection(cb, demographicsData.getPreferences()); addHorizontalSeperatorLine(cb, 123f); report.addAdditionalInformationSection(cb); addHorizontalSeperatorLine(cb, 80f); // addContent(document); document.close(); } catch (Exception e) { AppParam.getLog().error("Exception while creating patient demographics report "+e.getMessage()); } } private PdfPTable addTitleTable() throws DocumentException { PdfPTable titleTable = new PdfPTable(3); float[] columnWidths = {0.2f, 0.5f, 1f}; titleTable.setWidthPercentage(100); titleTable.setWidths(columnWidths); PdfPCell cell1 = new PdfPCell(); disableCellBorders(cell1); titleTable.addCell(cell1); PdfPCell cell2 = getAddressAsString(); disableCellBorders(cell2); titleTable.addCell(cell2); PdfPCell cell3 = getTitleText(); cell3.setPaddingTop(20f); cell3.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); disableCellBorders(cell3); titleTable.addCell(cell3); return titleTable; } private PdfPCell getAddressAsString(){ return (new PdfPCell(new Paragraph(ADDRESS_STRING, font))); } private PdfPCell getTitleText(){ PdfPCell cell = new PdfPCell(); PdfPTable subTable = new PdfPTable(1); subTable.setWidthPercentage(100); PdfPCell subCell1 = new PdfPCell(new Paragraph(TITLE_TEXT, titleFont)); subCell1.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); subCell1.setPaddingBottom(5f); disableCellBorders(subCell1); PdfPCell subCell2 = new PdfPCell(new Paragraph(SUB_TITLE_TEXT, font)); subCell2.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); subCell2.setPaddingTop(5f); disableCellBorders(subCell2); subTable.addCell(subCell1); subTable.addCell(subCell2); cell.addElement(subTable); return cell; } private void addPersonalInformationSection(PdfContentByte cb) throws DocumentException, IOException { BaseFont bfBold = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); BaseFont bfNormal = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.moveText(40f, 470f); cb.setFontAndSize(bfBold, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Personal Information:")); cb.endText(); cb.beginText(); cb.moveText(40f, 455f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Name:")); cb.endText(); cb.beginText(); cb.moveText(40f, 440f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Address:")); cb.endText(); cb.beginText(); cb.moveText(40f, 425f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Address(continued):")); cb.endText(); cb.beginText(); cb.moveText(40f, 410f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("State:")); cb.endText(); cb.beginText(); cb.moveText(40f, 395f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Zip:")); cb.endText(); cb.beginText(); cb.moveText(40f, 380f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Phone Number:")); cb.endText(); cb.beginText(); cb.moveText(40f, 365f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Email Address:")); cb.endText(); cb.beginText(); cb.moveText(40f, 350f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Date of Birth:")); cb.endText(); cb.beginText(); cb.moveText(40f, 335f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Gender:")); cb.endText(); cb.beginText(); cb.moveText(40f, 320f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText("PCP Name:"); cb.endText(); cb.beginText(); cb.moveText(400f, 470f); cb.setFontAndSize(bfBold, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Updated Personal Information:")); cb.endText(); cb.beginText(); cb.moveText(400f, 455f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Name:")); cb.endText(); addLineForManualEntry(cb, 520f, 453f); cb.beginText(); cb.moveText(400f, 440f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Address:")); cb.endText(); addLineForManualEntry(cb, 520f, 438f); cb.beginText(); cb.moveText(400f, 425f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Address(continued):")); cb.endText(); addLineForManualEntry(cb, 520f, 423f); cb.beginText(); cb.moveText(400f, 410f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("State:")); cb.endText(); addLineForManualEntry(cb, 520f, 408f); cb.beginText(); cb.moveText(400f, 395f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Zip:")); cb.endText(); addLineForManualEntry(cb, 520f, 393f); cb.beginText(); cb.moveText(400f, 380f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Phone Number:")); cb.endText(); addLineForManualEntry(cb, 520f, 378f); cb.beginText(); cb.moveText(400f, 365f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Email Address:")); cb.endText(); addLineForManualEntry(cb, 520f, 363f); cb.beginText(); cb.moveText(400f, 350f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Date of Birth:")); cb.endText(); addLineForManualEntry(cb, 520f, 348f); cb.beginText(); cb.moveText(400f, 335f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Gender:")); cb.endText(); addLineForManualEntry(cb, 520f, 332f); cb.beginText(); cb.moveText(400f, 320f); cb.setFontAndSize(bfNormal, 10f); cb.setCharacterSpacing(1.2f); cb.showText("PCP Name:"); cb.endText(); addLineForManualEntry(cb, 520f, 318f); } private void addAllergiesSection(PdfContentByte cb, String allergies) throws DocumentException, IOException { BaseFont bfBold = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.moveText(40f, 296f); cb.setFontAndSize(bfBold, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Allergies: ")); cb.endText(); ColumnText ctMsg = new ColumnText(cb); Font textFontMsg = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.NORMAL); float leadingMSG = 12; ctMsg.addText(new Chunk(allergies, textFontMsg)); ctMsg.setSimpleColumn(150, 315, 330, 270, leadingMSG, Element.ALIGN_LEFT); ctMsg.go(); cb.beginText(); cb.moveText(400f, 296f); cb.setFontAndSize(bfBold, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Updated Allergies: ")); cb.endText(); addLineForManualEntry(cb, 520f, 296f); addLineForManualEntry(cb, 520f, 282f); } private void addConditionsSection(PdfContentByte cb, String conditions) throws DocumentException, IOException { BaseFont bfBold = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.moveText(40f, 260f); cb.setFontAndSize(bfBold, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Conditions: ")); cb.endText(); ColumnText ctMsg = new ColumnText(cb); Font textFontMsg = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.NORMAL); float leadingMSG = 12; ctMsg.addText(new Chunk(conditions, textFontMsg)); ctMsg.setSimpleColumn(145, 278, 330, 233, leadingMSG, Element.ALIGN_LEFT); ctMsg.go(); cb.beginText(); cb.moveText(400f, 260f); cb.setFontAndSize(bfBold, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Updated Conditions: ")); cb.endText(); addLineForManualEntry(cb, 525f, 260f); addLineForManualEntry(cb, 525f, 246f); } private void addInsuranceSection(PdfContentByte cb) throws DocumentException, IOException { BaseFont bfBold = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.moveText(40f, 224f); cb.setFontAndSize(bfBold, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Insurance: ")); cb.endText(); cb.beginText(); cb.moveText(400f, 224f); cb.setFontAndSize(bfBold, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Updated Insurance: ")); cb.endText(); addLineForManualEntry(cb, 400f, 200f); addLineForManualEntry(cb, 400f, 186f); addLineForManualEntry(cb, 400f, 172f); } private void addPreferencesSection(PdfContentByte cb, String preferences) throws DocumentException, IOException { BaseFont bfBold = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.moveText(40f, 148f); cb.setFontAndSize(bfBold, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Preferences: ")); cb.endText(); ColumnText ctMsg = new ColumnText(cb); Font textFontMsg = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.NORMAL); float leadingMSG = 12; ctMsg.addText(new Chunk(preferences, textFontMsg)); ctMsg.setSimpleColumn(145, 165, 330, 126, leadingMSG, Element.ALIGN_LEFT); ctMsg.go(); cb.beginText(); cb.moveText(400f, 148f); cb.setFontAndSize(bfBold, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Updated Preferences: ")); cb.endText(); addLineForManualEntry(cb, 530f, 148f); addLineForManualEntry(cb, 530f, 134f); } private void addAdditionalInformationSection(PdfContentByte cb) throws DocumentException, IOException { BaseFont bfBold = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.moveText(40f, 110f); cb.setFontAndSize(bfBold, 10f); cb.setCharacterSpacing(1.2f); cb.showText(DataDefunkifier.toTitleCase("Additional Information: ")); cb.endText(); addLineForManualEntry(cb, 175f, 104f); addLineForManualEntry(cb, 175f, 90f); } private static void addHorizontalSeperatorLine(PdfContentByte cb, float y){ cb.setLineWidth(1.0f); cb.moveTo(40f, y); cb.lineTo(815, y); cb.stroke(); } public void disableCellBorders(PdfPCell cell){ cell.disableBorderSide(PdfPCell.TOP); cell.disableBorderSide(PdfPCell.BOTTOM); cell.disableBorderSide(PdfPCell.LEFT); cell.disableBorderSide(PdfPCell.RIGHT); } private void addLineForManualEntry(PdfContentByte cb, float x, float y){ cb.beginText(); cb.setLineWidth(1f); cb.moveTo(x, y); cb.lineTo(815f, y); cb.stroke(); cb.endText(); } private void printTextValueFromSystem(PdfContentByte cb, float x, float y, String text) throws IOException, DocumentException { BaseFont bfNormal = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.moveText(x, y); cb.setFontAndSize(bfNormal, 8f); cb.showText(text); cb.endText(); } private void addInsuranceValues(PdfContentByte cb, String customerNumber) throws DataModuleException, SystemException { CustomerInsurance custIns = new CustomerEnrollmentResolver().getActiveCustomerInsurance(Integer.parseInt(customerNumber)); if (null != custIns) { ArrayList insuranceCards = custIns.getInsuranceCardList(); int insSize = insuranceCards.size(); } } }