Add CSS Style And Download Using iTextSharp

INTRODUCTION

Let’s discuss how to add CSS in iTextSharp in C# ASP. Table is one of the most used elements in PDF file generation using ASP.NET application. Here we have “PdfTable” to make the structure in PDF file and “PdfCell” is used to make Rows and Columns inside table.

MERGE ROWS

You can merge the rows using iTextSharp. Find the below URL for more reference.
MERGE COLUMNS

Here you have an option to merge the Rows and Column using iTextSharp.
  1. PdfPTable table = newPdfPTable(4);  
  2. table.TotalWidth = 400 f;  
  3. table.LockedWidth = true;  
  4. PdfPCell header = newPdfPCell(newPhrase("Header"));  
  5. header.Colspan = 4;  
  6. table.AddCell(header);  
  7. table.AddCell("CellValue 1");  
  8. table.AddCell("CellValue 2");  
  9. table.AddCell("CellValue 3");  
  10. table.AddCell("CellValue 4");  
  11. PdfPTable nested = newPdfPTable(1);  
  12. nested.AddCell("Row 1");  
  13. nested.AddCell("Row 2");  
  14. nested.AddCell("Row 3");  
  15. PdfPCell nesthousing = newPdfPCell(nested);  
  16. nesthousing.Padding = 0 f;  
  17. table.AddCell(nesthousing);  
  18. PdfPCell bottom = newPdfPCell(newPhrase("bottom"));  
  19. bottom.Colspan = 3;  
  20. table.AddCell(bottom);  
  21. doc.Add(table);  

OUTPUT

 

SOURCE CODE

This is the source code to apply CSS style with pdf file and download the file.

 

  1. publicstaticstring CSS_STYLE = "th { background-color: #C0C0C0; font-size: 16pt; }"  
  2. "td { font-size: 10pt; }";  
  3. publicstaticstring HTML = "<html><body><table class='table-bordered'>"  
  4. "<thead><tr><th>Customer Name</th><th>Customer's Address</th> </tr></thead>"  
  5. "<tbody><tr><td> Shankar </td><td> Chennai </td></tr>"  
  6. "<tr><td> Krishnaa </td><td> Trichy </td></tr></tbody>"  
  7. "</table></body></html>";  

 

In the above code, you can see that style is applied on the code.

This is the method to generate the PDF file.
  1. publicvoid GeneratePdf(String file) {  
  2.     Document document = new Document();  
  3.     PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));  
  4.     document.open();  
  5.     CSSResolver cssResolver = new StyleAttrCSSResolver();  
  6.     CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS_STYLE.getBytes()));  
  7.     cssResolver.addCss(cssFile);  
  8.     // HTML  
  9.     HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);  
  10.     htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());  
  11.     // Pipelines  
  12.     PdfWriterPipeline pdfFile = new PdfWriterPipeline(document, writer);  
  13.     HtmlPipeline html = new HtmlPipeline(htmlContext, pdfFile);  
  14.     CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);  
  15.     // XML Worker  
  16.     XMLWorker worker = new XMLWorker(css, true);  
  17.     XMLParser p = new XMLParser(worker);  
  18.     p.parse(new ByteArrayInputStream(HTML.getBytes()));  
  19.     document.close();  
  20. }  

OUTPUT

You can see the output will be displayed like this,

 

Thank you.

Next Recommended Reading Add image in PDF using iTextSharp