我正在尝试使用下面的代码导出格式化的Pdf文件。我试图完全限定'Font',但我仍然遇到相同的错误甚至更多。完全限定'Font','Argument 1'后无法标记此错误,无法将'iTextSharp.text.pdf.BaseFont'转换为'System.Drawing.FontFamily'。这是代码。
void ExportDataTableToPdf(DataTable dtaTable, String strPdfPath, string strHeader)
{
System.IO.FileStream fs = new FileStream(strPdfPath, FileMode.Create, FileAccess.Write, FileShare.None);
Document document = new Document();
document.SetPageSize(iTextSharp.text.PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
//Report header
BaseFont bsHead = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
Font fsHead = new Font(bsHead, 16, 1, Color.Gray); /***Red underlined code ***/
Paragraph pfgHeading = new Paragraph();
pfgHeading.Alignment = Element.ALIGN_CENTER;
pfgHeading.Add(new Chunk(strHeader.ToUpper(), fsHead));
document.Add(pfgHeading);
//Author
Paragraph prgAuthor = new Paragraph();
BaseFont btnAuthor = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
Font fsAuthor = new Font(btnAuthor, 8, 2, Color.Gray); /***Red underlined code ***/
prgAuthor.Alignment = Element.ALIGN_RIGHT;
prgAuthor.Add(new Chunk("Author:2Deep", fsAuthor));
prgAuthor.Add(new Chunk("\nRun Date:" + DateTime.Now.ToShortDateString(), fsAuthor));
document.Add(prgAuthor);
//Line separator
Paragraph p = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, Color.Black, ,Element.ALIGN_LEFT)));
document.Add(p);
//line break
document.Add(new Chunk("\n", fsHead));
//Write Table
PdfPTable table = new PdfPTable(dtaTable.Columns.Count);
//Table Header
BaseFont btnColumnHeader = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
Font fntColumnHeader = new Font(btnColumnHeader, 10, 1, Color.White); /***Red underlined code ***/
for (int i = 0; i < dtaTable.Columns.Count; i++)
{
PdfPCell cell = new PdfPCell(); /***Red underlined code ***/
cell.BackgroundColor = Color.Gray;
cell.AddElement(new Chunk(dtaTable.Columns[i].ColumnName.ToUpper(), fntColumnHeader));
table.AddCell(cell);
}
//table data
for (int i=0; i < dtaTable.Rows.Count; i++)
{
for (int j=0; j < dtaTable.Columns.Count; j++)
{
table.AddCell(dtaTable.Rows[i][j].ToString());
}
}
document.Add(table);
document.Close();
writer.Close();
fs.Close();
答案 0 :(得分:2)
您在代码文件的顶部(或在名称空间定义中)具有using System.Drawing;
和using 'iTextSharp.text.pdf;
。
现在,在System.Drawing
中有一个Font
类型,同样在iTextSharp.text.pdf
中也有一个Font
对象。 Visual Studio无法确定您要使用哪一个,因此您必须停止出现歧义的情况。
您可以通过多种方式执行此操作:
System.Drawing
中的任何内容,请删除using System.Drawing;
声明)。Font a = new Font()
,使用System.Drawing.Font a = new System.Drawing.Font();
using
语句对类型进行别名:using TheFont = System.Drawing.Font
,然后在代码中编写类似TheFont = new TheFont();
的内容