Exemple #1
0
        public static byte[] ConvertToPdfWithTags(string html, string title, string docOptions)
        {
            DocOptions documentOptions = DocOptions.None;

            if (!string.IsNullOrEmpty(docOptions))
            {
                int options;
                if (int.TryParse(docOptions, out options))
                {
                    documentOptions = (DocOptions)options;
                }
            }
            PdfFontFactory.RegisterDirectory(System.Web.Hosting.HostingEnvironment.MapPath("~/content/fonts/"));
            ConverterProperties props = new ConverterProperties();

            FontProvider fp = new FontProvider();

            fp.AddDirectory(System.Web.Hosting.HostingEnvironment.MapPath("~/content/fonts/"));
            props.SetFontProvider(fp);
            props.SetTagWorkerFactory(new DefaultTagWorkerFactory());

            ImageData imageFirst =
                ImageDataFactory.Create(System.Web.Hosting.HostingEnvironment.MapPath(headerPage1));

            ImageData imageAll =
                ImageDataFactory.Create(System.Web.Hosting.HostingEnvironment.MapPath(headerAllPages));

            using (var workStream = new MemoryStream())
            {
                using (var pdfWriter = new PdfWriter(workStream, new WriterProperties().AddUAXmpMetadata().SetPdfVersion
                                                         (PdfVersion.PDF_2_0).SetFullCompressionMode(true)))
                {
                    PdfDocument pdfDoc = new PdfDocument(pdfWriter);
                    pdfDoc.GetCatalog().SetLang(new PdfString("en-GB"));


                    pdfDoc.GetCatalog().SetViewerPreferences(new PdfViewerPreferences().SetDisplayDocTitle(true));
                    if (documentOptions > 0)
                    {
                        pdfDoc.AddEventHandler(PdfDocumentEvent.END_PAGE, new PublicReportHeaderFooter(documentOptions, title, imageFirst, imageAll));
                    }
                    //Set meta tags
                    var pdfMetaData = pdfDoc.GetDocumentInfo();
                    pdfMetaData.AddCreationDate();
                    pdfMetaData.GetProducer();
                    pdfMetaData.SetCreator("iText Software");
                    //Set the document to be tagged
                    pdfDoc.SetTagged();

                    //Extra metadata tags available
                    //pdfMetaData.SetAuthor("");
                    using (var document = HtmlConverter.ConvertToDocument(html, pdfDoc, props))
                    {
                        //Can do more with document here if necessary
                    }

                    //Returns the written-to MemoryStream containing the PDF.
                    return(workStream.ToArray());
                }
            }
        }
 private static void Run(DocOptions obj)
 {
     throw new NotImplementedException();
 }