改变itextsharp表格的字体大小
本文关键字:字体 itextsharp 表格 改变 | 更新日期: 2023-09-27 18:03:01
我试图使用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");
}
}
}
尝试在短语构造函数中添加字体:
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