Reputation:
I am trying to append data from a csv file in a specific format to a pdf file. The problem is that I have been successfully able to append the data but for some reason the lines between the table rows are not being drawn in my pdf document. I am really lost as I don't know what I could be doing wrong.
This is what I tried so far :-
using System;
using System.Web;
using System.Web.UI;
using TikaOnDotNet.TextExtraction;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using Excel;
using System.Data;
using System.Collections.Generic;
namespace Project
{
public partial class Default : System.Web.UI.Page
{
int y;
string[] res = null;
public void button1Clicked(object sender, EventArgs args)
{
button1.Text = "You clicked me";
DPdfView v = new DPdfView();
res = v.ReadCSV().ToArray();
generatepdffile(res);
}
protected void generatepdffile(string[] res)
{
Document doc = new Document();
PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("file9.pdf"), FileMode.Create));
doc.Open();
PdfPTable table = new PdfPTable(7);
PdfPCell c1 = new PdfPCell();
PdfPCell c2 = new PdfPCell();
PdfPCell c3 = new PdfPCell();
PdfPCell c4 = new PdfPCell();
PdfPCell c5 = new PdfPCell();
PdfPCell c6 = new PdfPCell();
PdfPCell c7 = new PdfPCell();
String con = null;
for (int i= 0;i<res.Length;i++)
{
List<String> col = new List<String>();
foreach (string line in res[i].ToString().Split('|'))
{
con = res[i];
col.Add(line);
}
string[] columns = col.ToArray();
c1.AddElement(new Phrase(columns[0]));
c2.AddElement(new Phrase(columns[1]));
c3.AddElement(new Phrase(columns[2]));
c4.AddElement(new Phrase(columns[3]));
c5.AddElement(new Phrase(columns[4]));
c6.AddElement(new Phrase(columns[5]));
c7.AddElement(new Phrase(columns[6]));
table.Rows.Add(new PdfPRow(new PdfPCell[] { c1, c2, c3, c4, c5, c6, c7 }));
}
AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, table);
AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, new Chunk(con));
doc.Close();
}
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);
}
}
This is how my file looks now:
https://postimg.org/image/vbn3ui2qh/
Upvotes: 0
Views: 280
Reputation: 77528
Your code is much too long. Reduce it to:
PdfPTable table = new PdfPTable(7);
for (int i= 0;i<res.Length;i++)
{
List<String> col = new List<String>();
foreach (string line in res[i].ToString().Split('|'))
{
table.AddCell(new Phrase(line));
}
}
document.Add(table);
As you can see, this is much shorter than what you have.
I don't understand why:
List<String> col
when all you need is to put string
values in a table.PdfPRow
class to create the row. I am the #1 producer of iText documentation, and I haven't written any documentation that leads to believe you should use the PdfRow
class.PdfPTable
inside a Paragraph
. That isn't necessary. If you think it is, explain why you think so, and we'll explain how you can achieve what you want without using a Paragraph
.Upvotes: 1