我需要将N个PDF文件合并为一个。我创建一个空白文件第一将使用ITextSharp从html创建的N个pdf文件合并到另一个空白pdf文件
byte[] pdfBytes = null;
var ms = new MemoryStream();
var doc = new iTextSharp.text.Document();
var cWriter = new PdfCopy(doc, ms);
后来我骑车经过HTML字符串数组
foreach (NBElement htmlString in someElement.Children())
{
byte[] msTempDoc = getPdfDocFrom(htmlString.GetString(), cssString.GetString());
addPagesToPdf(cWriter, msTempDoc);
}
在getPdfDocFrom我使用XMLWorkerHelper创建PDF文件,并
private byte[] getPdfDocFrom(string htmlString, string cssString)
{
var tempMs = new MemoryStream();
byte[] tempMsBytes;
var tempDoc = new iTextSharp.text.Document();
var tempWriter = PdfWriter.GetInstance(tempDoc, tempMs);
tempDoc.Open();
using (var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssString)))
{
using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlString)))
{
//Parse the HTML
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(tempWriter, tempDoc, msHtml, msCss);
tempMsBytes = tempMs.ToArray();
}
}
tempDoc.Close();
return tempMsBytes;
}
后来恢复它作为字节数组我尝试从这个PDF文件中添加页面到空白页面。
3210当它试图从传递给函数的字节数组创建一个PdfReader时,它会中断。 “重建失败:未找到预告片;原始消息:未找到PDF startxref。”
我以前用过另一个库来处理PDF。我作为一个对象传递了2个PdfDocuments,并在循环中从一个到另一个添加了页面。它不支持Css,所以我不得不切换到ITextSharp。
我不太清楚PdfWriter和PdfCopy之间的区别。
但是,关闭文档后,MemoryStream变空。我无法从中创建字节数组 –
这可能是为什么人们使用'mainDocWriter.CloseStream = false;'我是Java开发人员,而且我很难理解为什么C#在关闭它后清空内存流。 C#有一些我无法完全理解的奇怪的奥秘。例如,这似乎工作:http:// stackoverflow。COM /问题/ 2815761 /创建PDF的功能于记忆,而不是-的物理文件 –