c# - 如何保存已编辑的 pdf?

标签 c# .net asp.net pdf itextsharp

我想保存编辑过的 pdf 模板。我该怎么做?

using (MemoryStream ms = new MemoryStream())  
{    
    PdfReader reader = new PdfReader("~/Content/Documents/Agreement.pdf");
    PdfStamper formFiller = new PdfStamper(reader, ms);
    AcroFields formFields = formFiller.AcroFields;
    formFields.SetField("Name", formData.Name);
    formFields.SetField("Location", formData.Address);
    formFields.SetField("Date", DateTime.Today.ToShortDateString());
    formFields.SetField("Email", formData.Email);
    formFiller.FormFlattening = true;
    formFiller.Close();
}

最佳答案

this example ,您只需要使用 FileStream 而不是 MemoryStream。

代码应该是这样的:

string newFile = @".\FormDocument_out.pdf";    
FileStream fs = new FileStream(newFile, FileMode.Create);    
try
{
        PdfReader reader = new PdfReader("~/Content/Documents/Agreement.pdf");
        PdfStamper formFiller = new PdfStamper(reader, fs);  
        AcroFields formFields = formFiller.AcroFields;
        formFields.SetField("Name", formData.Name);
        formFields.SetField("Location", formData.Address);
        formFields.SetField("Date", DateTime.Today.ToShortDateString());
        formFields.SetField("Email", formData.Email);
        formFiller.FormFlattening = true;
        formFiller.Close();
}
catch(Exception)
{
    throw;
}
finally
{
    fs.Close();
}

关于c# - 如何保存已编辑的 pdf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6219635/

相关文章:

ASP.NET MVC - session 为空

c# - 对象关系映射 - 插入更新存储过程

C# 作用域问题

c# - 如何在不在 wpf 中引入新引用的情况下从 View 模型 (.cs) 调用窗口 (.xaml.cs) 中的方法

c# - 在 Windows 窗体中为文本框、复选框等任何控件加载类似事件?

asp.net - Visual Studio 不要打开新的浏览器实例

c# - 在 Rest Wcf 服务中传递参数

c# - 识别视频帧大小

c# - 编译新的 .NET Core 项目意外依赖

asp.net - itextsharp SetFields 未设置