Export Doc to PDF Asp.net

拥有回忆 提交于 2019-12-25 03:09:03

问题


So I am using this code, to export a formview to Word. Its works great..But I want it to export to PDF so that it cannot be edited. Or may be to a word doc so that not body can make changes.

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition",
    "attachment;filename=Report.doc");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-word";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    FormView1.DataBind();
    FormView1.RenderControl(hw);
    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();
}

The problem is, even when I change the content type and header element in the above code, it says that the output pdf has errors.

I really want to either convert the doc to pdf or generate pdf using this code.

Please help.

Thanks..


回答1:


Your best bet to create PDFs in ASP.NET is to use a plug in like iTextSharp. I have used it in the past and it's very simple and free.

http://itextpdf.com/




回答2:


As mentioned above, creating PDF using one of the existing libraries would be more efficient.

But if you're down to use interop, you can download save as pdf plugin for Microsoft Office.
And then pass "pdf" format to SaveAs method

Alternatively, you can apply several properties to your word document:
1. Mark as Final doc.Final = true;
2. Restrict editing

For newer version of Word, there's a Protect method, that provides a convenient way of restricting editing: http://msdn.microsoft.com/en-us/library/ms178793.aspx



来源:https://stackoverflow.com/questions/17369539/export-doc-to-pdf-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!