locked
create and open .pdf file in Iframe RRS feed

  • Question

  • User-1651858287 posted

    I am trying to create a file in iframe but having a problem.

    string Path = Server.MapPath("~/HOD");
     
     
                    
                    try
                    {
     
                        //if file exist delete
                        if (File.Exists(Path + "/" + strip + ".pdf"))
                        {
                            File.Delete(Path + "/" + strip + ".pdf");
     
                        }
     
                    
     
                        FileStream fs = new FileStream(Path + "/" + strip + ".pdf", FileMode.Create, FileAccess.Write, FileShare.None);
     
                        Rectangle rec2 = new Rectangle(PageSize.A4);
     
                       
     
     
                        Document doc = new Document(rec2, 10, 10, 10, 10);
     
                        doc.Add(new Paragraph("What is your view of the type of support and feedback you received from your Supervisor/Head of Section/Head of Department (with respect to the motivation, guidance and coaching provided during the assessment period)? ", FontFactory.GetFont("Arial", 8, Font.BOLD)));
                        doc.Add(new Paragraph("\n"));
     
                        
     
                          PdfWriter writer = PdfWriter.GetInstance(doc, fs);
     
     
                        
                        doc.Close();
                        writer.Close();
                        fs.Close();
     
                       
     
                    }
                    
                     catch
                    {
                        Response.Write("File could not be created. Please contact administrator");
                    }
     
                    string pa = Path + "/" + strip + ".pdf";
                    string path = ResolveClientUrl(pa);
     
                    //load file in frame
                  
                    Iframe1.Attributes["src"] = pa;
                    string encodedPath = System.Web.HttpUtility.JavaScriptStringEncode(path, true);
                
    Wednesday, June 12, 2019 12:20 PM

All replies

  • User288213138 posted

    Hi sweetSteal,

    I tried running your code, found some problems and modified it.

    If you want to create a pdf file, you first use the Document.open() method to open a document to be written, then create a Write instance for the Document.

    The iframe src should be a relative path, not an absolute path,because you can't directly access the files in the c: drive in WebApplicaiton.

    The code:

    protected void Page_Load(object sender, EventArgs e)
            {
                string strip = "name";
                string Path = Server.MapPath("~/HOD");
                try
                {
                    //if file exist delete
                    if (File.Exists(Path + "/" + strip + ".pdf"))
                    {
                        File.Delete(Path + "/" + strip + ".pdf");
                    }
                    FileStream fs = new FileStream(Path + "/" + strip + ".pdf", FileMode.Create, FileAccess.Write, FileShare.None);
    
                    Rectangle rec2 = new Rectangle(PageSize.A4);
                    Document doc = new Document(rec2, 10, 10, 10, 10);
                    PdfWriter writer = PdfWriter.GetInstance(doc, fs);
                    doc.Open();               
                    doc.Add(new Paragraph("What is your view of the type of support and feedback you received from your Supervisor/Head of Section/Head of Department (with respect to the motivation, guidance and coaching provided during the assessment period)? ", FontFactory.GetFont("Arial", 8, Font.BOLD)));
                    doc.Add(new Paragraph("\n"));               
                    doc.Close();
                    writer.Close();
                    fs.Close();
                    
                }            
                catch
                {
                   
                    Response.Write("File could not be created. Please contact administrator");
                }
                string pa = Path + "\\" + strip + ".pdf";
                string path = ResolveClientUrl(pa);
                //load file in frame
                Iframe1.Attributes["src"] = "/HOD/name.pdf";        
                string encodedPath = System.Web.HttpUtility.JavaScriptStringEncode(path, true);
            }
    

    The result:

    Best regards,

    Sam

    Thursday, June 13, 2019 8:38 AM
  • User-1038772411 posted

    Hi, SweetSteal

    <asp:Panel ID="Panel1" runat="server">
        <iframe src="UploadFiles/RR Form_MP, BCA(P)_MCA_1501.pdf" width="400px" height="300px"  />
        </asp:Panel>

    Or

    Use '@' at the start of your Path,and after that give your whole path;
    Like

    myframe.Attributes["src"] = @"Folder:\20120517110052.pdf";
    
    Friday, June 14, 2019 7:28 AM