OpenNTF.org - Export to PDF
My Links (Not logged in)
Code Bin Search
 
Hosted by Prominic.NET
Rate This Code
5 - brilliant stuff
4 - very nice
3 - average
2 - needs work
1 - bad
   OpenNTF Code Bin
About This Code
Brief Description:
Export to PDF  
Rating:
Not Rated Yet 
Contributor:
Mihir Ku Swain 
Category:
Java 
Type:
Notes Client UI 
Notes Version:
R7.x 
Last Modified:
04 Apr 2007 
OpenNTF Disclaimer

All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.

Code / Description
This codw will help to Export Data from Lotus to PDF format., But there is some class need to add in the agent , Because that class are not in the Lotus. Before use you need to add all the class file of Itext Jar class. ( Just Search (itext-2.0.1) free vession. )



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();
}
}

Usage / Example
Code Attachments
Lotus to PDF.txt (13 Kbytes)
itext-2.0.1.zip (1760 Kbytes)
 Comments
Posted by James M. Anderson III on 04/04/2007 11:32:01 AMNot Yours
This is a direct copy of a post from another developer on searchDomino.
Are you trying to take credit for someone else's work?
Posted by Mihir Ku Swain on 04/04/2007 12:25:16 PMHi , i know this CP of Code.
i have not mentioned this was my code. and also not i tried to take credit of someone else.
Posted by Michael Marcavage on 04/04/2007 08:30:18 PMNot Yours
I am the developer who created the original code that you posted.
By you posting it, you are declaring it as yours and you did not state that you copied it.
So as Jim asked, Are you trying to take credit for someone else's work?
Posted by Patrick Kwintensson on 04/05/2007 01:38:09 AMWTF?
If I read the code, I do not see in any way that the person who posted the code claims to be the original author of the code.
Therefor I do not understand the agressive attitude against him.
At least he dares to share and for that I give him my credit.
Posted by Michael Marcavage on 04/05/2007 07:41:32 AMYou are Right
You know you are right, it has been a rough week therefore I am sorry for my agression
Posted by Kevin Pettitt on 04/12/2007 11:29:53 AMGolden Rule of Posting (Somebody Else's) Code
Mihir, we will have to take you at your word that you did not mean to claim credit for writing this code, but Michael is completely justified in being upset. What you need to understand is that regardless of intent, there is an *implied* claim of authorship when you post code to the OpenNTF code bin (or most any place else for that matter). It should not be necessary to read through the code itself to discover otherwise. Merely omitting an explicit claim of authorship as seems to be the case here is not sufficient, since then the implied claim applies.
The appropriate action would have been to mention the source of the code in the opening sentence of the Code/Description section, providing a link back to the source. In other words, you need to *explicitly*, and prominently, point out the fact that you are not the author. Ideally, the code you post here should not be a complete rehash of the original, but rather some sort of adaptation of the original, perhaps as part of a sample db download or some such. For an example of this practice, take a look at a post I did recently that involved taking another blogger's raw Excel Export code and greatly enhancing and nicely packaging it for download: http://www.lotusguru.com/lotusguru/LGBlog.nsf/d6plinks/KPET-6ZGL9L
Putting yourself in Michael's shoes, think how you might react if some of your code was posted elsewhere without any credit given to you, such that the poster appears to the casual observer to be the author of the code. It reminds me how I felt when I found a former (junior) colleague's online resume claiming to have performed MY team lead role back when we were at the same company, when in fact this person was more or less kicked off the project for incompetence.
I would recommend at a minimum editing the description above to reflect Michael's authorship, or better yet packaging the code in a sample db so folks like me can start using it with minimal fuss - that would truly be a valuable contribution to the community.
Thanks for your participation, and please consider this simply a lesson learned and not let it discourage you from further contributing.
Kevin
www.lotusguru.com
Posted by Vince Schuurman on 05/03/2007 10:37:52 AMWell SearchDomino didn't exactly invent that code either...
..I posted this technique on my blog and one of the articles is referenced on the home page of iText
http://www.lowagie.com/iText/docs.html
Posted by Irv Schor on 03/11/2010 04:21:28 PMEmail attachments
Will this method also roll up any attachments into the .pdf output? If not, is there anything out there, preferably open source, that will do so?
Posted by Michael Marcavage on 03/11/2010 06:04:27 PMEmail attachments
No, this method will not, as is....
You would either have to detach the attachments then go after them one by one or
not use the iText jar file at all and use http://www.pd4ml.com/
 Add your comment!