如何在c#中创建pdf [英] how to create pdf in c#

查看:79
本文介绍了如何在c#中创建pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,每个人

我想用c#创建pdf,我写了代码:



Hi, everybody
I want to create pdf using c# and I wrote that code :

private void btnCreate_Click(object sender, EventArgs e)
       {
           Document doc = new Document(iTextSharp.text.PageSize.LETTER,10,10,42,35);
           PdfWriter pwrt = PdfWriter.GetInstance(doc, new
                                      FileStream("test.pdf",FileMode.Create));
           //open docement
           doc.Open();
           //write some comment in pdf
           Paragraph prg = new Paragraph("I love you");
           //Add paragraph to document
           doc.Add(prg);
           //close document
           doc.Close();
       }





但该代码无效。请帮助我。



But that code can not work. Please help me.

推荐答案

根据您的评论,正在创建文件,但是在 bin \Debug 项目下的文件夹。您希望在特定路径中创建文件。



问题是您只在<中指定了相对路径code> FileStream 构造函数:

Based on your comments, the file is being created, but in the bin\Debug folder under your project. You want the file to be created in a specific path.

The problem is that you've only specified a relative path in the FileStream constructor:
new FileStream("test.pdf",FileMode.Create)



此路径将相对于当前路径进行解析,该路径通常与运行应用程序的路径相同。换句话说,项目下的 bin \Debug 路径。



要保存到特定路径,你需要将完整路径传递给 FileStream 构造函数:


This path will be resolved relative to the current path, which is usually the same as the path from which the application is running. In other words, the bin\Debug path under your project.

To save to a specific path, you'll need to pass the full path to the FileStream constructor:

new FileStream(@"C:\Your\Path\Here\test.pdf",FileMode.Create)





注意:小心试图保存记录到 C:驱动器的根目录;大多数应用程序无权写入该目录。确保选择了应用程序具有写权限的路径。



NB: Be careful trying to save the document to the root of your C: drive; most applications won't have permission to write to that directory. Make sure you chose a path to which your application has write permissions.


Refer - 以6个步骤创建PDF文档: [ ^ ]。



It说...

Refer - Creating PDF Document in 6 Steps:[^].

It says...
Quote:

FileStream fs = new FileStream("Chapter1_Example1.pdf", FileMode.Create, FileAccess.Write, FileShare.None);



但是您只在创建模式下初始化。我希望它能解决这个问题。否则,请告诉我。


But you have initialized only in Create mode. I hope it solves the issue. Otherwise, let me know.


这篇关于如何在c#中创建pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆