如何在 C# 中从外部站点的 URL 读取 PDF 文件 [已关闭]

问题描述 投票:0回答:1

我希望能够下载可通过我的 ASP.NET 应用程序中的路由 URL(外部站点)访问的 PDF 文件。

有办法吗?

现状:

路由的 URL 位于公司内部站点(Joomla站点),http://example/sites/index.php/2011-10-30-12-29-04/finish/11/1234。 此链接将用户重定向到 PDF 文件。 我需要使用路由 URL 在我的应用程序 (PdfReader) 中获取此 PDF 文件。


更新#1:

我按照您的建议对代码进行了一些更改(我将其添加到原始问题中),只需将内容流传递到我的 pdfReader 即可。但是,它仍然显示下载失败..


更新#2: 现在问题已经解决了。我必须按如下方式传递内容流

Dim pdfReader As New PdfReader(isp:=contentStream)

Public Async Function GetPDFFromCompanyWebsite() As Task(Of HttpResponseMessage)

    Using client As HttpClient = New HttpClient()

        Dim msg As HttpResponseMessage = Await client.GetAsync("http://example/sites/index.php/2011-10-30-12-29-04/finish/4/4088")

        If msg.IsSuccessStatusCode Then

            Dim contentStream = Await msg.Content.ReadAsStreamAsync()

            Dim pdfReader As New PdfReader(isp:=contentStream)
            Dim MST As MemoryStream = New MemoryStream()
            Dim pdfStamper As New PdfStamper(pdfReader, MST)
            For pageIndex As Integer = 1 To pdfReader.NumberOfPages
                Dim pageRectangle As Rectangle = pdfReader.GetPageSizeWithRotation(pageIndex)
                Dim pdfData As PdfContentByte = pdfStamper.GetOverContent(pageIndex)

                pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 40)
                Dim graphicsState As New PdfGState()

                graphicsState.FillOpacity = 0.1F
                pdfData.SetGState(graphicsState)

                pdfData.SetColorFill(BaseColor.BLUE)

                Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)

                pdfData.SetFontAndSize(bf, pageRectangle.Width / 25)
                pdfData.BeginText()
                pdfData.SetFlatness(1000)

                Dim windowsuser As String = User.Identity.Name.Substring(4)
                windowsuser = windowsuser + "        " + windowsuser + "        " + windowsuser + "        " + windowsuser + "        " + windowsuser + "        " + windowsuser + "        " + windowsuser
                '    pdfData.ShowTextAligned(Element.ALIGN_BOTTOM, User.Identity.Name, 100, 100, 45)
                Response.Write("width:height: " + pageRectangle.Width.ToString + " / " + pageRectangle.Height.ToString)
                pdfData.ShowTextAligned(Element.ALIGN_CENTER, windowsuser, pageRectangle.Width / 2, pageRectangle.Height / 2, 45)
                pdfData.ShowTextAligned(Element.ALIGN_CENTER, windowsuser, pageRectangle.Width / 4, pageRectangle.Height - (pageRectangle.Height / 4), 45)
                pdfData.ShowTextAligned(Element.ALIGN_CENTER, windowsuser, pageRectangle.Width - (pageRectangle.Width / 4), pageRectangle.Height / 4, 45)
                pdfData.ShowTextAligned(Element.ALIGN_CENTER, windowsuser, (3 * pageRectangle.Width) / 8, pageRectangle.Height - ((3 * pageRectangle.Height) / 8), 45)
                pdfData.ShowTextAligned(Element.ALIGN_CENTER, windowsuser, pageRectangle.Width - ((3 * pageRectangle.Width) / 8), (3 * pageRectangle.Height) / 8, 45)

                pdfData.EndText()
            Next

            pdfStamper.Close()
            Dim bytesInStream As Byte() = MST.ToArray()

            MST.Close()

            Response.Clear()
            Response.ClearContent()
            Response.ClearHeaders()
            Response.ContentType = "application/pdf"
            Response.AddHeader("content-disposition", "attachment;filename=File.pdf")
            Response.BufferOutput = True
            Response.Cache.SetCacheability(HttpCacheability.NoCache)
            Response.BinaryWrite(bytesInStream)
            Response.End()
            Response.Close()

            '        End Using
        End If

        Return msg
    End Using
End Function
c# asp.net url-routing pdf-reader
1个回答
4
投票

尝试使用 C# 中的 HttpClient 类向您公司的网站发出 GET 请求。你可以按照这个思路做一些事情。

using System.Net.Http;
using System.IO;

public async Task<HttpResponseMessage> GetPDFFromCompanyWebsite()
{
string currentDirectory = System.Web.Hosting.HostingEnvironment.MapPath("~");
string filePath = Path.Combine(currentDirectory, "App_Data", "someDocument.pdf");

using(HttpClient client = new HttpClient())
{
    HttpResponseMessage msg = await client.GetAsync($"http://example/sites/index.php/2011-10-30-12-29-04/finish/11/1234");

    if(msg.IsSuccessStatusCode)
    {
     using(var file = File.Create(filePath))
     { 
       // create a new file to write to
       var contentStream = await msg.Content.ReadAsStreamAsync(); // get the actual content stream
       await contentStream.CopyToAsync(file); // copy that stream to the file stream
       await file.FlushAsync(); // flush back to disk before disposing
     }
   }
  return msg;
} }
© www.soinside.com 2019 - 2024. All rights reserved.