Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6859287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:15:52+00:00 2026-05-27T02:15:52+00:00

I am generating PDF using iTextSharp.dll, but the problem is that I am not

  • 0

I am generating PDF using iTextSharp.dll, but the problem is that I am not able to apply that CSS. I have one div:

 <div id="personal" class="headerdiv">
      Personal Data
 </div>

now my .aspx.cs code is like this:

   iTextSharp.text.html.simpleparser.StyleSheet styles = new         iTextSharp.text.html.simpleparser.StyleSheet();

    styles.LoadTagStyle("#headerdiv", "height", "30px");
    styles.LoadTagStyle("#headerdiv", "font-weight", "bold");
    styles.LoadTagStyle("#headerdiv", "font-family", "Cambria");
    styles.LoadTagStyle("#headerdiv", "font-size", "20px");
    styles.LoadTagStyle("#headerdiv", "background-color", "Blue");
    styles.LoadTagStyle("#headerdiv", "color", "White");
    styles.LoadTagStyle("#headerdiv", "padding-left", "5px");

    HTMLWorker worker = new HTMLWorker(document);
    worker.SetStyleSheet(styles);


    // step 4: we open document and start the worker on the document 
    document.Open();
    worker.StartDocument();
    // step 5: parse the html into the document      
    worker.Parse(reader);
    // step 6: close the document and the worker     
    worker.EndDocument();
    worker.Close();
    document.Close();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:15:52+00:00Added an answer on May 27, 2026 at 2:15 am

    There’s a couple of things going on here. First and foremost, the HTML/CSS parser in iText and iTextSharp are far from complete. They’re definitely very powerful but still have a ways to go. Each version gets better so always make sure that you’re using the latest version.

    Second, I’ve seen more HTML/CSS activity in an add-on for iText/iTextSharp called XMLWorker that you might want to look at. You don’t “load styles” anymore, you just pass raw HTML/CSS in and it figures out a lot of things. You can see some examples here, see a list of supported CSS attributes here, download it here (and get the two missing files here and here).

    Third, LoadTagStyle is for loading style attributes for HTML tags, not CSS IDs or Classes. You want to use LoadStyle to load by class:

    styles.LoadStyle("<classname>", "<attribute>", "<value>");
    

    Unfortunately this method still doesn’t do what you want it to do always. For instance, to change the font size you’d think you’d say:

    styles.LoadStyle("headerdiv", "font-size", "60ptx);
    

    But to get it to work you can only use relative HTML font sizes (1,2,-1, etc) or PT sizes and you must use the size property:

    styles.LoadStyle("headerdiv", "size", "60pt");
    //or
    styles.LoadStyle("headerdiv", "size", "2");
    

    The LoadStyle honestly feels like an afterthought that was only partially completed and I recommend not using it actually. Instead I recommend writing the style attributes directly inline if you can:

    string html = "<div id=\"personal\" class=\"headerdiv\" style=\"padding-left:50px;font-size:60pt;font-family:Cambria;font-weight:700;\">Personal Data</div>";
    

    Obviously this defeats the points of CSS and once again, that’s why they’re working on the new XMLWorker above.

    Lastly, to use fonts by name you have to register them with iTextSharp first, it won’t go looking for them:

    iTextSharp.text.FontFactory.Register(@"c:\windows\fonts\cambria.ttc", "Cambria");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that is manually generating a PDF using PDFsharp in C#.
I've searched a lot but could not solve this problem. I am generating PDF
I have an old project that is using iTextSharp library for PDF generation. iTextSharp
I am generating a pdf file in asp.net c# using itextsharp. i am not
We are generating pdf using apache's xsl fo engine, namely fop 0.95. We have
I am generating one PDF from the Code Behind File using StringWriter and HtmlTextWriter.
I have been using Itextsharp for prefilling the fillable pdfs in .aspx pages. But
I am generating a pdf using iTextSharp. I would like to display it on
I'm dynamically generating a PDF using ABCpdf which contains a table of contents that
I generated latex template using perl,and complied with MikTeX, its generating pdf. But I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.