从表单数据创建PDF并保存

7
我正在使用可编辑的pdf文件(由Nitro PDF Software创建)在我的应用程序中。这些pdf文件有很多可编辑字段(如文本框)和一个按钮(如提交)。
每当用户打开pdf文件,输入文本并单击提交按钮时,他们将被重定向到一个aspx页面。
我该如何获取发送到此页面的所有静态和动态值,并创建另一个包含输入数据的pdf文件?我该如何保存创建的pdf文件?

2
为什么不去问PDF工具包的创建者呢? - jgauffin
5个回答

9

根据维基百科PDF Interactive elements,将数据和PDF整合有两种可能性(这里也有规范的链接):

  • AcroForms(也称为Acrobat表格),在PDF 1.2格式规范中引入,并包含在所有后续的PDF规范中。
  • Adobe XML Forms Architecture(XFA)表格,在PDF 1.5格式规范中引入。XFA规范未包含在PDF规范中,仅作为可选功能引用。 Adobe XFA表格与AcroForms不兼容。

由于兼容性问题,我会选择AcroForms。在这种情况下,我会使用XFDF,因为它是XML格式,因此更易于解析。我从未使用过Nitro,但是当您构建PDF表格时,通常会提供“保存”按钮,并为此按钮选择一个操作,“将表单发送/发布到服务器”并选择数据格式为XML,即XFDF。

这只在浏览器中查看 PDF 时有效。因此,典型的使用情况是:在服务器上有一个空白的 PDF 模板,在将 PDF 返回给用户之前,将数据混合到 PDF 中,用户在表单中输入数据(PDF 通过插件或在 Chrome 中本地打开),然后用户按下保存按钮,该按钮会将 XML 发送到服务器。下一次用户请求其 PDF 时,您将最新数据与模板再次混合。
因此,只有两个问题未解决: - 如何生成 XFDF: 非常简单,请参见http://wiki.developerforce.com/page/Adobe_XFDFParsing XML using XDocument以获取文件结构示例 - 如何将 XFDF 与 PDF 混合: 可以使用itext完成此操作,在 stackoverflow 上有几个示例,例如https://stackoverflow.com/a/6274311/734687

在这里查看完整的过程:http://itextpdf.com/book/chapter.php?id=9。该示例在运行时动态更新PDF表单。由于使用了iText,因此Java和C#之间没有区别。

请注意,早期版本的iText(Java版本为2.1.7及以下,C#版本为4.1.6及以下)是根据Mozilla Public License或LGPL分发的,而当前版本是根据Affero General Public License分发的。这解释了为什么仍在使用旧版本。


4

以下是在asp.net中生成PDF的代码示例:

下面是一个完整的代码示例,可以帮助您入门。 // 代码

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();
 }
}

2
如何获取发布到此页面的所有静态和动态值?
您可以像检索HTML控件中的任何其他值一样检索它们,例如:
string MyVal = Request.Form["FieldName"];

并创建另一个包含输入数据的PDF文件吗?
在此,您可以使用自己选择的PDF库(iText.Net、Nitro PDF、Amyuni PDF Creator.Net),加载您的PDF表单,为每个字段设置值,必要时压平您的文件,并保存。此部分的代码取决于所使用的库,但它们通常都有良好的文档,因此您应该能够轻松找到示例代码。

1

生成PDF,请使用以下代码:

名称空间:

     Using System.IO;
     Using.iTextSharp.text;
     Using.iTextSharp.text.pdf;

当按钮被点击时:
     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();
    }

0
下载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();

这个回答和问题有什么确切的关系呢? - mkl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接