使用itextsharp.xmlworker.dll创建PDF时不应用css

时间:2017-06-12 09:46:49

标签: c# pdf itext pdf-generation

我想生成PDF,但它没有采用我需要的所有css,比如它不应用Margin,Padding,Align等。我也想把图像放在我的PDF上,但我不知道怎么做?以下是我的代码

MemoryStream memoryStream = new MemoryStream();
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);        
PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);

htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());

//create a cssresolver to apply css
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);

cssResolver.AddCss("div{color: red; text-align:center; font-size:30px;}", true);
cssResolver.AddCss("h1{color: green;}", true);

//Create and attach pipline, without pipline parser will not work on css
IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(doc, writer)));

//Create XMLWorker and attach a parser to it
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser xmlParser = new XMLParser(worker);

//All is well open documnet and start writing.
doc.Open();
string htmltext = "<html><body><h1>This is Heading </h1><div>This is a div content.</div></body></html>";
xmlParser.Parse(new StringReader(htmltext));

//Done! close the documnet
doc.Close();

0 个答案:

没有答案