Prashant
Prashant

Reputation: 15

Export Doc to PDF Asp.net

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..

Upvotes: 0

Views: 1317

Answers (2)

RAS
RAS

Reputation: 3385

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

Upvotes: 0

Garrison Neely
Garrison Neely

Reputation: 3289

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/

Upvotes: 1

Related Questions