I'm trying to use iTextSharp to produce a PDF file from an Html TextBox type 
control in ASP.NET.

The problem is that the external CSS file is being ignored. I have followed the 
examples on your website but to no avail. None of the CSS is being applied.

I'm using

*         Visual Studio 2010 (C#)

*         ASP.NET Framework 4.0

*         itextsharp.dll vers. 5.4.4.0

*         itextsharp.xmlworker.dll vers. 5.4.4.0

*         CSS version 3.0

My Textbox control contains the following (which render correctly on the 
webpage they are displayed on before generating the PDF from it)...
<table class="Results">
  <tbody>
    <tr>
      <th class="CaseNumber">Case Number</th>
      <th class="AnimalName">Animal Name</th>
      <th class="RegNo">Reg No.</th>
    </tr>
    <tr>
      <td style="height:3px" colspan="4"><hr></td>
    </tr>
    <tr>
      <td class="CaseNumber">27842</td>
      <td class="AnimalName">Te Mania Modest M126</td>
      <td class="RegNo"></td>
    </tr>
  </tbody>
</table>

The css file contains:
table.Results
{
                margin-left:auto;
                margin-right:auto;
                width:95%;
                /*horiz-align: center;*/
                text-align: center;
                border: none;
                font-size: 6px;
                font-family: Arial;
}

table.Results  th
{
                font-size: 11px;
                text-align: left;
}
table.Results  th.CaseNumber
{
                color: red;           /* To see if this CSS is applied */
                width: 20%;
                text-align: center;
}
table.Results  th.AnimalName
{
                width: 35%;
}
table.Results  th.RegNo
{
                width: 25%;
}

table.Results  td
{
                text-align: left;
}
table.Results  td.CaseNumber
{
                width: 20%;
                text-align: center;
}
table.Results  td.AnimalName
{
                width: 35%;
}
table.Results  td.RegNo
{
                width: 25%;
}

The code is C#
FontFactory.RegisterDirectories();
var styles = new StyleSheet();
var doc = new Document(PageSize.A4);
var writer = PdfWriter.GetInstance(doc, outStream);

writer.PageEvent = new PDFHeaderFooter(doc, htmlFooter1Text, htmlHeaderText, 
htmlFooterText, styles);

try
{
   doc.Open();

   var htmlContext = new HtmlPipelineContext(null);
   htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());

   htmlContext.SetImageProvider(new ImageProvider(imageAndLinkRoot));
   htmlContext.SetLinkProvider(new LinkProvider(imageAndLinkRoot));

   var cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
   foreach (var styleFile in styleFiles)
   {
      try
      {
         cssResolver.AddCssFile(styleFile, true);
      }
      catch (Exception)
      {
         throw new Exception("Can't open CSS file: " + styleFile);
      }
   }

   var pipeline = new CssResolverPipeline(cssResolver,
                  new HtmlPipeline(htmlContext,
                     new PdfWriterPipeline(doc, writer)));

   var worker = new XMLWorker(pipeline, true);
   var p = new XMLParser(worker);

   p.Parse(new StringReader(htmlBodyText ?? String.Empty));
}
catch (Exception ex)
{
   var errorMsg = String.Format(PdfErrorMsgTmpl, ex.Message);

   //Display parser errors in PDF.
   var paragraph = new Paragraph(errorMsg);
   var text = paragraph.Chunks[0];
   if (text != null)
   {
      text.Font.Color = BaseColor.RED;
   }
   doc.Add(paragraph);

   // Let the calling routine know
   throw new Exception(errorMsg);
}
finally
{
   writer.CloseStream = false;
   doc.Close();
}

I've checked in my VS2010 debugger and the cssResolver has found my CSS file 
and loaded it.

Regards
Mike

------------------------------------------------------------------------------
Flow-based real-time traffic analytics software. Cisco certified tool.
Monitor traffic, SLAs, QoS, Medianet, WAAS etc. with NetFlow Analyzer
Customize your own dashboards, set traffic alerts and generate reports.
Network behavioral analysis & security monitoring. All-in-one tool.
http://pubads.g.doubleclick.net/gampad/clk?id=126839071&iu=/4140/ostg.clktrk
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to