0

目标是上传 .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.");
            }




        }
    }
}
4

1 回答 1

1

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

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

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

希望有帮助。

于 2013-06-14T17:57:32.817 回答