1

How to make a pdf file has function button?

This the code behind of PDF

 public partial class Pdf_Default : System.Web.UI.Page
{
protected override void Render(HtmlTextWriter writer)
{
    MemoryStream mem = new MemoryStream();
    StreamWriter twr = new StreamWriter(mem);
    HtmlTextWriter myWriter = new HtmlTextWriter(twr);
    base.Render(myWriter);
    myWriter.Flush();
    myWriter.Dispose();
    StreamReader strmRdr = new StreamReader(mem);
    strmRdr.BaseStream.Position = 0;
    string pageContent = strmRdr.ReadToEnd();
    strmRdr.Dispose();
    mem.Dispose();
    writer.Write(pageContent);
    CreatePDFDocument(pageContent);


}
public void CreatePDFDocument(string strHtml)
{

    string strFileName = HttpContext.Current.Server.MapPath("test.pdf");
    // step 1: creation of a document-object
    Document document = new Document();
    // step 2:
    // we create a writer that listens to the document
    PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
    StringReader se = new StringReader(strHtml);
    HTMLWorker obj = new HTMLWorker(document);
    document.Open();
    obj.Parse(se);
    document.Close();
    ShowPdf(strFileName);



}
public void ShowPdf(string strFileName)
{
    Response.ClearContent();
    Response.ClearHeaders();
    Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
    Response.ContentType = "application/pdf";
    Response.WriteFile(strFileName);
    Response.Flush();
    Response.Clear();
}

}

When I tried to put a Back Button, the PDF file shows only Label Test. I want to make a button that can go to another page (Non PDF). Thanks!

Clarify Seeker
  • 117
  • 1
  • 8

1 Answers1

1

When you send a PDF file as response using Response.ContentType = "application/pdf"; etc, many things may happen, the most common are:
1- The user who is navigating on your site has NOT a default PDF viewer and a "Save As..." dialog will show up (usually).
2- The user has a default PDF viewer, that shows up either as an external application outside the browser or integrated inside it.

On any of those cases, you will just show the PDF file, without any HTML content involved whatsoever. If you want to show a button that takes the user back to your site, you will have to modify the PDF file and add the button inside it.

Another choice is to embed the pdf file inside a page instead of sending it as response. In such case you can add the button to the page that is wrapping the PDF file.

Community
  • 1
  • 1
yms
  • 10,361
  • 3
  • 38
  • 68