简体   繁体   English

在asp.net的图像框中显示.pdf

[英]Display .pdf in image box in asp.net

The goal is to upload a .ppt, convert it to a .pdf, and then show that to the user. 目标是上载.ppt,将其转换为.pdf,然后将其显示给用户。

Right now I can upload and convert just fine. 现在,我可以上传和转换了。 If I uncomment those two lines in my code-behind, it shows a full-screen display. 如果我在后面的代码中取消注释这两行,它将显示全屏显示。

Is there any way I can display the .pdf inside an 'asp:Image' or some other non-fullscreen thing? 有什么办法可以在“ asp:Image”或其他非全屏显示内容中显示.pdf?

My front is this: 我的面前是这样的:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs"  Inherits="WebApplication1._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">


            <input type="file" id=File1 name=File1 runat="server" />
            <asp:Button id="b1" Text="Upload" OnCLick="DoUpload" runat="server" />

           <asp:Image ID="img" runat="server"  AlternateText="" Width="400" Height="400" />


   </asp:Content>

My code-behind is this 我的后台代码是这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
namespace WebApplication1
{

    public partial class _Default : System.Web.UI.Page
    {
        protected System.Web.UI.HtmlControls.HtmlInputFile File1;
        protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;


        protected void Page_Load(object sender, EventArgs e)
        {

        }



        protected void DoUpload(object sender, EventArgs e)
        {

            if((File1.PostedFile!=null)&&(File1.PostedFile.ContentLength>0))
            {
                string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
                string pdfn = fn.Remove(fn.Length - 3) + "pdf";
                string SaveLocation = Server.MapPath("Data")+"\\"+fn;
                string ShortLocation = Server.MapPath("Data")+"\\";
                string PdfLocation = Server.MapPath("Data") + "\\" + pdfn;
                try
                {
                    File1.PostedFile.SaveAs(SaveLocation);
                    Response.Write("The file has been uploaded. ||");

                    Response.Write(" " + SaveLocation + " " + ShortLocation);
                    Process p = new Process();
                    p.StartInfo.FileName = "cmd.exe";
                    p.StartInfo.Arguments = "soffice --headless --invisible -convert-to pdf "+fn;
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.Start();
                    p.WaitForExit();
                    Response.Write(" || File converted");
                    Response.Clear();

                    string filePath = PdfLocation;

                    //Response.ContentType = "application/pdf";
                    //Response.WriteFile(filePath);

                    img.ImageUrl = filePath;

                }
                catch(Exception ex)
                {
                    Response.Write("Error: " + ex.Message);
                }
            }
            else
            {
                Response.Write("Please select a file to upload.");
            }




        }
    }
}

Maybe try using and iframe to display the pdf? 也许尝试使用和iframe显示pdf?

<iframe src="*source here*"></iframe>

I've never tried it, but I know iframes are pretty nice. 我从未尝试过,但是我知道iframe非常不错。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM