iTextSharp.text.pdf 数据流方式写入附件
程序员文章站
2022-07-13 13:31:54
...
var filename = Snowflake.snowflake.GetId();
FileStream byteOut = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory + filename + "readonlypdf.pdf", FileMode.Create);
PdfReader reader = new PdfReader(fileData);
PdfStamper stamper = new PdfStamper(reader, byteOut);
//方法参数依次为:附件描述,附件流,附件文件(流和文件传一个就可以),附件名称
stamper.AddFileAttachment("", fileXMLData, null, "Evd.xml");//filexmlData
stamper.Close();
byteOut.Close();
using (FileStream fs = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory + filename + "readonlypdf.pdf", FileMode.Open, FileAccess.Read))
{
//获取文件大小
long size = fs.Length;
byte[] array = new byte[size];
//将文件读到byte数组中
fs.Read(array, 0, array.Length);
fs.Close();
fileStreamData.Add(array);
}
File.Delete(System.AppDomain.CurrentDomain.BaseDirectory + filename + "readonlypdf.pdf");