我尝试使用iTextSharp查看PDF文件中的表格。我已经弄明白了如何添加表格,并通过单击按钮打开PDF文件,但表格字体太大,文字被包裹起来使它们难以阅读。我已输入此代码,但表格字体没有任何反应。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace Lewis_Warby_Airbrushing
{
public partial class selectedorderForm : Form
{
public selectedorderForm(string strValue)
{
InitializeComponent();
textBox1.Text = strValue;
}
DataTable dt;
private void selectedorderForm_Load(object sender, EventArgs e)
{
SqlCeConnection con = new SqlCeConnection(@"Data Source=|DataDirectory|\LWADataBase.sdf;");
SqlCeDataAdapter sda = new SqlCeDataAdapter("select * from orderTBL ", con);
dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
DataView dv = new DataView(dt);
dv.RowFilter = "[Order Number] like '%" + textBox1.Text.Trim() + "%'";
dataGridView1.DataSource = dv;
}
private void pdfBTN_Click(object sender, EventArgs e)
{
iTextSharp.text.Font fontTitle = FontFactory.GetFont("Arial", 18, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 10, 10, 10);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("PurchaseOrder-"+textBox1.Text+".pdf", FileMode.Create));
doc.Open();
Paragraph title = new Paragraph("PURCHASE ORDER " + textBox1.Text +"\n Lewis Warby Airbrushing\n", fontTitle);
title.Alignment = Element.ALIGN_CENTER;
doc.Add(title);
title.SpacingAfter = 15f;
iTextSharp.text.Font fontTable = FontFactory.GetFont("Arial", 5, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
PdfPTable table = new PdfPTable(dataGridView1.Columns.Count);
table.SpacingBefore = 45f;
table.TotalWidth = 216f;
table.DefaultCell.Phrase = new Phrase() { Font = fontTable };
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
table.AddCell(new Phrase(dataGridView1.Columns[j].HeaderText));
}
table.HeaderRows = 1;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int k = 0; k < dataGridView1.Columns.Count; k++)
{
if (dataGridView1[k, i].Value != null)
{
table.AddCell(new Phrase(dataGridView1[k, i].Value.ToString()));
}
}
}
doc.Add(table);
doc.Close();
System.Diagnostics.Process.Start("PurchaseOrder-"+textBox1.Text+".pdf");
}
}
}
答案 0 :(得分:2)
尝试向Phrase构造函数添加字体:
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
table.AddCell(new Phrase(dataGridView1.Columns[j].HeaderText,fontTable));
}
//..
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int k = 0; k < dataGridView1.Columns.Count; k++)
{
if (dataGridView1[k, i].Value != null)
{
table.AddCell(new Phrase(dataGridView1[k, i].Value.ToString(),fontTable));
}
}
}
查看此链接,了解有关Chunkc,Phrases等的更多信息。http://www.mikesdotnetting.com/Article/82/iTextSharp-Adding-Text-with-Chunks-Phrases-and-Paragraphs