C# ITextSharp:指定 HTML 类或 ID CSS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9612212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:09:42  来源:igfitidea点击:

ITextSharp: Specify HTML Classes or ID CSS

c#htmlcssitext

提问by sazr

I am converting some HTML into a .pdf file using ITextSharp.

我正在使用 ITextSharp 将一些 HTML 转换为 .pdf 文件。

Is it possible to set a classes css in ITextSharp or can I only set HTML elements CSS?

是否可以在 ITextSharp 中设置类 css 或者我只能设置 HTML 元素 CSS?

For example: If I convert the following HTML

例如:如果我转换以下 HTML

<p class="redBigText">test</p>

Can I create a ITextSharp StyleSheet object and specify the CSS for the class redBigText?

我可以创建一个 ITextSharp StyleSheet 对象并为类redBigText指定 CSS吗?

StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
styles.LoadTagStyle(".redBigText", "font-size", "50px");
styles.LoadTagStyle(".redBigText", "color", "#ff0000");
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(mainContents), styles);

Or can I only set CSS elements in ITextSharp?

或者我只能在 ITextSharp 中设置 CSS 元素?

StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
styles.LoadTagStyle("P", "font-size", "50px");
styles.LoadTagStyle("P", "color", "#ff0000");
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(mainContents), styles);

回答by kuujinbo

Yes, you can specifiy a CSS class name:

是的,您可以指定一个 CSS 类名:

string Html = @"
<h1>h1</h1>
<p>Default paragraph</p>  
<p class='redBigText'>A paragraph with CSS class</p>  
";
StyleSheet styles = new StyleSheet();
styles.LoadStyle("redBigText", "size", "20pt");
styles.LoadStyle("redBigText", "color", "#ff0000");

It's documented here.

记录在这里

Unfortunately you cannot specify idattributes. Also be aware that if you mix and match LoadTagStyle()and LoadStyle()calls, the LoadTagStyle()wins. For example:

不幸的是,您无法指定id属性。另请注意,如果您混合搭配LoadTagStyle()LoadStyle()跟注,则LoadTagStyle()获胜。例如:

StyleSheet styles = new StyleSheet();
styles.LoadTagStyle("p", "size", "10pt");
styles.LoadTagStyle("p", "color", "#0000ff");     
styles.LoadStyle("redBigText", "size", "20pt");
styles.LoadStyle("redBigText", "color", "#ff0000");

Here, all paragraphs are blue and 10pt.

在这里,所有段落都是蓝色和 10pt。

回答by Syed Ali

although it is old post but might some one can get help by following solution.

虽然它是旧帖子,但可能有人可以通过以下解决方案获得帮助。

You have to convert the following line

您必须转换以下行

<p class="redBigText">test</p>

to

<p style="font-size : 50px;color : #ff0000">test</p>

itextsharp will now apply inline style to your html, it will not recognized css classes, as they are referenced from external file.

itextsharp 现在将内联样式应用于您的 html,它不会识别 css 类,因为它们是从外部文件引用的。

You can use This inline styles toolto convert you css classes to inline style.

您可以使用 此内联样式工具将您的 css 类转换为内联样式。

  • Just copy the css from your classes to your html page.
  • copy the html to inliner tool.
  • 只需将 css 从您的类复制到您的 html 页面。
  • 将 html 复制到内联工具。

You will get the inlined css html page. ~Happy Codding

您将获得内联的 css html 页面。~快乐编码