Using XMLWorker to reference external css file in ASP.Net

Last Reply 6 months ago By kalpesh

Posted 6 months ago

I know  I am mixing htmlworker and xmlworker horribly, please forgive.

I had an htmlworker that I succeeded in converting to xmlworker.

The big problem I am having is including css that you see at the bottom of the code so that the font size could be smaller than it currently shows when the PDF is generated.

Could you please help me with making it complete xmlworker that calls an external css file?

There is more code for the string body. I just put a small sample to show what I am trying to accomplish.

Many thanks in advance

 

string body = "<br />";
        body = body + "<table border='1' style='width:90%; text-align:left'>";
        body = body + "   <tr>";
        body = body + "  <td style='width:20%'>Title: <input name='txtTitle' type='text' id='txtTitle' style='color: #0093B2;font-weight:bold;' />" + lblTitle.Text + "</td><td style='width:20%'>Preparer Name: <input id='txtpreparerName' style='color: #0093B2;font-weight:bold;' />" + lblpreparerName.Text + "</td><td style='width:20%'>Email: <input name='txtEmail' type='text' id='txtEmail' style='color: #0093B2;font-weight:bold;' />" + lblEmail.Text + "</td><td style='width:20%'>Date: <span id='signDate' style='color: #0093B2;font-weight:bold;'>" + lblSignDate.Text + "</span></td>";
        body = body + "  </tr>";
        body = body + " </table><br /><br />";
        body = body + " <br /><br />";

        StringReader sr = new StringReader(body.ToString());

        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        iTextSharp.tool.xml.pipeline.css.ICSSResolver CSSResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
        using (MemoryStream memoryStream = new MemoryStream())
        {
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
            pdfDoc.Open();

            var xmlWorkerHelper = XMLWorkerHelper.GetInstance();
            var cssResolver = new StyleAttrCSSResolver();
            var xmlWorkerFontProvider = new XMLWorkerFontProvider();
            foreach (string font in fonts)
            {
                xmlWorkerFontProvider.Register(font);
            }
            var cssAppliers = new CssAppliersImpl(xmlWorkerFontProvider);
            var htmlContext = new HtmlPipelineContext(cssAppliers);
            htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            PdfWriterPipeline pdfWriterPipeline = new PdfWriterPipeline(pdfDoc, pdfWriter);
            HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, pdfWriterPipeline);
            CssResolverPipeline cssResolverPipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
            XMLWorker xmlWorker = new XMLWorker(cssResolverPipeline, true);
            //htmlparser.Parse(sr);
            XMLParser xmlParser = new XMLParser(xmlWorker);
            pdfDoc.Close();
            byte[] bytes = memoryStream.ToArray();
            memoryStream.Close();

//css
body
{
    font-family: Century Gothic;
    font-size: 10pt;
    color: #000000;
}