【问题标题】:Create PDF from form data and save it从表单数据创建 PDF 并保存
【发布时间】:2012-03-12 18:57:07
【问题描述】:

我在我的应用程序中使用可编辑的 pdf 文件(由 Nitro PDF Software 创建)。这些 pdf 文件有很多可编辑的字段(如文本框)和一个按钮(如提交)。

每当用户打开该 pdf 文件、输入文本并单击提交按钮时,他们都会被重定向到一个 aspx 页面。

如何获取发布到此页面的所有静态和动态值,并使用输入的数据创建另一个 pdf 文件?以及如何保存创建的 pdf 文件?

【问题讨论】:

  • 为什么不问问PDF工具包的创建者?

标签: c# asp.net pdf


【解决方案1】:

来自维基百科,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 时,您获取最近的数据并再次将其与模板混合。

所以只有两个问题是开放的:

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

请注意,以前版本的 iText(Java 最高 2.1.7 和 C# 最高 4.1.6)是根据 Mozilla 公共许可证或 LGPL 分发的,而当前版本是根据 Affero 通用公共许可证分发的。这就解释了为什么仍然使用旧版本。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      尝试开源库http://pdfsharp.codeplex.com/,可以在这里找到示例http://www.pdfsharp.net/wiki/

      【讨论】:

        【解决方案4】:

        如何获取发布到此页面的所有静态和动态值?

        您可以在从 html 控件检索任何其他值时检索它们,例如:

        string MyVal = Request.Form["FieldName"];
        

        并使用输入的数据创建另一个 pdf 文件?

        在这里,您可以使用您选择的 PDF 库(iText.Net、Nitro PDF、Amyuni PDF Creator.Net),加载您的 PDF 表单,为每个字段设置值,根据需要拼合您的文件,然后保存。这部分的代码取决于所使用的库,但它们通常都有很好的文档记录,因此您应该能够轻松找到示例代码。

        【讨论】:

          【解决方案5】:

          为什么不按照以下链接中提到的方式进行操作。

          http://www.gnostice.com/nl_article.asp?id=176&t=Generate_PDF_Forms_In_ASP_NET_Using_PDFOne_NET_v3

          它使用另一个 PDF 生成工具。但是你应该可以用硝基做同样的事情。

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              【解决方案7】:

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

              【讨论】:

              • 这究竟是如何回答这个问题的?
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2018-02-19
              • 2016-12-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多