Пример #1
0
        public PdfHtmConverter(ConvertRequest convertRequest)
        {
            _convertRequest = convertRequest;
            var registeredFontCounts = PdfFontFactory.RegisterDirectory(FONT_DIRECTORY);

            Console.WriteLine($"Number of the font registered: {registeredFontCounts}");
        }
Пример #2
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());
                }
            }
        }
Пример #3
0
        public Task Generate(bool overlay = true)
        {
            return(Task.Run(() =>
            {
                Repository.InitPaths();
                PdfFontFactory.RegisterDirectory(Repository.FontPath);
                var registeredFonts = PdfFontFactory.GetRegisteredFonts();
                var registeredFamilies = PdfFontFactory.GetRegisteredFamilies();

                var templateFile = overlay? Repository.GetOverlayTemplateFile(Card.Faction.Name, Card.Type.Name) :
                                   Repository.GetTemplateFile(Card.Faction.Name, Card.Type.Name);

                if (!File.Exists(templateFile))
                {
                    throw new Exception("Template could not be found");
                }

                XDocument svg;
                try
                {
                    // load svg for template
                    svg = XDocument.Load(templateFile);
                    // find the overlay root
                    Overlay = svg.Root.Elements(NS + "g").Single(x => x.Attribute("id").Value == "overlaylayer");
                }
                catch (Exception e)
                {
                    throw new Exception("Template could not be loaded", e);
                }

                // apply card information to svg template
                if (Template.ShowName)
                {
                    SetName();
                }
                if (Template.ShowType)
                {
                    SetType();
                }
                if (Template.ShowGoldCost)
                {
                    SetGoldCost();
                }
                if (Template.ShowLoyalty)
                {
                    SetLoyalty();
                }
                if (Template.ShowText)
                {
                    SetText();
                }
                if (Template.ShowAttack)
                {
                    SetAttack();
                }
                if (Template.ShowDefense)
                {
                    SetDefense();
                }
                if (Template.ShowDiscipline)
                {
                    SetDiscipline();
                }
                if (Template.ShowArt)
                {
                    SetArt();
                }
                if (Template.ShowInfo)
                {
                    SetInfo();
                }

                // write svg
                var svgFile = overlay? Repository.GetOverlaySvgFile(Card.Guid) : Repository.GetSvgFile(Card.Guid);

                File.WriteAllText(svgFile, svg.ToString());

                // write json
                string cardString = JsonConvert.SerializeObject(Card, Formatting.Indented);
                File.WriteAllText(Repository.GetJsonFile(Card.Guid), cardString);

                if (!overlay)
                {
                    var merge = XDocument.Parse("<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" height=\"340.15747\" width=\"244.48819\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"></svg>");
                    var border = XDocument.Load(Repository.GetPrintBorderFile(Card.Faction.Name, Card.Type.Name, "svg"));
                    border.Root.SetAttributeValue("height", "340.15747");
                    border.Root.SetAttributeValue("width", "244.48819");
                    border.Root.SetAttributeValue("y", "0");
                    border.Root.SetAttributeValue("x", "0");
                    merge.Root.Add(border.Root);

                    svg.Root.SetAttributeValue("height", "325.98425");
                    svg.Root.SetAttributeValue("width", "230.31496");
                    svg.Root.SetAttributeValue("y", "7.0866184");
                    svg.Root.SetAttributeValue("x", "7.0866184");
                    merge.Root.Add(svg.Root);


                    File.WriteAllText(Repository.GetPrintSvgFile(Card.Guid), merge.ToString());
                }
            }));
        }
        //static private ResumeData data;
        public DocumentBuilder()//ResumeData import)
        {
            //data = import;

            String    docname = "Test";
            PdfWriter writer  = null;
            bool      read    = false;

            try  // Try writing to file
            {
                writer = new PdfWriter(docname + ".pdf");
                read   = true;
            }
            catch  // Catch if file is open and display message to user
            {
                string            message = "Please close the open PDF or save using a different name.";
                string            caption = "Active PDF Open in Another Window";
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                DialogResult      msg;
                msg = MessageBox.Show(message, caption, buttons);
                Console.WriteLine("Please close the pdf file that is open");
            }
            if (read)
            {
                PdfDocument pdf = new PdfDocument(writer);
                Document    doc = new Document(pdf);

                /* Import Data */
                JSONHandler jhandle = new JSONHandler();
                ResumeData  data    = jhandle.ReadFromJSON();


                /*---------------------------           Writing to doc            ------------------------------------*/



                /* Register Fonts */
                PdfFontFactory.RegisterDirectory("C:\\Windows\\Fonts");
                PdfFont Courier = PdfFontFactory.CreateRegisteredFont("Courier New");
                PdfFont Gothic  = PdfFontFactory.CreateRegisteredFont("Century Gothic");

                /* Create Content */
                Paragraph Name = new Paragraph(data.Name)
                                 .SetTextAlignment(TextAlignment.CENTER)
                                 .SetFont(Courier)
                                 .SetFontSize(34);

                Paragraph Bio = new Paragraph(data.Email + " | " + data.Phone + " | " + data.Address1)
                                .SetTextAlignment(TextAlignment.CENTER)
                                .SetFont(Gothic)
                                .SetFontSize(9);

                Paragraph Site = new Paragraph()
                                 .Add(new Link("Barnes7619.com", PdfAction.CreateURI("https://Barnes7619.com")).SetUnderline())
                                 .SetTextAlignment(TextAlignment.CENTER)
                                 .SetFont(Gothic)
                                 .SetFontSize(9);


                Table SummaryHead = CreateSeparator("Summary", Courier);

                Table EducationHead = CreateSeparator("Education & Training", Courier);

                Table SoftwareHead = CreateSeparator("Software Experience", Courier);

                Table ExperienceHead = CreateSeparator("Experience", Courier);

                Table SkillsHead = CreateSeparator("Skills", Courier);



                ///* Formatting */
                //Name.Alignment = Element.ALIGN_CENTER;
                //Bio.Alignment = Element.ALIGN_CENTER;
                //Site.Alignment = Element.ALIGN_CENTER;

                /* Add Content to Document */
                doc.Add(Name);
                doc.Add(Bio);
                doc.Add(Site);
                doc.Add(SummaryHead);
                doc.Add(EducationHead);
                doc.Add(SoftwareHead);
                doc.Add(ExperienceHead);
                doc.Add(SkillsHead);

                /* Close Document */
                doc.Close();



                /* -------------------------  Completed Message  --------------------------- */
                string            message = "Resume Generated!";
                string            caption = "Finished";
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                DialogResult      msg;
                msg = MessageBox.Show(message, caption, buttons);
            }
        }