我想知道我有没有办法
使用PdfSharp
having external CSS
classes included in the HTML
从HTML片段创建PDF。
我找到了一种使用HTML内容生成PDF文件的方法,但是我的css类并没有被应用到pdf中。
下面是我对简单html字符串所做的操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System.Drawing;
using TheArtOfDev.HtmlRenderer.PdfSharp;
using PdfSharp;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PdfDocument pdf = PdfGenerator.GeneratePdf("<h1>tests</h1><p>test para</p>", PageSize.A4,20,null,null,null);
pdf.Save(@"C\testPDF.pdf");
}
}
ANd是这样生成的:
但是我想要的是包含这样的HTML字符串:
<h1>tests</h1><p class='para1'>test para</p>
有办法做到这一点吗?使用不同的库并不是问题,唯一的问题是使用的库应该是开源的。
发布于 2016-05-12 13:03:42
您可以将您的样式包含在头部分,它将被正确地呈现:
var testHtml = @"<head>
<style>
.test {
background-color: linen;
color: maroon;
}
</style>
</head>
<body class=""test"">
<p>
<h1>Hello World</h1>
This is html rendered text with css and image.
</p>
<p>
<img src=""http://fc62.deviantart.com/fs11/i/2006/236/d/e/The_Planet_Express_Ship_by_gasclown.jpg"" height=""100"" width=""100"">
</p>
</body>";
更多-您可以添加url到样式表,这将是工作!
https://stackoverflow.com/questions/31261981
复制相似问题