OWASP O2 Platform Blog

O2 Script – Creating PDFs with OWASP AppSec Brazil Certificates

This script shows how to use ITextSharp C# API to batch create PDFs (each containing a different text)

Document doc = null;
PdfWriter writer = null;

Action<string> openPdf =
 (targetFile) =>    {
 doc = new Document(PageSize.LETTER.Rotate());
 writer= PdfWriter.GetInstance(doc, new FileStream(targetFile, FileMode.Create));
 doc.Open();
 };

Action savePdf = ()=> doc.Close();

Action<string> addImage =
 (imagePath)=>{
 var certificateImage = iTextSharp.text.Image.GetInstance(imagePath);
 certificateImage.SetAbsolutePosition(-16 , 0);
 certificateImage.ScalePercent(29.2f);
 doc.Add(certificateImage);
 };

Action<string> addName =
 (name)=>{
 BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
 var cb = writer.DirectContent;
 cb.SetFontAndSize(bf, 34 );
 cb.BeginText();
 cb.SetTextMatrix(425  - (name.size() * 15) / 2, 275);
 cb.ShowText(name);
 cb.EndText();

 };
Func<string, List<string>> getNames =
 (dataFile)=>{
 return (from line in dataFile.fileContents().trim().split_onLines().remove(0)
 select line.split(",")[1].removeFirstChar().removeLastChar()).toList();
 };

Func<string, string,string> createPdf =
 (folder, name)=>{
 var certificateImagePath = folder.pathCombine("certificado.jpg");
 var certificatesFolder = folder.pathCombine("Certificates").createDir();
 var pdfFileName = "OWASP AppSec Brazil - {0}.pdf".format(name);
 var pdfFilePath = certificatesFolder.pathCombine(pdfFileName);
 openPdf(pdfFilePath);
 addImage(certificateImagePath);
 addName(name);
 savePdf();
 if(pdfFilePath.fileExists())
 "PDF created: {0}".info(pdfFilePath);
 else
 "PDF could not be created for name: {0}".error(name);
 return pdfFilePath;
 };

Action<string,string> createAndOpen =
 (folder, name)=>{
 var pdfCreated = createPdf(folder,name);
 pdfCreated.startProcess();
 };

//********************************************************************************

var sourceFolder = @"C:\_Work\OWASP\Brazil Conference Certificates\";
var cvsFile = sourceFolder.pathCombine("Inscrições View - 20101129 - certificados.csv");

var names = getNames(cvsFile);


foreach(var name in names)
 createPdf(sourceFolder,name);


/*createAndOpen(sourceFolder,"Lucas");
createAndOpen(sourceFolder,"Lucas Ferreira");
createAndOpen(sourceFolder,names[10]);*/

//*** Show results in GUI

var folderWithPdfs = sourceFolder.pathCombine("Certificates");
var topPanel = O2Gui.open<Panel>("Created PDFs for OWASP AppSec Brazil", 400,400 );
topPanel.add_TreeView()
 .add_Nodes(folderWithPdfs.files()
 .fileNames())
 .onDoubleClick<string>((pdfFile)=> folderWithPdfs.pathCombine(pdfFile).startProcess());

return "done";

//using iTextSharp.text
//using iTextSharp.text.pdf
//using System.IO
//O2Ref:itextsharp.dll

December 4, 2010 - Posted by | Interoperability

2 Comments »

  1. […] I just spent some time with Sarah Baso explaining how a particular script works (she is trying to automate the creation of pdfs with OWASP Summit certificates using O2 (similar with to O2 Script – Creating PDFs with OWASP AppSec Brazil Certificates ) […]

    Pingback by Decomposing an Lamba method used in an O2 Script « O2Platform.com for Developers | March 24, 2011 | Reply

  2. […] See this post for an example: O2 Script – Creating PDFs with OWASP AppSec Brazil Certificates   […]

    Pingback by What ‘Graph’ support exists in O2 today « OWASP O2 Platform Blog | July 30, 2011 | Reply


Leave a comment