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

时间:2013-06-14 17:49:38

标签: c# asp.net .net web-applications

目标是上传.ppt,将其转换为.pdf,然后将其显示给用户。

现在我可以上传和转换就好了。如果我在代码隐藏中取消注释这两行,则会显示全屏显示。

有什么方法可以在'asp:Image'或其他非全屏事物中显示.pdf吗?

我的前方是:

<%@ 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>

我的代码隐藏是这个

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.");
            }




        }
    }
}

1 个答案:

答案 0 :(得分:1)

也许尝试使用和iframe来显示pdf?

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

我从未尝试过,但我知道iframe非常好。

希望有所帮助。