Exemplo n.º 1
0
        /// <summary>
        /// Merge pdf files.
        /// </summary>
        /// <param name="sourceFiles">PDF files being merged.</param>
        /// <returns></returns>
        public static byte[] MergeFiles(string htmlDocument, IList <byte[]> sourceFiles, string pdfConverterLicenseKey)
        {
            PdfConverter pdfConverter = new PdfConverter
            {
                PdfStandardSubset = PdfStandardSubset.Pdf_A_1b
            };

            if (String.IsNullOrEmpty(pdfConverterLicenseKey) == false)
            {
                pdfConverter.LicenseKey = pdfConverterLicenseKey;
            }

            Winnovative.WnvHtmlConvert.PdfDocument.Document doc = pdfConverter.GetPdfDocumentObjectFromHtmlString(htmlDocument);
            try
            {
                if (sourceFiles != null)
                {
                    foreach (var file in sourceFiles)
                    {
                        using (var fileStream = new MemoryStream(file))
                        {
                            doc.AppendDocument(new Winnovative.WnvHtmlConvert.PdfDocument.Document(fileStream));
                        }
                    }
                }

                return(doc.Save());
            }
            finally {
                doc.Close();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts the specified html documents to a single pdf file.
        /// </summary>
        /// <param name="htmlDocuments"></param>
        /// <param name="pdfAttachments"></param>
        /// <param name="pdfConverterLicenseKey"></param>
        /// <returns></returns>
        public static byte[] ConvertHtmlToPdf(IEnumerable <string> htmlDocuments, ICollection <PdfAttachment> pdfAttachments, string pdfConverterLicenseKey)
        {
            //TODO: Add the ability to customize layout and margins.

            PdfConverter pdfConverter = new PdfConverter
            {
                PdfStandardSubset = PdfStandardSubset.Pdf_A_1b
            };

            if (String.IsNullOrEmpty(pdfConverterLicenseKey) == false)
            {
                pdfConverter.LicenseKey = pdfConverterLicenseKey;
            }

            Winnovative.WnvHtmlConvert.PdfDocument.Document doc = null;
            try
            {
                if (htmlDocuments != null)
                {
                    foreach (var htmlDocument in htmlDocuments)
                    {
                        if (doc == null)
                        {
                            doc = pdfConverter.GetPdfDocumentObjectFromHtmlString(htmlDocument);
                        }
                        else
                        {
                            doc.AppendDocument(pdfConverter.GetPdfDocumentObjectFromHtmlString(htmlDocument));
                        }
                    }
                }

                if (doc == null)
                {
                    throw new InvalidOperationException("No PDF Document Object was created.");
                }

                //Use iTextSharp to add all attachments
                if (pdfAttachments != null && pdfAttachments.Any())
                {
                    var reader = new PdfReader(doc.Save());

                    using (var outputPdfStream = new MemoryStream())
                    {
                        var stamper = new PdfStamper(reader, outputPdfStream);
                        foreach (var pdfAttachment in pdfAttachments)
                        {
                            stamper.AddFileAttachment(pdfAttachment.Description, pdfAttachment.Data, pdfAttachment.FileName, pdfAttachment.FileDisplayName);
                        }
                        stamper.Close();
                        outputPdfStream.Flush();
                        return(outputPdfStream.GetBuffer());
                    }
                }
                else
                {
                    return(doc.Save());
                }
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close();
                }
            }
        }