如何在c#中创建pdf

编程入门 行业动态 更新时间:2024-10-27 12:42:05
本文介绍了如何在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

本文发布于:2023-11-03 06:56:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554501.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   pdf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!