0

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

Garrison Neely
  • 3,238
  • 3
  • 27
  • 39
Prashant
  • 15
  • 7

2 Answers2

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/

Garrison Neely
  • 3,238
  • 3
  • 27
  • 39
  • Further you can create an HTTP Handler that can stream the PDF to the browser as an attachment that allows the user to view and/or save the PDF. I use this concept all the time in application I work on. Here is a blog entry on how to [generate simple PDF reports using ASP.NET HttpHandler](http://kuujinbo.info/cs/itext_reports.aspx) – Karl Anderson Jun 28 '13 at 16:49
0

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

RAS
  • 3,375
  • 15
  • 24