在itextsharp表中更改字体大小 [英] Changing font size in itextsharp table

查看:403
本文介绍了在itextsharp表中更改字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用iTextSharp查看PDF文件中的表格。我已经想出了如何添加表格,并通过单击按钮打开PDF文件,但表格字体太大,并且文字被包装使它们难以阅读。我已输入此代码,但表格字体没有任何反应。

Im attempting to view a table in a PDF file using iTextSharp. I've figured out how to add the table, and make the PDF file open off a button click, but the table font is too big, and words are wrapped making them hard to read. I've entered this code, but nothing happens to the tables font.

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


        }
    }
}


推荐答案

尝试向Phrase构造函数添加字体:

Try adding font to Phrase constructors:

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

Check this link for more info about Chunkc, Phrases etc. http://www.mikesdotnetting.com/Article/82/iTextSharp-Adding-Text-with-Chunks-Phrases-and-Paragraphs

这篇关于在itextsharp表中更改字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆