【问题标题】:Add text to an exisiting pdf file using ITextSharp in c#?在 c# 中使用 ITextSharp 将文本添加到现有的 pdf 文件?
【发布时间】:2017-02-01 13:51:59
【问题描述】:

我正在尝试使用ITextSharp 将文本添加到现有的 pdf 文件。我完全不知道如何解决它。这是我到目前为止所尝试的。我想将 wordDocContents 附加到 pdf 文件中。我想在 pdf 文件的开头附加文本,因为 pdf 文件是空的。

using System;
using System.Web;
using System.Web.UI;
using TikaOnDotNet.TextExtraction;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace Project    
{
    public partial class Default : System.Web.UI.Page
    {
        public void button1Clicked(object sender, EventArgs args)
        {
            button1.Text = "You clicked me";    
            var textExtractor = new TextExtractor();
            var wordDocContents = textExtractor.Extract("t.csv");
            Console.WriteLine(wordDocContents);
            Console.ReadLine();
            generatepdffile();
        }

        protected void generatepdffile()
        {   
            Document doc = new Document();
            PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("file1.pdf"), FileMode.Create));
        }
    }
}

“wordDocContents”中的内容如下。我的目标是按照其在 pdf 文件中打印的方式附加内容。

ACCOUNT_NUMBER,CUSTOMER_NAMES,VALUE_DATE,BOOKING_DATE,TRANSACTION,ACCOUNT_TYPE,BALANCE_TYPE,REFERENCE,MONEY.OUT,MONEY.IN,RUNNING.BALANCE,BRANCH,EMAIL,ACTUAL.BALANCE,AVAILABLE.BALANCE
1000000001,TEST,,2847899,KES,Account,,,10/10/2016,9/11/2016,15181800,UPPER HILL BRANCH,another@yahoo.com,5403.75,5403.75,
1000000001,,9/11/2016,9/11/2016,Opening Balance,,,,,,4643.22,,,,,
1000000001,,12/10/2016,12/10/2016,Mobile Mpesa Transfer,,,,1533,,3110.22,,,,,
1000000001,,17-10-2016,17-10-2016,ATM Withdrawal,,,6.29006E+11,1000,,2110.22,,,,,
1000000001,,17-10-2016,17-10-2016,ATM Withdrawal,,,6.29118E+11,2000,,110.22,,,,,
1000000001,,17-10-2016,17-10-2016,Mobile Mpesa Transfer,,,,2083,,-1972.78,,,,,
1000000001,,17-10-2016,17-10-2016,Transfer from Mpesa,,,,0,4000,2027.22,,,,,
1000000001,,18-10-2016,18-10-2016,Mobile Mpesa Transfer,,,,333,,1694.22,,,,,

上述问题与建议的问题不重复,请查看以下链接。输出 pdf 的格式与链接中的格式完全不同。 https://postimg.org/image/tul7vxvrh/

【问题讨论】:

  • 为什么这个问题看起来和另一个人的这个问题完全一样? stackoverflow.com/questions/41981371/…(当然现在已经删除了..)
  • “添加到现有的 pdf 文件” 到底是什么意思?从新页面开始?还是从当前内容的末尾开始?或者在给定页面上的给定坐标?或者在某些表单域中?请足够精确,以便其他人可以帮助您。

标签: c# itext


【解决方案1】:

您可能需要至少在带有提取文本的 pdf 中添加一个段落。

一个可能的helper 可能是

private void AddParagraph(Document doc, int alignment, iTextSharp.text.Font font, iTextSharp.text.IElement content) 
{ 
   Paragraph paragraph = new Paragraph(); 
   paragraph.SetLeading(0f, 1.2f); 
   paragraph.Alignment = alignment; 
   paragraph.Font = font; 
   paragraph.Add(content);
   doc.Add(paragraph); 
}

草拟的用法示例

      private Font _largeFont = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD, BaseColor.BLACK); 
      void generatepdffile(string content)

          {
            Document doc = new Document();
            PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("file1.pdf"), FileMode.Create));
            doc.Open(); 
            AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, new Chunk(content));
            doc.Close();

          }

从类似你的东西调用

generatepdffile(wordDocContents.Text);

您可以阅读更多关于生成pdf表格的详细示例here

创建表格

基本上,您应该从 csv 字符串拆分创建表

    PdfPTable table = new PdfPTable(colNum);
    foreach (string line in content.Split('\n'))
    {
        foreach (string cellstr in line.Split(','))
        {
            PdfPCell cell = new PdfPCell(new Phrase(cellstr));
            table.AddCell(cell);
        }
    }
    AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, table);

【讨论】:

  • :- 谢谢....有没有办法可以在 pdf 文件中以表格格式显示数据?
  • @jessica 哦,对不起,这可能是example 的一张桌子......如果你想让我用简短的摘录更新答案,请告诉我
  • :- 谢谢...请您看看上面的快照并帮助我创建一张像图片中的表格。这是网址:- postimg.org/image/tul7vxvrh
  • :- 如果您能更新我的简短摘录,我将不胜感激,这可以帮助我生成上述链接中指定的输出。
  • 我只是添加了一个快速粗略的草稿,但您当然需要扩展并完成它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-07
  • 2012-07-23
  • 2014-07-06
  • 2014-12-12
  • 2016-01-29
相关资源
最近更新 更多