import lotus.domino.*; import java.io.*; import java.util.*; import java.awt.*; import java.math.*; import java.text.*; import java.lang.*; import java.awt.*; import java.awt.event.*; import com.lowagie.text.*; import com.lowagie.text.pdf.*; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Rectangle; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; import com.lowagie.text.Chunk; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.Phrase; import com.lowagie.text.Image; import com.lowagie.text.html.HtmlWriter; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.RandomAccessFileOrArray; import com.lowagie.text.pdf.codec.TiffImage; import com.lowagie.text.Element; import com.lowagie.text.PageSize; public class createPOPDFAgent extends AgentBase{ Frame frame; Label l; PdfPCell cell; Paragraph p1; String tiff_file; String imTiff_file; String pdf_file; String strInPutXMLData; String strOutPutImage; String PdfWriteDirBase; String PdfWriteDir; String path; Locale locale = Locale.US; public void NotesMain (){ String richTextField = null; lotus.domino.Document lnDoc = null; int imageId = 1; String docUNID = null; try{ Session session = getSession(); AgentContext agentContext = session.getAgentContext(); Database db = agentContext.getCurrentDatabase(); lnDoc = agentContext.getDocumentContext(); NoteCollection nc = db.createNoteCollection(false); nc.buildCollection(); if(lnDoc != null) { nc.add(lnDoc); } //Export to file docUNID = lnDoc.getUniversalID(); // Create a frame to show progress frame = new Frame(); l = new Label("Collecting Form Data."); frame.add(l, "Center"); frame.setSize(400, 100); frame.show(); l.setText("Collecting Form Data."); addButton(); //Get PDF Data //Get Form Information l.setText("Collecting Form Data.."); addButton(); String strForm = lnDoc.getItemValueString("Form"); String strGetCreatedDate = lnDoc.getItemValueString("CreatedDate"); String strGetJDFContact = lnDoc.getItemValueString("JDFContact"); String strGetTagline = lnDoc.getItemValueString("tagline"); String strGetMemo = lnDoc.getItemValueString("Memo"); //Get Distributor Information l.setText("Collecting Form Data..."); addButton(); String strGetDistName = lnDoc.getItemValueString("CompanyName"); String strGetDistContactName = lnDoc.getItemValueString("OppContactName"); String strGetDistContactEmail = lnDoc.getItemValueString("OppContactEmail"); String strGetDistAddy = lnDoc.getItemValueString("StreetAddress1"); String strGetDistCity = lnDoc.getItemValueString("StreetCity"); String strGetDistState = lnDoc.getItemValueString("StreetState"); String strGetDistZip = lnDoc.getItemValueString("StreetZip"); String strGetDistContactPhone = lnDoc.getItemValueString("OppContactPhone"); String strGetDistContactFax = lnDoc.getItemValueString("OppContactFax"); //Get MFG Information l.setText("Collecting Form Data...."); addButton(); String strGetMfgName = lnDoc.getItemValueString("Manufacturer"); String strGetMfgFax = lnDoc.getItemValueString("ManufacturerFaxNumber"); String strGetMfgPhone = lnDoc.getItemValueString("ManufacturerPhoneNumber"); String strGetMfgContactName = lnDoc.getItemValueString("ManufacturerContact"); String strGetMfgContactEmail = lnDoc.getItemValueString("ManufacturerContactEmail"); //Get Item Data String strGetB2 = lnDoc.getItemValueString("B2"); String strGetQ2 = lnDoc.getItemValueString("Q2"); String strGetD2 = lnDoc.getItemValueString("D2"); String strGetP1 = lnDoc.getItemValueString("P1"); double dGetN1 = lnDoc.getItemValueDouble("N1"); String strGetN1 = NumberFormat.getNumberInstance(locale).format(dGetN1); double dGetC1 = lnDoc.getItemValueDouble("C1"); String strGetC1 = NumberFormat.getNumberInstance(locale).format(dGetC1); double dGetST1 = lnDoc.getItemValueDouble("ST1"); String strGetST1 = NumberFormat.getNumberInstance(locale).format(dGetST1); l.setText("Starting PDF Creation."); addButton(); PdfWriteDirBase = "C:\\Temp\\"; PdfWriteDir = "\\PDF\\"; String strPDFDirectory = PdfWriteDirBase + PdfWriteDir; boolean exists = (new File(strPDFDirectory)).exists(); if (exists) { System.out.println("Directory: Does exsist"); } else { System.out.println("Directory: Does not exsist"); boolean success = (new File(strPDFDirectory)).mkdirs(); if (!success) { System.out.println("Directory: Not able to create directory " + strPDFDirectory); } } //PDF creation session.setEnvironmentVar("POPDFDirectory", "", true); path = PdfWriteDirBase + PdfWriteDir + "\\" + "Quote for - " + strGetDistName + ".pdf"; session.setEnvironmentVar("POPDFDirectory", path, true); File PdfAtt = new File(path); com.lowagie.text.Document pdfDoc = new com.lowagie.text.Document(PageSize.LETTER,36,36,36,36); PdfWriter writer = PdfWriter.getInstance(pdfDoc, new FileOutputStream(path)); //Set PDF Properties pdfDoc.addTitle("PDF Document Title"); pdfDoc.addSubject("PDF Document Subject"); pdfDoc.addKeywords("PDF Document Keyword"); pdfDoc.addCreator("PDF Document Creator"); pdfDoc.addAuthor("PDF Document Author"); //Start PDF Document pdfDoc.open(); //Get Standard Fonts // the 14 standard fonts in PDF Font[] fonts = new Font[4]; fonts[0] = FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL); fonts[1] = FontFactory.getFont(FontFactory.HELVETICA, 30, Font.BOLD); fonts[2] = FontFactory.getFont(FontFactory.HELVETICA, 20, Font.BOLD); fonts[3] = FontFactory.getFont(FontFactory.HELVETICA, 10, Font.BOLD); //IMAGE TESTING************************************************* // creation a jpeg passed as an array of bytes to the Image // l.setText("RandomAccessFile rf = new RandomAccessFile(JDF-Color-Logo.gif, r)"); // addButton(); // RandomAccessFile rf = new RandomAccessFile("JDF-Color-Logo.gif", "r"); // l.setText("int size = (int)rf.length();"); // addButton(); // int size = (int)rf.length(); // l.setText("byte imext[] = new byte[size];"); // addButton(); // byte imext[] = new byte[size]; // l.setText("rf.readFully(imext);"); // addButton(); // rf.readFully(imext); // l.setText("rf.close();"); // addButton(); // rf.close(); // l.setText("Image imgLogo = Image.getInstance(imext);"); // addButton(); // Image imgLogo = Image.getInstance(imext); // l.setText("pdfDoc.add(imgLogo);"); // addButton(); // pdfDoc.add(imgLogo); //IMAGE TESTING************************************************* //Import Logo //Image imgLogo = Image.getInstance("http://www.fakeurl.com/download/Color-Logo.gif"); My Edit Code---Mihir // pdfDoc.add(imgLogo); //JDF Logo and such float[] Lwidth = {.4f, 3f}; PdfPTable jtable = new PdfPTable(Lwidth); //PdfPCell LogoCell = new PdfPCell( new PdfPCell(imgLogo, false)); MyEditCode---Mihir //LogoCell.setBorder(Rectangle.NO_BORDER); My EditCode ---Mihir //LogoCell.setHorizontalAlignment(Element.ALIGN_LEFT); MyEditCode ---Mihir //jtable.addCell(LogoCell); MyEditCode --->Mihir PdfPCell AddyCell = new PdfPCell(new Paragraph("Address"+strGetTagline)); AddyCell.setBorder(Rectangle.NO_BORDER); AddyCell.setHorizontalAlignment(Element.ALIGN_LEFT); jtable.addCell(AddyCell); jtable.setHorizontalAlignment(Element.ALIGN_LEFT); pdfDoc.add(jtable); //PDF File - Title Need to Change as per the Requirement................(Coments Mihir) float[] width = {1.2f,2.5f, 1.2f,2.5f}; PdfPTable stable = new PdfPTable(width); PdfPCell TitleCell = new PdfPCell(new Paragraph("Quote", fonts[1])); TitleCell.setColspan(4); TitleCell.setHorizontalAlignment(Element.ALIGN_CENTER); TitleCell.setVerticalAlignment(Element.ALIGN_MIDDLE); stable.addCell(TitleCell); stable.addCell("Created Date:"); stable.addCell(strGetCreatedDate); stable.addCell("Contact Details:"); stable.addCell(strGetJDFContact); stable.setHorizontalAlignment(Element.ALIGN_RIGHT); stable.setWidthPercentage(100); stable.setSpacingBefore(15f); pdfDoc.add(stable); //PDF File - Dist and Mfg Information w/ Contacts float[] widths = {2f, 3.4f, .1f,2f, 3.4f}; PdfPTable table = new PdfPTable(widths); PdfPCell DistCell = new PdfPCell(new Paragraph("Detail Information of Emoloyees.", fonts[2])); DistCell.setColspan(2); DistCell.setMinimumHeight(30f); DistCell.setHorizontalAlignment(Element.ALIGN_CENTER); DistCell.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(DistCell); table.addCell(" "); PdfPCell MfgCell = new PdfPCell(new Paragraph("Detail Information of Employees.", fonts[2])); MfgCell.setMinimumHeight(30f); MfgCell.setHorizontalAlignment(Element.ALIGN_CENTER); MfgCell.setVerticalAlignment(Element.ALIGN_MIDDLE); MfgCell.setColspan(2); table.addCell(MfgCell); table.addCell("Emp_ID:"); table.addCell(strGetDistName); table.addCell(" "); table.addCell("Name:"); table.addCell(strGetMfgName); table.addCell("Age:"); table.addCell(strGetDistAddy); table.addCell(" "); table.addCell("Salary:"); table.addCell(strGetMfgContactName); table.addCell("Address"); table.addCell(strGetDistCity+", "+strGetDistState+" "+strGetDistZip); table.addCell(" "); table.addCell("Phone:"); table.addCell(strGetMfgPhone); table.addCell("Contact:"); table.addCell(strGetDistContactName); table.addCell(" "); table.addCell("Fax:"); table.addCell(strGetMfgFax); table.addCell("Phone:"); table.addCell(strGetDistContactPhone); table.addCell(" "); table.addCell("E-Mail:"); table.addCell(strGetMfgContactEmail); table.addCell("Fax:"); table.addCell(strGetDistContactFax); table.addCell(" "); table.addCell(" "); table.addCell(" "); table.addCell("E-Mail:"); table.addCell(strGetDistContactEmail); table.addCell(" "); table.addCell(" "); table.addCell(" "); table.setWidthPercentage(100); table.setSpacingBefore(15f); pdfDoc.add(table); //PDF File - Item Data float[] hwidth = {1f, 1f,1f,1f,1f,1f,1f}; PdfPTable htable = new PdfPTable(hwidth); //Build Titles //Manufacturer Name PdfPCell TitleCell1 = new PdfPCell(new Paragraph("Manufacturer", fonts[3])); TitleCell1.setVerticalAlignment(Element.ALIGN_MIDDLE); htable.addCell(TitleCell1); //Item# PdfPCell TitleCell2 = new PdfPCell(new Paragraph("Item #", fonts[3])); TitleCell2.setVerticalAlignment(Element.ALIGN_MIDDLE); htable.addCell(TitleCell2); //Description PdfPCell TitleCell3 = new PdfPCell(new Paragraph("Description", fonts[3])); TitleCell3.setVerticalAlignment(Element.ALIGN_MIDDLE); htable.addCell(TitleCell3); //Quantity PdfPCell TitleCell4 = new PdfPCell(new Paragraph("Quantity", fonts[3])); TitleCell4.setVerticalAlignment(Element.ALIGN_MIDDLE); htable.addCell(TitleCell4); //Case Pack PdfPCell TitleCell5 = new PdfPCell(new Paragraph("Case Pack", fonts[3])); TitleCell5.setVerticalAlignment(Element.ALIGN_MIDDLE); htable.addCell(TitleCell5); //Unit Cost PdfPCell TitleCell6 = new PdfPCell(new Paragraph("Unit Cost", fonts[3])); TitleCell6.setVerticalAlignment(Element.ALIGN_MIDDLE); htable.addCell(TitleCell6); //Amount PdfPCell TitleCell7 = new PdfPCell(new Paragraph("Amount", fonts[3])); TitleCell7.setVerticalAlignment(Element.ALIGN_MIDDLE); htable.addCell(TitleCell7); htable.setWidthPercentage(100); htable.setSpacingBefore(15f); pdfDoc.add(htable); //Actual Items float[] iwidth = {1f, 1f,1f,1f,1f,1f,1f}; PdfPTable itable = new PdfPTable(iwidth); itable.addCell(strGetB2); itable.addCell(strGetQ2); itable.addCell(strGetD2); itable.addCell(strGetN1); itable.addCell(strGetP1); itable.addCell(strGetC1); itable.addCell(strGetST1); itable.setWidthPercentage(100); pdfDoc.add(itable); //Close the PDF Document Cleanly pdfDoc.close(); l.setText("Completed PDF Creation...."); addButton(); String filepath = PdfAtt.getAbsolutePath(); lnDoc.replaceItemValue("PDFCreateFlag", "True"); lnDoc.replaceItemValue("PDFLocation", path); lnDoc.save(true,true); } catch(Exception e) { e.printStackTrace(); } } public String ReplaceSubString(String SourceS, String SearchS, String ReplaceS) { while(SourceS.indexOf( SearchS ) >= 0 ) { String leftString = SourceS.substring(0, SourceS.indexOf( SearchS ) ); String rightString = SourceS.substring( SearchS.length() + SourceS.indexOf( SearchS ) ); SourceS = leftString + ReplaceS + rightString; } return SourceS; } static String replace(String str, String pattern, String replace) { int s = 0; int e = 0; StringBuffer result = new StringBuffer(); while ((e = str.indexOf(pattern, s)) >= 0) { result.append(str.substring(s, e)); result.append(replace); s = e+pattern.length(); } result.append(str.substring(s)); return result.toString(); } public void addButton() { Button b = new Button("OK"); b.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { frame.dispose(); } } ); frame.add(b, "South"); frame.show(); } }