Created
March 22, 2011 14:57
-
-
Save DinisCruz/881344 to your computer and use it in GitHub Desktop.
O2 Platform script to create Summit Certificates (First pass, needs to be designed)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sourceFolder = @"C:\O2\_tempDir\Summit Certificates\".createDir(); // DC NOTE: use this folder | |
var pathToBackGroundImage = sourceFolder.pathCombine("Summit Logo.gif"); | |
if (pathToBackGroundImage.fileExists().isFalse()) | |
"http://www.owasp.org/images/b/b7/Final_summit_logo.jpg".download(pathToBackGroundImage); | |
Document doc = null; | |
PdfWriter writer = null; | |
Action<string> openPdf = | |
//(targetFile) => { //DC note: the problem was here (note the extra ) | |
(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); // DC note: and here | |
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 certificatesFolder = folder.pathCombine("Certificates").createDir(); | |
var pdfFileName = "OWASP AppSec Brazil - {0}.pdf".format(name); | |
var pdfFilePath = certificatesFolder.pathCombine(pdfFileName); | |
openPdf(pdfFilePath); | |
if (pathToBackGroundImage.fileExists()) | |
addImage(pathToBackGroundImage); | |
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(); | |
}; | |
//******************************************************************************** | |
createAndOpen(sourceFolder,"Sarah Baso"); | |
/*createAndOpen(sourceFolder,"Lucas"); | |
createAndOpen(sourceFolder,"Lucas Ferreira"); | |
createAndOpen(sourceFolder,names[10]);*/ | |
//DC NOTE: you don't need this in the beggining now | |
/* | |
// create all | |
var cvsFile = sourceFolder.pathCombine("Inscrições View - 20101129 - certificados.csv"); | |
var names = getNames(cvsFile); | |
foreach(var name in names) | |
createPdf(sourceFolder,name); | |
//*** 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment