在document.close()

时间:2015-11-05 23:54:49

标签: c# asp.net pdf itextsharp

我从HTML页面创建文档,然后将其另存为PDF。

在关闭文档之前,我需要在文档的末尾添加一个PNG。

PNG文件是一个siganture。

我的文件声明

// Create a Document object
var document = new Document(PageSize.A4, 50, 50, 25, 25);

// Create a new PdfWriter object, specifying the output stream
var output = new FileStream(Server.MapPath("MyFirstPDF.pdf"), FileMode.Create);
var writer = PdfWriter.GetInstance(document, output);

// Open the Document for writing
document.Open();

撰写HTML页面

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);

HtmlTextWriter hWriter = new HtmlTextWriter(sw);
base.Render(hWriter);

将元素添加到文档并关闭。这是我想在关闭(并生成pdf)之前将png(本地服务器)附加到文档的位置。

string imgFileName = "signature" + ".png";
string outputFilePath = ConfigHelper.GetImageOutputFullPath(imgFileName);

var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(PageResultCleaned), null);

// Enumerate the elements, adding each one to the Document...
foreach (var htmlElement in parsedHtmlElements)
    document.Add(htmlElement as IElement);

// Close the Document - this saves the document contents to the output stream
document.Close();

1 个答案:

答案 0 :(得分:0)

我通过在将HTML添加到文档之前将图像作为img元素添加到HTML中来解决这个问题。

PageResultCleaned = PageResultCleaned.Replace(@"<h3>Sign Here:</h3>", "<img src=\"" + outputFilePath + "\" alt=\"Signature\" height=\"100\" width=\"300\">");