大家好,请问一下,怎样对用ITextSharp生成的PDF文档进行样式控制,
比如说:对某一行的背景颜色进行控制,能不能对某一行的背景颜色进行自己设置。
还有就是怎么样增加PDF的文章标题,用 document.AddTitle("xxxxxx");
document.AddSubject("this is a subject");好像都没看到出现。说明:我是用一个GRIDVIEW传进去,GRIDVIEW的行颜色我已经改变,但生成PDF时,却不显示。然后table一行一行的增加。最后 document.Add(table);
谢谢~~~
比如说:对某一行的背景颜色进行控制,能不能对某一行的背景颜色进行自己设置。
还有就是怎么样增加PDF的文章标题,用 document.AddTitle("xxxxxx");
document.AddSubject("this is a subject");好像都没看到出现。说明:我是用一个GRIDVIEW传进去,GRIDVIEW的行颜色我已经改变,但生成PDF时,却不显示。然后table一行一行的增加。最后 document.Add(table);
谢谢~~~
if (pdfInfo["printStyle"].ToString() == "纵向")
{
document = new Document(PageSize.A4, Convert.ToSingle(pdfInfo["left"]), Convert.ToSingle(pdfInfo["right"]), Convert.ToSingle(pdfInfo["top"]), Convert.ToSingle(pdfInfo["bottom"]));
}
else
{
document = new Document(PageSize.A4.Rotate(), Convert.ToSingle(pdfInfo["left"]), Convert.ToSingle(pdfInfo["right"]), Convert.ToSingle(pdfInfo["top"]), Convert.ToSingle(pdfInfo["bottom"]));
} //新建PDF文件夹
if (!Directory.Exists(path + @"\PDF"))
{
Directory.CreateDirectory(path);
} PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(path+@"\PDF\"+title+".pdf", FileMode.Create)); //文件写入流
BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); //字体基础,固定的,也是中文支持的前提
Font fontChinese = new Font(bfChinese, 9, Font.NORMAL, new Color(0, 0, 0)); //字体大小,样式,颜色 Font fontHeader = new Font(bfChinese, Convert.ToInt32(pdfInfo["headerFontSize"]), Convert.ToInt32(pdfInfo["headerFontStyle"]), new Color(System.Drawing.ColorTranslator.FromHtml(pdfInfo["headerFontColor"].ToString()))); //页眉字体样式
Font fontFooter = new Font(bfChinese, Convert.ToInt32(pdfInfo["footerFontSize"]), Convert.ToInt32(pdfInfo["footerFontStyle"]), new Color(System.Drawing.ColorTranslator.FromHtml(pdfInfo["footerFontColor"].ToString()))); //页脚字体样式 Font fontTitle = new Font(bfChinese, 15, Font.BOLD, new Color(0, 0, 0)); //报表标题字体样式
Font fontReTitle = new Font(bfChinese, Convert.ToInt32(pdfInfo["tableTitleFontSize"]), Convert.ToInt32(pdfInfo["tableTitleFontStyle"]), new Color(System.Drawing.ColorTranslator.FromHtml(pdfInfo["tableTitleFontColor"].ToString()))); //子报表标题字体样式
Font fontTableHeader = new Font(bfChinese, Convert.ToInt32(pdfInfo["tableHeadFontSize"]), Convert.ToInt32(pdfInfo["tableHeadFontStyle"]), new Color(System.Drawing.ColorTranslator.FromHtml(pdfInfo["tableHeadFontColor"].ToString())));//表标题字体样式
Font fontReport = new Font(bfChinese, Convert.ToInt32(pdfInfo["reportFontSize"]), Convert.ToInt32(pdfInfo["reportFontStyle"]), new Color(System.Drawing.ColorTranslator.FromHtml(pdfInfo["reportFontColor"].ToString()))); //页眉
HeaderFooter header = new HeaderFooter(new Phrase(pdfInfo["headerContent"].ToString() == "none" ? "" : pdfInfo["headerContent"].ToString(), fontHeader), false); header.Border = iTextSharp.text.Rectangle.NO_BORDER; header.Alignment = Convert.ToInt32(pdfInfo["headerTextAlign"]); document.Header = header; //页脚
HeaderFooter footer = new HeaderFooter(new Phrase(pdfInfo["footerSizeBeginContent"].ToString()=="none"?"":pdfInfo["footerSizeBeginContent"].ToString(), fontFooter), new Phrase(pdfInfo["footerSizeEndContent"].ToString()=="none"?"":pdfInfo["footerSizeEndContent"].ToString(), fontFooter)); footer.Border = iTextSharp.text.Rectangle.NO_BORDER; footer.Alignment = Convert.ToInt32(pdfInfo["footerTextAlign"]);
document.Footer = footer; document.Open(); //打开文档开始写内容,所有整体PDF文档的配置内容及页眉页脚都要在OPEN之前完成 Paragraph pTitle = new Paragraph(title, fontTitle); //加入Title,这里放到一个段落里,有利于格式的控制
pTitle.Alignment = Element.ALIGN_CENTER;
document.Add(pTitle); Paragraph pDate = new Paragraph("日期:" + date, fontChinese); //加入日期
pDate.Alignment = Element.ALIGN_RIGHT;
document.Add(pDate);
#endregion
好像需要在文档关闭时
public static void ConvertGrdiViewToPDF_EachPageHeader(GridView pobjGrdv, string PDFFileName, string FontPath, float FontSize)
{
tools tl = new tools();
string strFileName = PDFFileName + ".pdf"; Document document = new Document(PageSize.B4, 15, 15, 15, 15);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(HttpContext.Current.Server.MapPath(strFileName), FileMode.Create));
BaseFont baseFont = BaseFont.CreateFont(FontPath,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); Font font = new Font(baseFont, FontSize); HeaderFooter footer = new HeaderFooter(new Phrase("------ ", font), new Phrase(" -------", font));
footer.Border = Rectangle.NO_BORDER;
footer.Alignment = Rectangle.ALIGN_CENTER;
document.Footer = footer;
document.Open();
PdfPTable table = new PdfPTable(pobjGrdv.Columns.Count);
pobjGrdv.AllowPaging = false;
table.DefaultCell.BackgroundColor = iTextSharp.text.Color.LIGHT_GRAY;
for (int j = 0; j < pobjGrdv.Columns.Count; j++)
{
PdfPCell headercell = new PdfPCell(new Phrase(pobjGrdv.HeaderRow.Cells[j].Text, font));
headercell.BackgroundColor=Color.RED;
table.AddCell(headercell);
// table.AddCell(new Phrase(pobjGrdv.HeaderRow.Cells[j].Text, font));
}
try
{
table.HeaderRows = 1;
//table.DefaultCell.BackgroundColor = Color.WHITE;
Color currentCellBackgroundColor = Color.WHITE;
for (int i = 0; i < pobjGrdv.Rows.Count; i++)
{ for (int j = 0; j < pobjGrdv.Columns.Count; j++)
{ if (j == 1)
{
string tempstring = tl.decryptMeR(pobjGrdv.Rows[i].Cells[1].Text);
string[] nums = tempstring.Split('\0');
string temp = nums[0]; string num;
if (temp.Length == 16)
{
num = temp.Substring(0, 4) + "-" + temp.Substring(4, 4) + "-" + temp.Substring(8, 4) + "-" + temp.Substring(12, 4);
}
else
{
num = temp.Substring(0, 4) + "-" + temp.Substring(4, 6) + "-" + temp.Substring(9, 5);
} PdfPCell mycell = new PdfPCell(new Phrase(temp, font));
if(i<1)
{
mycell.BackgroundColor = Color.WHITE;
table.AddCell(mycell);
}
else if (i < 2)
{
if (pobjGrdv.Rows[i - 1].Cells[2].Text.ToString() != pobjGrdv.Rows[i].Cells[2].Text.ToString())
{
currentCellBackgroundColor = Color.LIGHT_GRAY;
mycell.BackgroundColor = currentCellBackgroundColor;
table.AddCell(mycell);
}
else
{
mycell.BackgroundColor = currentCellBackgroundColor;
table.AddCell(mycell);
}
}
else
{
mycell.BackgroundColor = currentCellBackgroundColor;
table.AddCell(mycell);
}
}
else
{
PdfPCell mycell2 = new PdfPCell(new Phrase(pobjGrdv.Rows[i].Cells[j].Text, font));
if(i<1)
{
mycell2.BackgroundColor = Color.WHITE;
table.AddCell(mycell2);
}
else if (i < 2)
{
if (pobjGrdv.Rows[i - 1].Cells[2].Text.ToString() != pobjGrdv.Rows[i].Cells[2].Text.ToString())
{
currentCellBackgroundColor = Color.LIGHT_GRAY;
mycell2.BackgroundColor = currentCellBackgroundColor;
table.AddCell(mycell2);
}
else
{
mycell2.BackgroundColor = currentCellBackgroundColor;
table.AddCell(mycell2);
}
}
else
{
mycell2.BackgroundColor = currentCellBackgroundColor;
table.AddCell(mycell2);
}
} }
if (i >0)
{ if (pobjGrdv.Rows[i].Cells[2].Text.ToString() != pobjGrdv.Rows[i+1].Cells[2].Text.ToString())
{
if (currentCellBackgroundColor == Color.WHITE)
{
currentCellBackgroundColor = Color.LIGHT_GRAY;
}
else
{
currentCellBackgroundColor = Color.WHITE; }
}
} }
}
catch (Exception e)
{
string E = e.ToString();
// table = null;
}
finally
{
String tempReportCriteria = string.Empty;
//string tempReportCriteria;
if (sType.Equals("date"))
{
tempReportCriteria = "Date (From): " + sStartDate + " Date (To):" + sEndDate + "\n\t\n\t";
}
else if (sType.Equals("ref"))
{
tempReportCriteria = "Reference No.: " + sRef + "\n\t\n\t";
}
else if (sType.Equals("cc"))
{
tempReportCriteria = "Credit Card No.:" + sCC + "\n\t\n\t";
}
Paragraph ph1 = new Paragraph();
Chunk chk=new Chunk("Online Redemption and Lucky Draw");
ph1.Add(chk);
ph1.Alignment = Element.ALIGN_CENTER;
document.Add(ph1); Paragraph ph2 = new Paragraph();
Chunk chk2 = new Chunk(tempReportCriteria);
ph2.Add(chk2);
ph2.Alignment = Element.ALIGN_CENTER;
document.Add(ph2);
document.Add(table); document.Close();
writer.Close(); String FullFileName = System.Web.HttpContext.Current.Server.MapPath(strFileName);
FileInfo DownloadFile = new FileInfo(FullFileName);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = false;
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
}