0

I am trying to add text to an existing pdf-file using ITextSharp. I am completely clueless how to solve it. This is what I tried so far. I want to append the wordDocContents to the pdf-file. I want to append text at the beginning of the pdf file as the pdf file is empty.

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

The contents in 'wordDocContents' is as follows. My goal is to append the contents just the way its printed below in the pdf file.

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,,,,,

The above question is not a duplicate of the question suggested, Kindly have a look at the following link. The format of the output pdf is completetly different than the one in the link. https://postimg.org/image/tul7vxvrh/

  • 1
    Why does this question look exactly like this question yet from another person? http://stackoverflow.com/questions/41981371/how-to-add-text-to-an-exisiting-pdf-file-using-itextsharp-in-c (which of course is now deleted..) – CodeLikeBeaker Feb 01 '17 at 14:08
  • What exactly do you mean by *"add to an existing pdf-file"*? Starting on a new page? Or starting at the end of the current content? Or at given coordinates on a given page? Or in some form fields? Please be precise enough for others to help you. – mkl Feb 01 '17 at 14:13
  • 1
    Possible duplicate of [Insert text in existing pdf with itextsharp](http://stackoverflow.com/questions/41315738/insert-text-in-existing-pdf-with-itextsharp) – Рахул Маквана Feb 01 '17 at 14:53

1 Answers1

0

You likely need to add at least a Paragraph to the pdf with the extracted text.

A possible helper could be

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

A drafted example of usage

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

          }

called from something like your

generatepdffile(wordDocContents.Text);

You can read more detailed examples about generating pdf tables here

Creating a table

Basically, you should create the table from the csv string splitting

    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);
  • :- Thank you....Is there a way I can display the data in a table format in the pdf file ? –  Feb 01 '17 at 14:34
  • @jessica oh, sorry, this is a possible [example](http://www.c-sharpcorner.com/blogs/create-table-in-pdf-using-c-sharp-and-itextsharp) for a table... let me know if you want me to update the answer with a short extract –  Feb 01 '17 at 14:37
  • :- Thanks...Kindly can you please have a look at the above snapshot and help me create a table like the one in the picture. This is the url:- https://postimg.org/image/tul7vxvrh/ –  Feb 01 '17 at 14:48
  • :- I would really appreciate if you can update me with a short extract which can help me generate the output as specified in the above link. –  Feb 01 '17 at 14:58
  • I've added just a quick, rough draft but you need to expand and complete it of course –  Feb 01 '17 at 14:59
  • :- Thanks a lot. I really appreciate your help a lot. –  Feb 01 '17 at 15:10