【问题标题】:How to open a PDF file inside a webpage?如何在网页中打开 PDF 文件?
【发布时间】:2016-04-18 02:43:06
【问题描述】:

我需要让我的 MVC 网站访问者打开其中的 PDF 文件并从中提取一些信息。有没有办法做到这一点?

【问题讨论】:

    标签: c# asp.net-mvc pdf


    【解决方案1】:

    divPdf 成为必需的div 然后您可以使用embed 文件:

     <div id="divPdf">
           <embed src="fileName.pdf" />
     </div>
    

    【讨论】:

    • 这是下载 PDF,而不是在浏览器 DIV 中显示 PDF。
    【解决方案2】:
    public static void ConvertToSwf(string pdfPath, string swfPath, int page)
    {
        try
        {
            string exe =  HttpContext.Current.Server.MapPath("PDF2SWF/pdf2swf.exe");
            if (!File.Exists(exe))
            {
                throw new ApplicationException("Can not find: " + exe);
            }
    
            StringBuilder sb = new StringBuilder();
            sb.Append(" -o \"" + swfPath + "\"");//output
            sb.Append(" -z");
            sb.Append(" -s flashversion=9");//flash version
            sb.Append(" -s disablelinks");
            sb.Append(" -p " + "1" + "-" + page);//page range
            sb.Append(" -j 100");//Set quality of embedded jpeg pictures to quality. 0 is worst (small), 100 is best (big). (default:85)
            sb.Append(" \"" + pdfPath + "\"");//input
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = exe;
            proc.StartInfo.Arguments = sb.ToString();
            proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            proc.Start();
            proc.WaitForExit();
            proc.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    
    public static int GetPageCount(string pdfPath)
    {
        try
        {
            byte[] buffer = File.ReadAllBytes(pdfPath);
            int length = buffer.Length;
            if (buffer == null)
                return -1;
            if (buffer.Length <= 0)
                return -1;
            string pdfText = Encoding.Default.GetString(buffer);
            System.Text.RegularExpressions.Regex rx1 = new System.Text.RegularExpressions.Regex(@"/Type\s*/Page[^s]");
            System.Text.RegularExpressions.MatchCollection matches = rx1.Matches(pdfText);
            return matches.Count;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    

    http://www.swftools.org/

    PDF2Swf.exe 可以将【.pdf】转换为【.swf】,然后显示在您的页面中
    这可能是工作!

    【讨论】:

      【解决方案3】:

      使用这个

      <div>
          <iframe src="[Full path of your PDF file]" ></iframe>
      </div>
      

      【讨论】:

        猜你喜欢
        • 2010-11-15
        • 2022-01-25
        • 1970-01-01
        • 1970-01-01
        • 2021-03-22
        • 1970-01-01
        • 2017-01-20
        • 1970-01-01
        • 2020-04-21
        相关资源
        最近更新 更多