我用这个组件做pdf操作, 因为要处理多少, 所以想用byte[] 一路操作下去,但是使用的适合发现无法读byte, 只能先写成文件到硬盘,再读出来进去下一步操作, 请问是哪里出了问题
方法如下public byte[] AddWaterMark(byte[] sourceFile, string waterContent)
{
//待加水印的文件
PdfReader reader = new PdfReader(sourceFile);
//加完水印的文件
MemoryStream fs = new MemoryStream();
PdfStamper stamper = new PdfStamper(reader, fs);
int total = reader.NumberOfPages + 1;
PdfContentByte content;
//设置字体
BaseFont bs = BaseFont.CreateFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
for (int i = 1; i < total; i++)
{
//在内容上方加水印
content = stamper.GetOverContent(i);
content.BeginText();
content.SetColorFill(BaseColor.BLACK);
content.SetFontAndSize(bs, 8);
content.ShowTextAligned(Element.ALIGN_CENTER, waterContent, 3
方法如下public byte[] AddWaterMark(byte[] sourceFile, string waterContent)
{
//待加水印的文件
PdfReader reader = new PdfReader(sourceFile);
//加完水印的文件
MemoryStream fs = new MemoryStream();
PdfStamper stamper = new PdfStamper(reader, fs);
int total = reader.NumberOfPages + 1;
PdfContentByte content;
//设置字体
BaseFont bs = BaseFont.CreateFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
for (int i = 1; i < total; i++)
{
//在内容上方加水印
content = stamper.GetOverContent(i);
content.BeginText();
content.SetColorFill(BaseColor.BLACK);
content.SetFontAndSize(bs, 8);
content.ShowTextAligned(Element.ALIGN_CENTER, waterContent, 3
content.EndText();
}
//fs 流读出
byte[] result = fs.GetBuffer();
stamper.Close();
fs.Close();
reader.Close();
return result;
}
我看了下itext的确不像spire那样有很明确的save方法,但你这个代码看起来没有执行最终绘制的方法,就像电子签名要通过MakeSignature.SignDetached这个方法来最终执行一样
无法直接返回stream , 进行其他的操作我需要对pdf文档进行生成,增加水印, 压缩成zip 等多种操作, 我想用stream 进行操作,现在只能生成文件后 ,又用filestream读出来, 太浪费资源了