C# 生成并保存 PDF 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9394945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate and save a PDF file
提问by hmk
I am using editable pdf files (created by Nitro PDF Software) in my application. These pdf files have a lot of editable fields (like textboxes) and one button (like submit).
我在我的应用程序中使用可编辑的 pdf 文件(由Nitro PDF Software创建)。这些 pdf 文件有很多可编辑的字段(如文本框)和一个按钮(如提交)。
Whenever a user opens that pdf file, enters the text and clicks the submit button they get redirected to an aspx page.
每当用户打开该 pdf 文件,输入文本并单击提交按钮时,他们就会被重定向到一个 aspx 页面。
How do I get all the static and dynamic values that are posted to this page, and create another pdf file with the entered data? And how do I save the created pdf file?
如何获取发布到此页面的所有静态和动态值,并使用输入的数据创建另一个 pdf 文件?以及如何保存创建的 pdf 文件?
采纳答案by yms
How do I get all the static and dynamic values that are posted to this page?
如何获取发布到此页面的所有静态和动态值?
You can retreive them as you retrieve any other value from an html control, for example:
您可以在从 html 控件检索任何其他值时检索它们,例如:
string MyVal = Request.Form["FieldName"];
and create another pdf file with the entered data?
并使用输入的数据创建另一个 pdf 文件?
Here you can use a PDF library of your choice ( iText.Net, Nitro PDF, Amyuni PDF Creator.Net), load your PDF form, set the values to each field, flatten your file if needed, and save. The code for this part depends on the library being used, but they are usally well documented so you should be able to find sample code easily.
在这里,您可以使用您选择的 PDF 库(iText.Net、Nitro PDF、Amyuni PDF Creator.Net)、加载您的 PDF 表单、为每个字段设置值、根据需要展平您的文件并保存。这部分的代码取决于所使用的库,但它们通常都有很好的文档记录,因此您应该能够轻松找到示例代码。
回答by PraveenVenu
Why dont you do it the way mentioned in the below link.
为什么不按照以下链接中提到的方式进行操作。
http://www.gnostice.com/nl_article.asp?id=176&t=Generate_PDF_Forms_In_ASP_NET_Using_PDFOne_NET_v3
http://www.gnostice.com/nl_article.asp?id=176&t=Generate_PDF_Forms_In_ASP_NET_Using_PDFOne_NET_v3
It uses another PDF generation tool. But you should be able to do the same with nitro.
它使用另一个 PDF 生成工具。但是你应该可以用硝基做同样的事情。
回答by Denis Besic
Try open source library http://pdfsharp.codeplex.com/, sample can be found here http://www.pdfsharp.net/wiki/.
尝试开源库http://pdfsharp.codeplex.com/,示例可以在这里找到http://www.pdfsharp.net/wiki/。
回答by ankit rajput
User following code for Generate PDF in asp.net:
用户使用以下代码在 asp.net 中生成 PDF:
There's a full code sample below to get you started. // Code
下面有一个完整的代码示例,可以帮助您入门。// 代码
using System;
using System.IO;
using System.Diagnostics;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class iTextDemo
{
public static void Main()
{
Console.WriteLine("iText Demo");
// step 1: creation of a document-object
Document myDocument = new Document(PageSize.A4.Rotate());
try
{
// step 2:
// Now create a writer that listens to this doucment and writes the document to desired Stream.
PdfWriter.GetInstance(myDocument, new FileStream("Salman.pdf", FileMode.Create));
// step 3: Open the document now using
myDocument.Open();
// step 4: Now add some contents to the document
myDocument.Add(new Paragraph("First Pdf File made by Salman using iText"));
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: Remember to close the documnet
myDocument.Close();
}
}
回答by ChrLipp
From Wikipedia, PDF Interactive elementsthere are two possibilities for integrating data and PDFs (there are also links to the specifications):
从维基百科,PDF 交互元素有两种可能来集成数据和 PDF(也有规范的链接):
- AcroForms (also known as Acrobat forms), introduced in the PDF 1.2 format specification and included in all later PDF specifications.
- Adobe XML Forms Architecture (XFA) forms, introduced in the PDF 1.5 format specification. The XFA specification is not included in the PDF specification, it is only referenced as an optional feature. Adobe XFA Forms are not compatible with AcroForms.
- AcroForms(也称为 Acrobat 表单),在 PDF 1.2 格式规范中引入并包含在所有后来的 PDF 规范中。
- Adobe XML Forms Architecture (XFA) 表单,在 PDF 1.5 格式规范中引入。XFA 规范不包含在 PDF 规范中,它仅作为可选功能被引用。Adobe XFA 表单与 AcroForms 不兼容。
For compatibility issues I would go for AcroForms. In that case I would use XFDF, because it is XML and therefore easier to parse. I never used Nitro, but when you build a PDF form you usually provide a "Save" button and choose at action for this button "Send/Post form to server" with the data format XML which is just XFDF.
对于兼容性问题,我会选择 AcroForms。在那种情况下,我会使用 XFDF,因为它是 XML,因此更容易解析。我从未使用过 Nitro,但是当您构建 PDF 表单时,您通常会提供一个“保存”按钮并在此按钮的操作中选择“向服务器发送/发布表单”,数据格式为 XML,即 XFDF。
This works only when the PDF is viewed in the browser. So the typical use case is: have an empty PDF template on the server, before returning the PDF to the user mix your data into the PDF, the user enters data in the form (PDF is opened in the browser via a plugin or natively in Chrome), then the user presses the save buttons which posts a xml on the server. The next time the user asks for his PDF, you took the recent data and mix it again with the template.
这仅在浏览器中查看 PDF 时才有效。所以典型的用例是:在服务器上有一个空的 PDF 模板,在将 PDF 返回给用户之前,将您的数据混合到 PDF 中,用户在表单中输入数据(PDF 通过插件在浏览器中打开或在本地打开Chrome),然后用户按下在服务器上发布 xml 的保存按钮。下次用户要求他的 PDF 时,您获取最近的数据并将其与模板再次混合。
So only two questions are open:
所以只有两个问题是开放的:
- how to generate a XFDF:
very easy, see http://wiki.developerforce.com/page/Adobe_XFDFor Parsing XML using XDocumentfor an example of the file structure - how to mix a XFDF with the PDF: This can be done with itext, there are several examples on stackoverflow, eg. https://stackoverflow.com/a/6274311/734687
- 如何生成 XFDF:
非常简单,请参阅http://wiki.developerforce.com/page/Adobe_XFDF或使用 XDocument 解析 XML 以获取文件结构示例 - 如何将 XFDF 与 PDF 混合:这可以通过itext完成,stackoverflow 上有几个示例,例如。https://stackoverflow.com/a/6274311/734687
See the complete process here: http://itextpdf.com/book/chapter.php?id=9. This example updates the PDF with the form dynamically at run time. Since iText is used there is no difference between Java and C#.
在此处查看完整过程:http: //itextpdf.com/book/chapter.php?id=9。本示例在运行时使用表单动态更新 PDF。由于使用了 iText,因此 Java 和 C# 之间没有区别。
Be aware that previous versions of iText (Java up to 2.1.7 and C# up to 4.1.6) were distributed under the Mozilla Public License or the LGPL, while current versions are distributed under the Affero General Public License. Thats explains why the older versions are still used.
请注意,以前版本的 iText(Java 最高 2.1.7 和 C# 最高 4.1.6)是在 Mozilla 公共许可证或 LGPL 下分发的,而当前版本是在 Affero 通用公共许可证下分发的。这就是为什么仍然使用旧版本的原因。
回答by Vigna
To generate PDF, use following code:
要生成 PDF,请使用以下代码:
NameSpace:
命名空间:
Using System.IO;
Using.iTextSharp.text;
Using.iTextSharp.text.pdf;
On button click:
点击按钮:
private void button1_Click(object sender, EventArgs e)
{
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(@"C:\Test.pdf", FileMode.Create));
doc.Open();
Paragraph paragraph = new Paragraph("Hi, \n This is xxx from yyy \n This is my pdf file");
doc.Add(paragraph);
doc.Close();
}
回答by Pandurang Chopade
Download itextSharp dll file,use namespace
下载itextSharp dll文件,使用命名空间
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text.pdf.draw;
Document document = new Document(PageSize.A4,43,43, 43, 43);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"D:\abc\text.pdf", FileMode.Create));
PdfPCell cell = null;
PdfPTable table = null;
document.Open();
Chunk glue = new Chunk(new VerticalPositionMark());
Paragraph para = new Paragraph();
table = new PdfPTable(1);
table.TotalWidth = 340f;
table.LockedWidth = true;
table.SpacingBefore = 20f;
table.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(PhraseCell(new Phrase("SCHEME INSTALLMENT RECEIPT ", FontFactory.GetFont("Arial", 12,1)), PdfPCell.ALIGN_CENTER));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 1;
cell.PaddingBottom = 10f;
table.AddCell(cell);
document.Add(table);
Phrase ph1 = new Phrase();
Paragraph mm = new Paragraph();
ph1.Add(new Chunk(Environment.NewLine));
ph1.Add(new Chunk("Name : " + name,FontFactory.GetFont("Arial", 10,1)));
ph1.Add(glue);
ph1.Add(new Chunk("Bill No. : " + BillNo, FontFactory.GetFont("Arial", 10,1)));
ph1.Add(new Chunk(Environment.NewLine));
ph1.Add(new Chunk("Address : " + address, FontFactory.GetFont("Arial", 10,1)));
ph1.Add(glue);
ph1.Add(new Chunk("Bill Date : " + billDate, FontFactory.GetFont("Arial", 10,1)));
ph1.Add(new Chunk(Environment.NewLine));
ph1.Add(new Chunk("Mobile No. : " + mobile, FontFactory.GetFont("Arial", 10,1)));
ph1.Add(glue);
ph1.Add(new Chunk("Scheme No. : " + orderNo, FontFactory.GetFont("Arial", 10,1)));
mm.Add(ph1);
para.Add(mm);
document.Add(para);
table = new PdfPTable(3);
table.TotalWidth = 340f;
table.LockedWidth = true;
table.SpacingBefore = 20f;
table.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(PhraseCell(new Phrase("HSN Code ", FontFactory.GetFont("Arial", 10,1)), PdfPCell.ALIGN_CENTER));
table.AddCell(PhraseCell(new Phrase("No of Installment", FontFactory.GetFont("Arial", 10,1)), PdfPCell.ALIGN_CENTER));
table.AddCell(PhraseCell(new Phrase("Installment Amount", FontFactory.GetFont("Arial", 10,1)), PdfPCell.ALIGN_CENTER));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 3;
cell.PaddingBottom = 10f;
table.AddCell(cell);
table.AddCell(PhraseCell(new Phrase("7113", FontFactory.GetFont("Arial", 10,1)), PdfPCell.ALIGN_CENTER));
table.AddCell(PhraseCell(new Phrase(paidNo, FontFactory.GetFont("Arial", 10,1)), PdfPCell.ALIGN_CENTER));
table.AddCell(PhraseCell(new Phrase(paidAmount, FontFactory.GetFont("Arial", 10,1)), PdfPCell.ALIGN_CENTER));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 3;
cell.PaddingBottom = 10f;
table.AddCell(cell);
PdfContentByte contentByte = writer.DirectContent;
contentByte.MoveTo(45.0, 627.0);
contentByte.LineTo(550.0, 627.0);
document.Add(table);
Paragraph para1 = new Paragraph();
Phrase ph2 = new Phrase();
Paragraph mm1 = new Paragraph();
string amountWord = ConvertNumbertoWords(Convert.ToInt64(paidAmount));
ph2.Add(new Chunk(Environment.NewLine));
ph2.Add(new Chunk(Environment.NewLine));
ph2.Add(new Chunk(Environment.NewLine));
ph2.Add(new Chunk(amountWord + " Only", FontFactory.GetFont("Arial", 10,1)));
ph2.Add(new Chunk(Environment.NewLine));
ph2.Add(new Chunk("By Cash", FontFactory.GetFont("Arial", 10,1)));
ph2.Add(new Chunk(Environment.NewLine));
ph2.Add(new Chunk(paidAmount, FontFactory.GetFont("Arial", 10,1)));
mm1.Add(ph2);
para1.Add(mm1);
document.Add(para1);
Paragraph para3 = new Paragraph();
Phrase ph3 = new Phrase();
Paragraph mm3 = new Paragraph();
ph3.Add(new Chunk(Environment.NewLine));
ph3.Add(new Chunk("Credit Card Charges :", FontFactory.GetFont("Arial", 10,1)));
ph3.Add(new Chunk(Environment.NewLine));
ph3.Add(new Chunk("Customer Sign.", FontFactory.GetFont("Arial", 10,1)));
ph3.Add(glue);
ph3.Add(new Chunk("For Example", FontFactory.GetFont("Arial", 10,1)));
mm3.Add(ph3);
para3.Add(mm3);
document.Add(para3);
document.Close();

