简体   繁体   English

PdfSharp:从指定的文件路径和名称获取 PdfDocument

[英]PdfSharp: Get PdfDocument from specified file path and name

I am using pdfsharp in a .net application and am trying to open a pdf from a specified path and file name.我在 .net 应用程序中使用 pdfsharp,并试图从指定的路径和文件名打开 pdf。 However, when I try this:但是,当我尝试这个时:

PdfDocument doc = PdfReader.Open(path, PdfDocumentOpenMode.Import);

Where path is the filepath and name, it is appended to the path for my project's web folder.其中path是文件路径和名称,它附加到我项目的 web 文件夹的路径中。 For example, if my path is https:\site.net\files\thisfile.pdf , it will search for C:\Users\thisuser\Proj\ProjWeb\https:\site.net\files\thisfile.pdf instead. For example, if my path is https:\site.net\files\thisfile.pdf , it will search for C:\Users\thisuser\Proj\ProjWeb\https:\site.net\files\thisfile.pdf instead.

How can I get a PdfDocument using only the path and file name I have specified, without having this additional path being appended to it?如何仅使用我指定的路径和文件名获取PdfDocument ,而不附加此附加路径?

The solution must account for multiple filepaths as the value of path is based on other conditions.该解决方案必须考虑多个文件路径,因为path的值基于其他条件。

You are trying to load file from URL, while Pdfreader.Open only supports loading files locally or from a stream.您正在尝试从 URL 加载文件,而 Pdfreader.Open 仅支持在本地或从 stream 加载文件。

   HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(path);
   Stream pdfstream = req.GetResponse().GetResponseStream();

   PdfDocument doc = PdfReader.Open(pdfstream, PdfDocumentOpenMode.Import);

The above code will try to load the PDF from a remote url into Stream, then open the PDF from said stream. The above code will try to load the PDF from a remote url into Stream, then open the PDF from said stream. I haven't tested this, but according to this ( where the code was obtained ) https://forum.pdfsharp.net/viewtopic.php?f=2&t=2030 , you might have to use MemoryStream in place of Stream. I haven't tested this, but according to this ( where the code was obtained ) https://forum.pdfsharp.net/viewtopic.php?f=2&t=2030 , you might have to use MemoryStream in place of Stream.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM