using System; using System.Collections; using System.Collections.Generic; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html; using iTextSharp.text.html.simpleparser; using iTextSharp.text.xml.xmp; using log4net; using Sleis.ViewModels; using Spring.Objects; using Sleis.Data; using System.Net; namespace Sleis.Pdf { public class PdfMaker { internal static ILog LOG = LogManager.GetLogger(typeof(PdfMaker)); private static readonly iTextSharp.text.Font COVER_PAGE_LABEL_FONT = new iTextSharp.text.Font( iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.BOLD); private static readonly iTextSharp.text.Font COVER_PAGE_VALUE_FONT = new iTextSharp.text.Font( iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL); private static readonly iTextSharp.text.Font COVER_PAGE_HD_FONT = new iTextSharp.text.Font( iTextSharp.text.Font.FontFamily.HELVETICA, 14, iTextSharp.text.Font.BOLD); public DirectoryInfo Output { get; set; } public IList Elements { get; set; } public Org.BouncyCastle.X509.X509Certificate[] Chain; public PdfSignatureMaker SignatureMaker { get; set; } public PdfSignatureArgs SignatureArgs { get; set; } public bool DeleteTempFiles { get; set; } public void Init() { if (Output == null) { throw new ArgumentNullException("Output"); } if (!Output.Exists) { throw new DirectoryNotFoundException(Output.FullName); } if (Elements == null || Elements.Count < 1) { throw new ArgumentNullException("Elements"); } } public byte[] Make(string html) { LOG.Debug("Init Make"); string fileOutputPath = GetTempFilePath(); LOG.Debug("Result: " + fileOutputPath); Document pdf = new Document(PageSize.LETTER_LANDSCAPE, 5, 5, 5, 5); PdfWriter.GetInstance(pdf, File.OpenWrite(fileOutputPath)); LOG.Debug("Openning..."); pdf.Open(); List htmlarraylist = HTMLWorker.ParseToList(new StringReader(html), null); for (int k = 0; k < htmlarraylist.Count; k++) { pdf.Add((IElement)htmlarraylist[k]); } LOG.Debug("Closing..."); pdf.Close(); byte[] content = File.ReadAllBytes(fileOutputPath); File.Delete(fileOutputPath); return content; } public byte[] Make(ObjectWrapper data) { LOG.Debug("Init Make"); string fileOutputPath = GetTempFilePath(); LOG.Debug("Result: " + fileOutputPath); Rectangle size = PageSize.LETTER.Rotate(); Document pdf = new Document(size, 20, 20, 20, 20); pdf.SetPageSize(size); pdf.AddTitle(SignatureArgs.Title); pdf.AddAuthor(SignatureArgs.Author); pdf.AddCreator(SignatureArgs.Creator); PdfWriter pdfWriter = PdfWriter.GetInstance(pdf, File.OpenWrite(fileOutputPath)); pdfWriter.PageEvent = new PageEventHelper(); LOG.Debug("Openning..."); pdf.Open(); foreach (ElementWriter writer in Elements) { writer.Write(pdf, data); //pdf.Add(writer.GetElement(null, data)); } LOG.Debug("Closing..."); pdf.Close(); byte[] content = File.ReadAllBytes(fileOutputPath); File.Delete(fileOutputPath); return content; } public byte[] Sign(byte[] content, Dictionary runtimeArgs) { LOG.Debug("Init Sign"); //write out the Db document string srcPath = GetTempFilePath(); File.WriteAllBytes(srcPath, content); //make a cover string signaturePage = MakeSignaturePage(runtimeArgs); //make a combined string combinedPath = StitchPdfs(signaturePage, srcPath); //signed the file string signedFile = SignatureMaker.Sign(combinedPath, SignatureArgs); byte[] signedContent = File.ReadAllBytes(signedFile); //Cleanup if (DeleteTempFiles) { File.Delete(srcPath); File.Delete(signaturePage); File.Delete(combinedPath); File.Delete(signedFile); } return signedContent; } private string GetTempFilePath() { return Path.Combine(Output.FullName, String.Format("{0}.pdf", Guid.NewGuid())); } private string MakeSignaturePage(Dictionary runtimeArgs) { LOG.Debug("Init Cover"); string fileOutputPath = GetTempFilePath(); LOG.Debug("Result: " + fileOutputPath); Document pdf = new Document(); pdf.AddTitle(SignatureArgs.Title); pdf.AddAuthor(SignatureArgs.Author); pdf.AddCreator(SignatureArgs.Creator); PdfWriter.GetInstance(pdf, File.OpenWrite(fileOutputPath)); LOG.Debug("Openning..."); pdf.Open(); //Header pdf.Add(new Paragraph("Signature Page", new iTextSharp.text.Font( iTextSharp.text.Font.FontFamily.HELVETICA, 24, iTextSharp.text.Font.BOLD))); foreach (KeyValuePair serverArg in runtimeArgs) { WriteLine(pdf, serverArg); } LOG.Debug("Closing..."); pdf.Close(); return fileOutputPath; } private void WriteLine(Document pdf, KeyValuePair args) { pdf.Add(new Paragraph(args.Key, COVER_PAGE_LABEL_FONT)); pdf.Add(new Paragraph(args.Value, COVER_PAGE_VALUE_FONT)); } private void WriteHeadLine(Document pdf, string text) { pdf.Add(new Paragraph(text, COVER_PAGE_HD_FONT)); } private string StitchPdfs(params string[] fileNames) { string outFile = GetTempFilePath(); int pageOffset = 0; List> master = new List>(); int f = 0; Document document = null; PdfCopy writer = null; while (f < fileNames.Length) { // we create a reader for a certain document PdfReader reader = new PdfReader(fileNames[f]); reader.ConsolidateNamedDestinations(); // we retrieve the total number of pages int n = reader.NumberOfPages; IList> bookmarks = SimpleBookmark.GetBookmark(reader); if (bookmarks != null) { if (pageOffset != 0) { SimpleBookmark.ShiftPageNumbers(bookmarks, pageOffset, null); } master.AddRange(bookmarks); } pageOffset += n; if (f == 0) { // creation of a document-object document = new Document(reader.GetPageSizeWithRotation(1)); // create a writer that listens to the document writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create)); // open the document document.Open(); } // add content for (int i = 0; i < n; ) { ++i; if (writer != null) { PdfImportedPage page = writer.GetImportedPage(reader, i); writer.AddPage(page); } } PRAcroForm form = reader.AcroForm; if (form != null && writer != null) { writer.CopyAcroForm(reader); } f++; } if (master.Count > 0 && writer != null) { writer.Outlines = master; } // close the document if (document != null) { document.Close(); } return outFile; } } }