本文整理汇总了C#中iTextSharp.text.Font.SetStyle方法的典型用法代码示例。如果您正苦于以下问题:C# Font.SetStyle方法的具体用法?C# Font.SetStyle怎么用?C# Font.SetStyle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类iTextSharp.text.Font
的用法示例。
在下文中一共展示了Font.SetStyle方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetFormattedFont
private static IT.Font GetFormattedFont(Font font)
{
IT.Font iTextFont = new IT.Font();
//Return default font if font style is not available.
if (font == null)
{
return new IT.Font();
}
foreach (var format in font.Formats)
{
switch (format)
{
case FontFormats.Bold:
iTextFont.SetStyle(IT.Font.BOLD);
break;
case FontFormats.Italic:
iTextFont.SetStyle(IT.Font.ITALIC);
break;
case FontFormats.Underlined:
iTextFont.SetStyle(IT.Font.UNDERLINE);
break;
default:
break;
}
}
return iTextFont;
}
示例2: ExportReport
public ExportReport()
{
xdoc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + "\\AppTemplate\\report.xml");
pdfDoc = new Document(PageSize.A4, 0, 0, 0, 0);
Rectangle rect = new Rectangle(794, 1123);
pdfDoc.SetPageSize(rect);
pdfDoc.SetMargins(0, 0, 20, 0);
BaseFont.AddToResourceSearch("iTextAsian.dll");
BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
BaseFont bsFont = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
fontTitle = new Font(bsFont, 20);
fontContent = new Font(bsFont, 12);
fontLabel = new Font(bsFont, 12);
fontTableRemark = new Font(bsFont, 14);
fontLabel.SetStyle(Font.BOLD);
fontTitle.SetStyle(Font.BOLD);
fontTableRemark.SetStyle(Font.BOLD);
}
示例3: AbsExportPDF
/// <summary>
/// 用report.xml的xdocument实例化
/// </summary>
/// <param name="doc">report内容</param>
public AbsExportPDF(XDocument doc)
{
xdoc = doc;
pdfDoc = new Document(PageSize.A4, 0, 0, 0, 0);
Rectangle rect = new Rectangle(794, 1123);
pdfDoc.SetPageSize(rect);
pdfDoc.SetMargins(0, 0, 20, 0);
BaseFont.AddToResourceSearch("iTextAsian.dll");
BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
BaseFont bsFont;
bsFont = BaseFont.CreateFont(@"c:\WINDOWS\fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
fontTitle = new Font(bsFont, 20);
fontContent = new Font(bsFont, 12);
fontLabel = new Font(bsFont, 12);
fontTableRemark = new Font(bsFont, 14);
fontLabel.SetStyle(Font.BOLD);
fontTitle.SetStyle(Font.BOLD);
fontTableRemark.SetStyle(Font.BOLD);
}
示例4: setDefaults
private void setDefaults(IPdfFont baseFont, Font toFont)
{
toFont.Color = baseFont.Color;
toFont.Size = baseFont.Size;
toFont.SetStyle((int)baseFont.Style);
}
示例5: AddSpacing
/**
* Adds extra space.
* This method should probably be rewritten.
*/
protected internal void AddSpacing(float extraspace, float oldleading, Font f) {
if (extraspace == 0) return;
if (pageEmpty) return;
if (currentHeight + line.Height + leading > IndentTop - IndentBottom) return;
leading = extraspace;
CarriageReturn();
if (f.IsUnderlined() || f.IsStrikethru()) {
f = new Font(f);
int style = f.Style;
style &= ~Font.UNDERLINE;
style &= ~Font.STRIKETHRU;
f.SetStyle(style);
}
Chunk space = new Chunk(" ", f);
space.Process(this);
CarriageReturn();
leading = oldleading;
}
示例6: OnEndPage
public override void OnEndPage(PdfWriter writer, Document document)
{
Font Calibri = FontFactory.GetFont("Calibri", 9, iTextSharp.text.Font.NORMAL);
Font CalibriBold = FontFactory.GetFont("Calibri", 10, iTextSharp.text.Font.BOLD);
Font CalibriHeader = FontFactory.GetFont("Calibri", 60, iTextSharp.text.Font.BOLD);
Font CalibriStrikeThrough = FontFactory.GetFont("Calibri", 10, iTextSharp.text.Font.STRIKETHRU);
Font cbh = new Font(CalibriHeader);
cbh.Size = 12;
cbh.SetStyle(1);
String _clientaddress = String.Empty;
if (ClientAddress2 != null)
{
_clientaddress = ClientAddress + ", " + ClientAddress2;
}
else
{
_clientaddress = ClientAddress;
}
String footerstr2 = Client + ", " + _clientaddress + ", " + ClientCity + ", " + ClientCounty + ", " + ClientZipCode + ".";
Chunk f = new Chunk(footerstr2, new Font(Calibri));
// Paragraph footer1 = new Paragraph(footerstr2, FontFactory.GetFont(FontFactory.TIMES, 10, iTextSharp.text.Font.NORMAL));
Paragraph footer1 = new Paragraph(f);
footer1.Alignment = Element.ALIGN_CENTER;
String footerstr = " " + Client + " is Registered in England and Wales. Company Number " + CompRegistrationNo;
Chunk f1 = new Chunk(footerstr, new Font(Calibri));
//Paragraph footer = new Paragraph(footerstr, FontFactory.GetFont(FontFactory.TIMES, 10, iTextSharp.text.Font.NORMAL));
Paragraph footer = new Paragraph(f1);
Int32 footerlength = footerstr.Length;
Int32 footerlength2 = footerstr2.Length;
// footer.Alignment = Element.ALIGN_RIGHT;
PdfPTable tabFot = new PdfPTable(new float[] { 1F });
tabFot.HorizontalAlignment = 1;
tabFot.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell cell1;
cell1 = new PdfPCell(footer1);
cell1.Border = 0;
PdfPCell cell;
tabFot.TotalWidth = 580F;
cell = new PdfPCell(footer);
cell.Border = 0;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.HorizontalAlignment = 0;
tabFot.AddCell(cell1);
tabFot.AddCell(cell);
tabFot.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin;
// tabFot.WriteSelectedRows(0, -1, 140, document.Bottom+30, writer.DirectContent);
tabFot.WriteSelectedRows(0, -1, document.LeftMargin + 110, document.Bottom + 30, writer.DirectContent);
}
示例7: GetFont
protected Font GetFont(FontEnum mtype)
{
Font mFont = null;
switch (mtype)
{
case FontEnum.TitleCenter:
mFont = new Font(bfSun, 18);
mFont.SetStyle("normal");
break;
case FontEnum.TitleLeft1:
mFont = new Font(bfSun, 16);
mFont.SetStyle("bold");
break;
case FontEnum.TitleLeft2:
mFont = new Font(bfSun, 14);
mFont.SetStyle("normal");
break;
case FontEnum.Title12Bold:
mFont = new Font(bfSun, 12);
mFont.SetStyle("bold");
break;
case FontEnum.TableHeader:
mFont = new Font(bfSun, 12);
mFont.SetStyle("bold");
break;
case FontEnum.TableIndex:
mFont = new Font(bfSun, 11);
mFont.SetStyle("bold");
break;
case FontEnum.TableSM:
mFont = new Font(bfSun, 9);
mFont.SetStyle("normal");
break;
default:
mFont = new Font(bfSun, 12);
mFont.SetStyle("normal");
break;
}
return mFont;
}
示例8: Signatures
private List<Paragraph> Signatures()
{
Paragraph signature1 = new Paragraph(20f);
Paragraph signature2 = new Paragraph(20f);
Paragraph signature3 = new Paragraph(20f);
Font underline = new Font(signature1.Font);
underline.SetStyle(Font.UNDERLINE);
string spaces = " ";
spaces = spaces.PadLeft(70);
Phrase line = new Phrase(spaces, underline);
signature1.Add(line);
signature1.Add(new Phrase("(1)", this.ContentFont));
signature2.Add(line);
signature2.Add(new Phrase("(2)", this.ContentFont));
signature3.Add(line);
signature3.Add(new Phrase("(3)", this.ContentFont));
List<Paragraph> signatures = new List<Paragraph>() { signature1, signature2, signature3 };
return signatures;
}
示例9: Introduction
private Paragraph Introduction()
{
string space = " ";
Paragraph introduction = new Paragraph(20f);
introduction.Alignment = Element.ALIGN_JUSTIFIED;
Font underline = new Font(introduction.Font);
underline.SetStyle(Font.UNDERLINE);
introduction.Add("Aos ");
Phrase day = new Phrase(space.PadLeft(4), underline);
introduction.Add(day);
introduction.Add(" dias do mês de ");
Phrase month = new Phrase(space.PadLeft(20), underline);
introduction.Add(month);
introduction.Add(" de ");
Phrase year = new Phrase(space.PadLeft(8), underline);
introduction.Add(year);
introduction.Add(" no(a) ");
Phrase space6 = new Phrase(space.PadLeft(50), underline);
introduction.Add(space6);
introduction.Add(new Phrase(" "));
Phrase space7 = new Phrase(space.PadLeft(130), underline);
introduction.Add(space7);
introduction.Add(" na presença dos abaixo assinados, procedeu-se à inutilização por de acordo com o(s) artigo(s) ");
Phrase space10 = new Phrase(space.PadLeft(20), underline);
introduction.Add(space10);
introduction.Add(" da Portaria n.º ");
introduction.Add(year);
introduction.Add(" / ");
introduction.Add(year);
introduction.Add(" e disposições da tabela de seleção, dos documentos, a seguir identificados: ");
return introduction;
}
示例10: Prepare_Print
protected void Prepare_Print()
{
String[] weekDays = new String[] { " ","Sun", "Mon", "Tue", "Wed", "Thu" };
int[] sessions = new int[] {0,1,2,3,4,5,6,7,8,9};
PdfPTable tblSchedule = new PdfPTable(10);
PdfPRow[] tempRow = new PdfPRow[6];
PdfPCell[][] tempCell = new PdfPCell[6][];
int rowIndex = 0;
int cellIndex = 0;
Paragraph subject = new Paragraph();
Paragraph teacher = new Paragraph();
Paragraph lunch = new Paragraph();
Paragraph dayPara = new Paragraph();
Paragraph sessionPara = new Paragraph();
Paragraph teacherPara = new Paragraph();
Font lunch_font = new Font();
Font day_session_para = new Font();
Font session_font = new Font();
Font teacher_font = new Font();
session_font.Size = 10;
teacher_font.SetStyle("Italics");
teacher_font.Size = 7;
lunch_font.SetColor(153, 153, 255);
lunch_font.SetStyle("italics");
lunch = new Paragraph("Lunch", lunch_font);
day_session_para.SetColor(0, 0, 153);
foreach (String weekDay in weekDays)
{
tempCell[rowIndex] = new PdfPCell[10];
tempRow[rowIndex] = new PdfPRow(tempCell[rowIndex]);
foreach (int session in sessions)
{
if (session == 0 || session == 6)
{
if (session == 0)
{
dayPara = new Paragraph(weekDays[rowIndex],day_session_para);
tempCell[rowIndex][cellIndex] = new PdfPCell(dayPara);
}
else
if (weekDay != " ")
{
tempCell[rowIndex][cellIndex] = new PdfPCell(lunch);
}
else
{
//tempCell[rowIndex][cellIndex] = new PdfPCell(new Phrase(Convert.ToString(sessions[cellIndex])));
dayPara = new Paragraph(Convert.ToString(sessions[cellIndex]), day_session_para);
tempCell[rowIndex][cellIndex] = new PdfPCell(dayPara);
}
}
else
{
if (weekDay == " ")
{
dayPara = new Paragraph(Convert.ToString(sessions[cellIndex]), day_session_para);
tempCell[rowIndex][cellIndex] = new PdfPCell(dayPara);
//tempCell[rowIndex][cellIndex] = new PdfPCell(new Phrase(Convert.ToString(sessions[cellIndex])));
}
else
{
string query = "select B.CourseTitle,A.TeacherID from tblStudentCourseMap as A,tblCourses as B where A.ComCod = B.ComCod and A.DaySession = '" + weekDay + session + "' and A.StudentID = '" + Current_User_ID + "'";
myCon.ConOpen();
SqlDataReader sessionDet = myCon.ExecuteReader(query);
if (sessionDet.Read())
if (!sessionDet.IsDBNull(0))
{
sessionPara = new Paragraph(sessionDet.GetString(0), session_font);
//tempCell[rowIndex][cellIndex] = new PdfPCell(sessionPara);
teacherPara = new Paragraph(sessionDet.GetString(1), teacher_font);
tempCell[rowIndex][cellIndex] = new PdfPCell(new Phrase(sessionPara));
tempCell[rowIndex][cellIndex].Phrase.Add(new Phrase("\n"));
tempCell[rowIndex][cellIndex].Phrase.Add(teacherPara);
//tempCell[rowIndex][cellIndex] = new PdfPCell(new Phrase(sessionDet.GetString(0) + "\n" + sessionDet.GetString(1)));
}
else
{
tempCell[rowIndex][cellIndex] = new PdfPCell(new Phrase(""));
//tempCell[rowIndex][cellIndex
}
else
tempCell[rowIndex][cellIndex] = new PdfPCell(new Phrase(""));
myCon.ConClose();
tempCell[rowIndex][cellIndex].FixedHeight = 75;
}
}
//tempCell[rowIndex][cellIndex].Width = 50;
cellIndex++;
//tempRow[rowIndex].Cells.Add(tempCell[cellIndex++, rowIndex]);
}
cellIndex = 0;
//rowIndex++;
//.........这里部分代码省略.........
示例11: InitFirstPage
/// <summary>
/// 第一页信息
/// </summary>
/// <param name="document"></param>
public void InitFirstPage()
{
document.Add(new Paragraph("\n\n\n\n"));
//Title
Font font = new Font(bfSun, 24);
font.SetStyle("bold");
Paragraph pg = new Paragraph(SystemTitle, font);
pg.Alignment = Element.ALIGN_CENTER;
document.Add(pg);
//subTitle
font = new Font(bfSun, 18);
font.SetStyle("bold");
pg = new Paragraph(SubTitle, font);
pg.Alignment = Element.ALIGN_CENTER;
document.Add(pg);
//版权、时间
document.Add(new Paragraph("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"));
font = new Font(bfSun, 18);
font.SetStyle("bold");
pg = new Paragraph(Company, font);
pg.Alignment = Element.ALIGN_CENTER;
document.Add(pg);
font = new Font(bfSun, 14);
font.SetStyle("bold");
string mTime = string.Format("{0}年{1}月", DateTime.Now.Year, DateTime.Now.Month);
pg = new Paragraph(mTime, font);
pg.Alignment = Element.ALIGN_CENTER;
document.Add(pg);
}
示例12: AddDataToPdf
public static void AddDataToPdf(SupermarketContext db)
{
Document doc = new Document();
PdfAWriter.GetInstance(doc, new FileStream(@"../../.." + "/Aggregated-Sales-Report.pdf", FileMode.Create));
doc.Open();
PdfPTable table = new PdfPTable(5);
Font fontBold = new Font(Font.FontFamily.HELVETICA, 14f);
fontBold.SetStyle("bold");
Font fontNormal = new Font(Font.FontFamily.HELVETICA, 12f);
PdfPCell header = new PdfPCell(new Phrase("Aggregated Sales Report", fontBold));
header.Colspan = 5;
header.HorizontalAlignment = 1;
table.AddCell(header);
using (db)
{
var dates =
(from d in db.SalesReports
select d.ReportDate).Distinct();
decimal grandTotal = 0;
foreach (var date in dates)
{
PdfPCell cellDate = new PdfPCell(new Phrase("Date : " + date.ToShortDateString()));
cellDate.Colspan = 5;
table.AddCell(cellDate);
PdfPCell cellProduct = new PdfPCell(new Phrase("Products", fontBold));
table.AddCell(cellProduct);
PdfPCell cellQuantity = new PdfPCell(new Phrase("Quantity", fontBold));
table.AddCell(cellQuantity);
PdfPCell cellUnitPrice = new PdfPCell(new Phrase("Unit Price", fontBold));
table.AddCell(cellUnitPrice);
PdfPCell cellLocation = new PdfPCell(new Phrase("Location", fontBold));
table.AddCell(cellLocation);
PdfPCell cellSum = new PdfPCell(new Phrase("Sum", fontBold));
table.AddCell(cellSum);
var sales =
(from s in db.SalesReports
where s.ReportDate == date
select s);
decimal totalSum = 0;
foreach (var sale in sales)
{
if (sale.Product.ProductName != null)
{
table.AddCell(sale.Product.ProductName);
table.AddCell(sale.Quantity.ToString() + " " + db.Measures
.Where(x => x.MeasureID == sale.Product.MeasureID)
.Select(x => x.MeasureName).First().ToString());
table.AddCell(sale.UnitPrice.ToString());
table.AddCell(sale.SupermarketName);
table.AddCell(sale.Sum.ToString());
totalSum += sale.Sum;
}
}
string text = string.Format("Total sum for {0} :", date.ToShortDateString());
PdfPCell cellTotal = new PdfPCell(new Phrase(text, fontNormal));
cellTotal.Colspan = 4;
cellTotal.HorizontalAlignment = 2;
table.AddCell(cellTotal);
table.AddCell(totalSum.ToString());
grandTotal += totalSum;
}
string grand = string.Format("Grand Total: ");
PdfPCell cellGrand = new PdfPCell(new Phrase(grand));
cellGrand.Colspan = 4;
cellGrand.HorizontalAlignment = 2;
table.AddCell(cellGrand);
table.AddCell(grandTotal.ToString());
doc.Add(table);
}
doc.Close();
}
示例13: Vendas
public ActionResult Vendas(FormCollection form)
{
if (form.HasKeys())
{
int cliente = int.Parse(form["txtCliente"]);
int produto = int.Parse(form["txtProduto"]);
List<Venda> vendas = Venda.Listar();
if (cliente > 0)
{
vendas = vendas.Where(v => v.CodCliente == cliente).ToList();
}
if (produto > 0)
{
vendas = vendas.Where(v => v.VendaProduto.FirstOrDefault(vp => vp.CodProduto == produto) != null).ToList();
}
if (!String.IsNullOrWhiteSpace(form["txtDataInicio"]))
{
DateTime data = DateTime.Parse(form["txtDataInicio"], new CultureInfo("pt-BR"));
vendas = vendas.Where(v => v.DtVenda >= data).ToList();
}
if (!String.IsNullOrWhiteSpace(form["txtDataTermino"]))
{
DateTime data = DateTime.Parse(form["txtDataTermino"] + " 23:59:59", new CultureInfo("pt-BR"));
vendas = vendas.Where(v => v.DtVenda <= data).ToList();
}
if (vendas.Count == 0)
{
TempData["Erro"] = "Não há vendas no filtro especificado";
return RedirectToAction("Vendas");
}
vendas = vendas.OrderBy(v => v.DtVenda).ToList();
// PDF
MemoryStream memStream = new MemoryStream();
Document document = new Document(PageSize.A4.Rotate(), 36, 36, 54, 36);
PdfWriter writer = PdfWriter.GetInstance(document, memStream);
writer.CloseStream = false;
writer.PageEvent = new PaginacaoPdfPageEventHelper();
document.Open();
Paragraph titulo = new Paragraph("Relatório de Vendas");
titulo.Font.Size = 18;
titulo.Font.SetStyle(Font.BOLD);
document.Add(titulo);
document.Add(Chunk.NEWLINE);
if (!String.IsNullOrWhiteSpace(form["txtDataInicio"]) || !String.IsNullOrWhiteSpace(form["txtDataTermino"]))
{
string periodo = "";
if (!String.IsNullOrWhiteSpace(form["txtDataInicio"]) && !String.IsNullOrWhiteSpace(form["txtDataTermino"]))
{
periodo = $"de {form["txtDataInicio"]} até {form["txtDataTermino"]}";
}
else if (!String.IsNullOrWhiteSpace(form["txtDataInicio"]))
{
periodo = $"de {form["txtDataInicio"]} até {DateTime.Today.ToString("dd/MM/yyyy")}";
}
else if (!String.IsNullOrWhiteSpace(form["txtDataTermino"]))
{
periodo = $"até {form["txtDataTermino"]}";
}
Paragraph filtroPeriodo = new Paragraph($"Período: {periodo}");
filtroPeriodo.Font.SetStyle(Font.BOLD);
document.Add(filtroPeriodo);
}
string descricao = "Todos";
if (produto > 0)
{
descricao = Produto.ObterPorCodigo(produto).Descricao;
}
Paragraph filtroProduto = new Paragraph($"Produto: {descricao}");
filtroProduto.Font.SetStyle(Font.BOLD);
document.Add(filtroProduto);
string nome = "Todos";
if (cliente > 0)
{
nome = Cliente.ObterPorCodigo(cliente).Pessoa.Nome;
}
Paragraph filtroCliente = new Paragraph($"Cliente: {nome}");
filtroCliente.Font.SetStyle(Font.BOLD);
document.Add(filtroCliente);
document.Add(Chunk.NEWLINE);
PdfPTable tabela = new PdfPTable(6);
tabela.SetWidths(new float[] { 1, 1, 1, 1, 1, 1 });
tabela.TotalWidth = document.PageSize.Width - document.RightMargin - document.LeftMargin;
tabela.LockedWidth = true;
//.........这里部分代码省略.........
示例14: Genera
public static void Genera(string sourcePath,string content)
{
sourcePath = HttpContext.Current.Server.MapPath(sourcePath);
//定义一个Document,并设置页面大小为A4,竖向
iTextSharp.text.Document doc = new Document(PageSize.A4);
try
{
//写实例
PdfWriter.GetInstance(doc, new FileStream(sourcePath, FileMode.Create));
//打开document
doc.Open();
//载入字体
BaseFont baseFont = BaseFont.CreateFont(
"C:\\WINDOWS\\FONTS\\SIMHEI.TTF", //黑体
BaseFont.IDENTITY_H, //横向字体
BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, 9);
//写入一个段落, Paragraph
doc.Add(new Paragraph("第一段:" + content, font));
doc.Add(new Paragraph("这是第二段 !", font));
#region 图片
//以下代码用来添加图片,此时图片是做为背景加入到pdf文件当中的:
Stream inputImageStream = new FileStream(HttpContext.Current.Server.MapPath("~/images/logo.jpg"), FileMode.Open, FileAccess.Read, FileShare.Read);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImageStream);
image.SetAbsolutePosition(0, 0);
image.Alignment = iTextSharp.text.Image.UNDERLYING; //这里可以设定图片是做为背景还是做为元素添加到文件中
doc.Add(image);
#endregion
#region 其他元素
doc.Add(new Paragraph("Hello World"));
//另起一行。有几种办法建立一个段落,如:
Paragraph p1 = new Paragraph(new Chunk("This is my first paragraph.\n", FontFactory.GetFont(FontFactory.HELVETICA, 12)));
Paragraph p2 = new Paragraph(new Phrase("This is my second paragraph.", FontFactory.GetFont(FontFactory.HELVETICA, 12)));
Paragraph p3 = new Paragraph("This is my third paragraph.", FontFactory.GetFont(FontFactory.HELVETICA, 12));
//所有有些对象将被添加到段落中:
p1.Add("you can add string here\n\t");
p1.Add(new Chunk("you can add chunks \n")); p1.Add(new Phrase("or you can add phrases.\n"));
doc.Add(p1); doc.Add(p2); doc.Add(p3);
//创建了一个内容为“hello World”、红色、斜体、COURIER字体、尺寸20的一个块:
Chunk chunk = new Chunk("创建了一个内容为“hello World”、红色、斜体、COURIER字体、尺寸20的一个块", FontFactory.GetFont(FontFactory.COURIER, 20, iTextSharp.text.Font.COURIER, new iTextSharp.text.Color(255, 0, 0)));
doc.Add(chunk);
//如果你希望一些块有下划线或删除线,你可以通过改变字体风格简单做到:
Chunk chunk1 = new Chunk("This text is underlined", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.UNDEFINED));
Chunk chunk2 = new Chunk("This font is of type ITALIC | STRIKETHRU", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.ITALIC | iTextSharp.text.Font.STRIKETHRU));
//改变块的背景
chunk2.SetBackground(new iTextSharp.text.Color(0xFF, 0xFF, 0x00));
//上标/下标
chunk1.SetTextRise(5);
doc.Add(chunk1);
doc.Add(chunk2);
//外部链接示例:
Anchor anchor = new Anchor("website", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.UNDEFINED, new iTextSharp.text.Color(0, 0, 255)));
anchor.Reference = "http://itextsharp.sourceforge.net";
anchor.Name = "website";
//内部链接示例:
Anchor anchor1 = new Anchor("This is an internal link\n\n");
anchor1.Name = "link1";
Anchor anchor2 = new Anchor("Click here to jump to the internal link\n\f");
anchor2.Reference = "#link1";
doc.Add(anchor); doc.Add(anchor1); doc.Add(anchor2);
//排序列表示例:
List list = new List(true, 20);
list.Add(new ListItem("First line"));
list.Add(new ListItem("The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?"));
list.Add(new ListItem("Third line"));
doc.Add(list);
//文本注释:
Annotation a = new Annotation("authors", "Maybe its because I wanted to be an author myself that I wrote iText.");
doc.Add(a);
//包含页码没有任何边框的页脚。
iTextSharp.text.HeaderFooter footer = new iTextSharp.text.HeaderFooter(new Phrase("This is page: "), true);
footer.Border = iTextSharp.text.Rectangle.NO_BORDER;
doc.Footer = footer;
//Chapter对象和Section对象自动构建一个树:
iTextSharp.text.Font f1 = new iTextSharp.text.Font();
f1.SetStyle(iTextSharp.text.Font.BOLD);
Paragraph cTitle = new Paragraph("This is chapter 1", f1);
Chapter chapter = new Chapter(cTitle, 1);
Paragraph sTitle = new Paragraph("This is section 1 in chapter 1", f1);
Section section = chapter.AddSection(sTitle, 1);
doc.Add(chapter);
//构建了一个简单的表:
Table aTable = new Table(4, 4);
aTable.AutoFillEmptyCells = true;
aTable.AddCell("2.2", new System.Drawing.Point(2, 2));
aTable.AddCell("3.3", new System.Drawing.Point(3, 3));
aTable.AddCell("2.1", new System.Drawing.Point(2, 1));
aTable.AddCell("1.3", new System.Drawing.Point(1, 3));
//.........这里部分代码省略.........
示例15: CreatePdf
public static void CreatePdf(string path)
{
Document myDoc = new Document(PageSize.A4.Rotate());
iTextSharp.text.pdf.PdfWriter.GetInstance(myDoc, new FileStream(path, FileMode.Create));
myDoc.Open();
PdfPTable table = new PdfPTable(5);
table.TotalWidth = 800f;
table.LockedWidth = true;
Font font = new Font(Font.FontFamily.COURIER, 20f, 1);
font.SetStyle("bold");
PdfPCell cell = new PdfPCell(new Phrase("Aggregated Sales Report", font));
cell.Colspan = 5;
cell.Indent = 50;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Padding = 5;
cell.BackgroundColor = new BaseColor(229, 228, 226);
table.AddCell(cell);
using (SupermarketDBContext sqlContext = new SupermarketDBContext())
{
//MAGIC QUERRY
//var salesRep = ( from sr in sqlContext.SalesReports
// join p in sqlContext.Products
// on sr.ProductId equals p.ProductId
// select new { sr, p.ProductName }).Distinct().GroupBy(x => x.sr.ReportDate);
var salesReports = sqlContext.SalesReports.GroupBy(x => x.ReportDate).ToList();
decimal grandTotal = 0m;
foreach (var reports in salesReports)
{
string format = "dd-MMM-yyyy";
var date = reports.First().ReportDate.ToString(format);
PdfPCell dateCell = new PdfPCell(
new Phrase(date, font));
dateCell.Colspan = 5;
dateCell.Indent = 50;
dateCell.HorizontalAlignment = Element.ALIGN_CENTER;
dateCell.Padding = 5;
dateCell.BackgroundColor = new BaseColor(229, 228, 226);
table.AddCell(dateCell);
foreach (var report in reports)
{
string productId = report.Product.ProductName.ToString();
string quantity = report.Quantity.ToString();
string unitPrice = report.UnitPrice.ToString();
string location = report.Location.ToString();
string sum = report.Sum.ToString();
table.AddCell(productId);
table.AddCell(quantity);
table.AddCell(unitPrice);
table.AddCell(location);
table.AddCell(sum);
}
PdfPCell footrCell = new PdfPCell(new Phrase("Total sum for " + date));
footrCell.Colspan = 4;
footrCell.Indent = 50;
footrCell.HorizontalAlignment = Element.ALIGN_RIGHT;
footrCell.Padding = 5;
table.AddCell(footrCell);
//PdfPCell totalCell = new PdfPCell();
decimal reportSum = reports.Sum(x => x.Sum);
grandTotal += reportSum;
table.AddCell(reportSum.ToString());
}
PdfPCell grandFootrCell = new PdfPCell(new Phrase("Grand total:"));
grandFootrCell.Colspan = 4;
grandFootrCell.Indent = 50;
grandFootrCell.HorizontalAlignment = Element.ALIGN_RIGHT;
grandFootrCell.Padding = 5;
table.AddCell(grandFootrCell);
table.AddCell(new PdfPCell(new Phrase(grandTotal.ToString(),font)));
myDoc.Add(table);
}
myDoc.Close();
}