gpt4 book ai didi

c# - 更改 itextsharp 表中的字体大小

转载 作者:太空宇宙 更新时间:2023-11-03 10:45:25 33 4
gpt4 key购买 nike

我正在尝试使用 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、短语等的更多信息。http://www.mikesdotnetting.com/Article/82/iTextSharp-Adding-Text-with-Chunks-Phrases-and-Paragraphs

关于c# - 更改 itextsharp 表中的字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23598192/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com