Пример #1
0
        /// <summary>
        /// Merge CV and covering letter and return pdf location
        /// </summary>
        /// <param name="filesToMerge"></param>
        /// <param name="outputFilename"></param>
        /// <param name="result"></param>
        public static void MergePdf(IEnumerable<BulkPrintDocEntry> filesToMerge, string outputFilename, ref BulkPrintCvResult result)
        {
            result.ErrorCvs = new List<BulkPrintErrorCvs>();
            var bulkPrintDocEntries = filesToMerge as BulkPrintDocEntry[] ?? filesToMerge.ToArray();
            if (!bulkPrintDocEntries.Any()) return;
            var document = new iTextSharp.text.Document();
            // initialize pdf writer
            var writer = PdfWriter.GetInstance(document, new FileStream(outputFilename, FileMode.Create));
            // open document to write
            document.Open();
            var content = writer.DirectContent;
            foreach (var docEntry in bulkPrintDocEntries)
            {
                var sCoveringLetterHtml = docEntry.CoveringLetterHtml;
                // create page
                document.NewPage();
                // add styles
                var styles = new StyleSheet();
                styles.LoadStyle("left", "width", "22%");
                styles.LoadTagStyle("td", "valign", "top");
                styles.LoadStyle("bordered", "border", "1");
                var hw = new HTMLWorker(document, null, styles);
                hw.Parse(new StringReader(sCoveringLetterHtml));
                var sFileName = docEntry.CvFileName.ToLower().Replace(".docx", ".pdf").Replace(".DOCX", ".pdf").Replace(".doc", ".pdf").Replace(".DOC", ".pdf").Replace(".rtf", ".pdf").Replace(".RTF", ".pdf");
                if (!File.Exists(sFileName))
                {
                    //pdf file not exists
                    result.ErrorCvs.Add(new BulkPrintErrorCvs
                    {
                        Error = "Pdf file does not exists. " + sFileName,
                        ContactId = docEntry.ContactId,
                        ContactName = new Contacts().GetCandidateName(docEntry.ContactId),
                        Document = docEntry.Doc
                    });
                    continue;
                }

                // Create pdf reader
                var reader = new PdfReader(sFileName);
                if (!reader.IsOpenedWithFullPermissions)
                {
                    //pdf file does not have permission
                    result.ErrorCvs.Add(new BulkPrintErrorCvs
                    {
                        Error = "Do not have enough permissions to read the file",
                        ContactId = docEntry.ContactId,
                        ContactName = new Contacts().GetCandidateName(docEntry.ContactId),
                        Document = docEntry.Doc
                    });
                    continue;
                }

                var numberOfPages = reader.NumberOfPages;

                // Iterate through all pages
                for (var currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
                {
                    // Determine page size for the current page
                    document.SetPageSize(reader.GetPageSizeWithRotation(currentPageIndex));
                    // Create page
                    document.NewPage();
                    var importedPage = writer.GetImportedPage(reader, currentPageIndex);
                    // Determine page orientation
                    var pageOrientation = reader.GetPageRotation(currentPageIndex);
                    switch (pageOrientation)
                    {
                        case 90:
                            content.AddTemplate(importedPage, 0, -1, 1, 0, 0, reader.GetPageSizeWithRotation(currentPageIndex).Height);
                            break;
                        case 270:
                            content.AddTemplate(importedPage, 0, 1, -1, 0, reader.GetPageSizeWithRotation(currentPageIndex).Width, 0);
                            break;
                        default:
                            content.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0);
                            break;
                    }
                }
            }
            document.Close();
        }
Пример #2
0
        /// <summary>
        /// This function merge the cvs and send the pdf document path
        /// </summary>
        /// <param name="contactIds"></param> 
        /// <param name="jobId"></param>
        /// <returns></returns>
        public static BulkPrintCvResult BulkCvPrint(string contactIds, int jobId)
        {
            var result = new BulkPrintCvResult
            {
                IsError = true,
                ErrorCvs = new List<BulkPrintErrorCvs>(),
                PdfDownloadUrl = "",
                Error = "Unexpected error occurred while generating the document!"
            };
            try
            {
                List<BulkPrintDocEntry> cvs;
                if (jobId > 0)
                {
                    //get candidate cvs for job
                    cvs = (contactIds.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
                        .Select(candidateId => GetDocumentByDocType(new[] { 4 }, Convert.ToInt32(candidateId), jobId).ToList())
                        .Where(documents => documents.Count > 0)
                        .Select(documents => new BulkPrintDocEntry
                        {
                            Doc = documents[0],
                            ContactId = documents[0].RefId,
                            CoveringLetterHtml = "",
                            CvFileName =
                                GetDocumentLocation(documents[0].DocumentTypeValue, documents[0].RefId, jobId) + documents[0].FileName
                        })).ToList();
                }
                else
                {
                    // get the latest cvs for the candidates 
                    cvs = (contactIds.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
                        .Select(candidateId => GetDocumentByDocType(new[] { 4 }, Convert.ToInt32(candidateId), jobId).ToList())
                        .Where(documents => documents.Count > 0)
                        .Select(documents => new BulkPrintDocEntry
                        {
                            Doc = documents[0],
                            ContactId = documents[0].RefId,
                            CoveringLetterHtml = "",
                            CvFileName =
                                GetDocumentLocation(documents[0].DocumentTypeValue, documents[0].RefId) + documents[0].FileName
                        })).ToList();
                }
                // no cvs found
                if (cvs.Count <= 0)
                {
                    result.IsError = true;
                    result.Error = "No Cvs found!";
                    return result;
                }


                // define file name
                var folderName = DateTime.Now.ToString("ddMMyyyyHHmmss");
                var outputFileName = Guid.NewGuid().ToString();
                // set file location
                var printFileLocation = ConfigurationManager.AppSettings["appDocPath"] + "temp\\" + folderName + "\\";
                if (!Directory.Exists(printFileLocation))
                    Directory.CreateDirectory(printFileLocation);
                // set output file name
                outputFileName = printFileLocation + outputFileName + ".pdf";
                // do the merge
                MergePdf(cvs, outputFileName, ref result);
                if (!File.Exists(outputFileName)) return result;
                else if (result.ErrorCvs.Count == cvs.Count)
                {
                    // all cvs found errors while generating pdf
                    return result;
                }
                outputFileName = "system/temp/" + folderName + "/" + Path.GetFileName(outputFileName);

                result.PdfDownloadUrl = outputFileName;
                return result;
            }
            catch (Exception)
            {
                return result;
            }

        }