更新时间:2023-09-13 18:22:16
只是一个狂野而疯狂的猜测,但我想你之所以如此创建新文件就是这一行。
Just a wild and crazy guess, but I think the reason why you are always creating new files is this line.
PdfWriter.GetInstance(doc, new FileStream(pdfpath, FileMode.Create));
您正在使用FileMode.Create ...您应该将其更改为FileMode.OpenOrCreate或FileMode.Open。
You are using FileMode.Create...you should probably change that to FileMode.OpenOrCreate or FileMode.Open.
请试试这个
Please try this
using (Stream inputPdfStream = new FileStream(Server.MapPath("~") + "/pdf/test.pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream inputImageStream = new FileStream(Server.MapPath("~") + "/logo/test.jpg", FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream outputPdfStream = new FileStream(Server.MapPath("~") + "/pdf/Result.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
{
var reader = new PdfReader(inputPdfStream);
var stamper = new PdfStamper(reader, outputPdfStream);
var pdfContentByte = stamper.GetOverContent(1);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImageStream);
image.SetAbsolutePosition(100, 100);
pdfContentByte.AddImage(image);
stamper.Close();
}