我正在尝试使用iTextSharp将简单的HTML转换为PDF。请考虑以下情况,其中HTML div宽度不适合PDF页面大小,PDF格式中的内容为空白。
TextReader reader = new StringReader(html);
using (MemoryStream memoryStream = new MemoryStream())
{
using (Document document = new Document(PageSize.A4, 0, 0, 36, 0))
{
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destPath, FileMode.Create));
writer.ViewerPreferences = PdfWriter.PageModeUseOutlines | PdfWriter.PageLayoutSinglePage;
writer.CloseStream = false;
document.Open();
//writer.PageEvent = new HeaderFooterHelper();
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser parser = new XMLParser(true, worker, Encoding.Unicode);
parser.Parse(reader);
worker.Close();
document.Close();
}
}
HTML输入:
<html>
<head>
</head>
<body>
<div style="width:1000px">
Blood is needed for emergencies and for people who have cancer, blood disorders,sickle cell, anemia and other illnesses. Some people need regular blood transfusions to live. For nearly 5 million people who receive blood transfusions every year, your donation can make the difference between life and death
</div>
</body>
</html>
我们正在将从EVO生成的PDF迁移到iTextSharp。这是迁移过程中遇到的问题之一。因此,无法根据pdf页面大小更改/操作内容。具体而言,寻找具有PageSize.A4选项的解决方案。