C#中怎么導(dǎo)出pdf

C#中怎么導(dǎo)出pdf,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)錫山免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了1000多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。

一.接口部分的代碼

[HttpGet]    public HttpResponseMessage ExportPdf(string id)    {      string pdfName = "";       //id 查詢條件,根據(jù)實際情況修改即可       //pdfName 例如download.pdf      byte[] pdfData= _policyGapManagerService.ExportPdf(id, out pdfName);//獲得pdf字節(jié)      var result = new HttpResponseMessage(HttpStatusCode.OK)      {        Content = new ByteArrayContent(pdfData)      };      result.Content.Headers.ContentDisposition =        new ContentDispositionHeaderValue("attachment")        {          FileName = pdfName        };      result.Content.Headers.ContentType =new MediaTypeHeaderValue("application/pdf");      return result;    }

二.返回pdfbyte數(shù)組

1.下載http模式的pdf文件(以ASP.NET為例,將PDF存在項目的目錄下,可以通過http直接打開項目下的pdf文件)

#region 調(diào)用本地文件使用返回pdfbyte數(shù)組    /// <summary>    /// 調(diào)用本地文件使用返回pdfbyte數(shù)組    /// </summary>    /// <param name="srcPdfFile">‘D:\in2434341555551.pdf'</param>    /// <returns></returns>    public static byte[] GetSignaturePDFByte(string srcPdfFile)    {      using (FileStream fsRead = new FileStream(srcPdfFile, FileMode.Open, FileAccess.Read, FileShare.Read))      {        int fsLen = (int)fsRead.Length;        byte[] hebyte = new byte[fsLen];        fsRead.Read(hebyte, 0, hebyte.Length);        return hebyte;      }    }    #endregion 調(diào)用本地文件使用返回pdfbyte數(shù)組    #region 從網(wǎng)站上下載pdf,轉(zhuǎn)化為字節(jié)流    /// <summary>    /// 從網(wǎng)站上下載pdf,轉(zhuǎn)化為字節(jié)流    /// </summary>    /// <param name="srcPdfFile">文件地址:'https://******/group2/M00/00/04/wKj-mlpcoZ2IUbK5AACrpaV6k98AAAB6gAAAAAAAKu9562.pdf'</param>    /// <returns></returns>    public static Byte[] GetByteByRemoteURL(string srcPdfFile)    {      byte[] arraryByte;      HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(srcPdfFile);      req.Method = "GET";      using (WebResponse wr = req.GetResponse())      {        StreamReader responseStream = new StreamReader(wr.GetResponseStream(), Encoding.UTF8);        int length = (int)wr.ContentLength;        byte[] bs = new byte[length];        HttpWebResponse response = wr as HttpWebResponse;        Stream stream = response.GetResponseStream();        //讀取到內(nèi)存        MemoryStream stmMemory = new MemoryStream();        byte[] buffer1 = new byte[length];        int i;        //將字節(jié)逐個放入到Byte 中        while ((i = stream.Read(buffer1, 0, buffer1.Length)) > 0)        {          stmMemory.Write(buffer1, 0, i);        }        arraryByte = stmMemory.ToArray();        stmMemory.Close();      }      return arraryByte;    }    #endregion 從網(wǎng)站上下載pdf,轉(zhuǎn)化為字節(jié)流    #region 從網(wǎng)站上下載文件,保存到其他路徑    /// <summary>    /// 從網(wǎng)站上下載文件,保存到其他路徑    /// </summary>    /// <param name="pdfFile">文件地址</param>    /// <param name="saveLoadFile">保存文件路徑:D:\12221.pdf</param>    /// <returns></returns>    public string SaveRemoteFile( string saveLoadFile , string pdfFile)    {      //bool flag = false;      var f = saveLoadFile + Guid.NewGuid().ToString("D") + ".pdf";      Uri downUri = new Uri(pdfFile);      //建立一個WEB請求,返回HttpWebRequest對象      HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(downUri);      //流對象使用完后自動關(guān)閉      using (Stream stream = hwr.GetResponse().GetResponseStream())      {        //文件流,流信息讀到文件流中,讀完關(guān)閉        using (FileStream fs = File.Create(f))        {          //建立字節(jié)組,并設(shè)置它的大小是多少字節(jié)          byte[] bytes = new byte[102400];          int n = 1;          while (n > 0)          {            //一次從流中讀多少字節(jié),并把值賦給N,當(dāng)讀完后,N為0,并退出循環(huán)            n = stream.Read(bytes, 0, 10240);            fs.Write(bytes, 0, n); //將指定字節(jié)的流信息寫入文件流中          }        }      }      //return flag;      //return _outPath + saveLoadFile;      return f;    }    #endregion 從網(wǎng)站上下載文件,保存到其他路徑

2.ftp模式的pdf文件

/// <summary>    /// 下載FTP文件。    /// </summary>    /// <param name="offsetPath">相對路徑</param>    /// <param name="fileName">文件名稱</param>    /// <returns>下載結(jié)果,本地文件路徑</returns>    public string DownLoad(string offsetPath,string fileName)    {      try      {        FtpWebRequest ftpWeb = (FtpWebRequest)WebRequest.Create(_ftpRootPath + offsetPath + fileName);        ftpWeb.Method = WebRequestMethods.Ftp.DownloadFile;        ftpWeb.UseBinary = true;        var resp = ftpWeb.GetResponse();        using (FileStream fs = new FileStream(_outPath + fileName, FileMode.Create))        {          using (var s = resp.GetResponseStream())          {            if (s == null) { return "文件不存在!"; }            int readCout = 0;            byte[] bytes = new byte[1024];            readCout = s.Read(bytes, 0, 1024);            while (readCout > 0)            {              fs.Write(bytes, 0, readCout);              readCout = s.Read(bytes, 0, 1024);            }          }        }        resp.Close();        return _outPath + fileName;      }      catch (Exception e)      {        return e.Message;      }          }    /// <summary>    /// 判斷文件是否存在    /// </summary>    /// <param name="offsetPath"></param>    /// <param name="fileName"></param>    /// <returns></returns>    public bool FileExists(string offsetPath, string fileName)    {      try      {        FtpWebRequest ftpWeb = (FtpWebRequest)WebRequest.Create(_ftpRootPath + offsetPath + fileName);        ftpWeb.Method = WebRequestMethods.Ftp.DownloadFile;        ftpWeb.UseBinary = true;        var resp = (FtpWebResponse)ftpWeb.GetResponse();        resp.Close();        return true;      }      catch (Exception)      {        return false;      }    }    /// <summary>    /// 獲取目錄下所有文件    /// </summary>    /// <returns></returns>    public string[] Files(string offsetPath)    {      try      {        FtpWebRequest ftpWeb = (FtpWebRequest)WebRequest.Create(_ftpRootPath + offsetPath);        ftpWeb.Method = WebRequestMethods.Ftp.ListDirectory;        Stream stream = ftpWeb.GetResponse().GetResponseStream();        if (stream == null)        {          return null;        }        List<string> fileList = new List<string>();        using (StreamReader sr = new StreamReader(stream))        {          StringBuilder sb = new StringBuilder();          do          {            sb.Append(sr.ReadLine());            if (sb.Length > 0)            {              fileList.Add(sb.ToString());              sb.Clear();            }            else            {              break;            }          } while (true);        }        return fileList.ToArray();      }      catch (Exception)      {         return null;      }    }

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

網(wǎng)站題目:C#中怎么導(dǎo)出pdf
轉(zhuǎn)載來源:http://bm7419.com/article20/geicco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、虛擬主機、網(wǎng)站收錄、關(guān)鍵詞優(yōu)化、網(wǎng)站排名App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作