直接通过代码打开生成的pdf文件,无需将其保存到磁盘上
我使用 Sharepoint 2010,正在开发一个 Web 部件,在按钮单击事件中,需要生成并直接打开 pdf 文件。不应保存到磁盘上。 我尝试了下面的代码
protected void Button1_OnClick(object sender, EventArgs e)
{
Document myDoc = new Document(PageSize.A4.Rotate());
try
{
PdfWriter.GetInstance(myDoc, new FileStream(@"C:\Directory\Test.pdf", FileMode.Create));
myDoc.Open();
myDoc.Add(new Paragraph("Hello World"));
}
catch (DocumentException ex)
{
Console.Error.WriteLine(ex.Message);
}
myDoc.Close();
}
我也尝试了下面的代码,它也在服务器上生成了我不想要的文件。
Document document = new Document(PageSize.A4);
PdfWriter.GetInstance(document, new FileStream(HttpContext.Current.Server.MapPath("~/Test.pdf"), FileMode.Create));
document.Open();
var WelcomePara = new Paragraph("Hello World");
document.Add(WelcomePara);
document.Close();
这个在桌面上创建了pdf文件,我需要以pdf格式打开它。有人可以帮助我吗?
I use Sharepoint 2010 and I am developing a web part where on a button click event, a pdf file needs to be generated and opened directly. Should not be saving onto the disk.
I tried the below code
protected void Button1_OnClick(object sender, EventArgs e)
{
Document myDoc = new Document(PageSize.A4.Rotate());
try
{
PdfWriter.GetInstance(myDoc, new FileStream(@"C:\Directory\Test.pdf", FileMode.Create));
myDoc.Open();
myDoc.Add(new Paragraph("Hello World"));
}
catch (DocumentException ex)
{
Console.Error.WriteLine(ex.Message);
}
myDoc.Close();
}
I also tried the below code which also generates the file on the Server which I dont want.
Document document = new Document(PageSize.A4);
PdfWriter.GetInstance(document, new FileStream(HttpContext.Current.Server.MapPath("~/Test.pdf"), FileMode.Create));
document.Open();
var WelcomePara = new Paragraph("Hello World");
document.Add(WelcomePara);
document.Close();
This one creates the pdf file on the desktop, I need it to be opened in the pdf format.Can someone help me please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
几乎每次接受
FileStream
的东西实际上确实接受了一个通用的System.IO.Stream
对象,该对象< code>FileStream 是 的子类。这意味着您可以使用它的表兄弟System.IO.MemoryStream
这就是您正在寻找的:如果您不想,则不必创建字节数组,我只是在展示如何创建 PDF 并为您提供“类似 .net”的内容供您使用。
Almost every time that something accepts a
FileStream
is actually really accepts a genericSystem.IO.Stream
object whichFileStream
is a subclass of. This means that you can instead use its cousinSystem.IO.MemoryStream
which is what you are looking for:You don't have to create the byte array if you don't want, I was just showing how to create a PDF and give you something ".net-like" for you to work with.
我终于能够让它工作了。
I was able to get it work finally.
这对我有用。
This Works for me.