生成pdf的重点了 先看图片吧 可能会出现的情况 :
正常情况下文章和图片的排列
当文章内容大于左栏的时候
图片在左 文章在右
正文图片大于左栏甚至是右栏的情况
文字在左 图片在右
下面贴代码 代码有点多 可能某些部分还不够优化:
int pageNum = 0;//根据是否分栏添加页数 int newspagenum = 0; for (int i = 0; i < dt.Rows.Count; i++)//将所有的内容和版面进行输出 { document.NewPage();//新建一个页面 status = 0; if (dt.Rows[i]["IntroTitle"].ToString() != "")//在左栏写入引题 { ct.AddText(new Paragraph(dt.Rows[i]["IntroTitle"].ToString(), BaseFontAndSize(0, 17, "9f9fa0"))); ct.Indent = 20; ct.SetSimpleColumn(left[column], 100, rightwidth[column], 760, 16, Element.ALIGN_CENTER); ct.Go(); } ct.AddText(new Paragraph(dt.Rows[i]["Title"].ToString() + "/n", BaseFontAndSize(0, 22, "000000")));//在左栏写入标题 ct.Indent = 20; ct.SetSimpleColumn(left[column], 100, rightwidth[column], 720, 16, Element.ALIGN_CENTER); ct.Go(); int subHeight = 0; if (dt.Rows[i]["SubTitle"].ToString() != "") { int subCount = dt.Rows[i]["SubTitle"].ToString().Length / 80;//判断字符串的长度 ct.AddText(new Paragraph(dt.Rows[i]["SubTitle"].ToString(), BaseFontAndSize(0, 15, "9f9fa0")));//在左栏写入副题 ct.SetSimpleColumn(left[column], 60, rightwidth[column], 675, 16, Element.ALIGN_CENTER); ct.Go(); subHeight = (subCount + 1) * 50;//加高 } ct.AddText(new Paragraph(dt.Rows[i]["Author"].ToString(), BaseFontAndSize(0, 10, "000000")));//在左栏写入作者 ct.SetSimpleColumn(left[column], 60, rightwidth[column], 690 - subHeight, 16, Element.ALIGN_CENTER); ct.Go(); int imageHeight = 0; int imageTwo = 0; bool banmian = false; bool boolNewPage = false; if (dt.Rows[i]["Image"].ToString() != "")//在左栏写入正文图片 { string[] image = dt.Rows[i]["Image"].ToString().Split(','); for (int j = 0; j < image.Length - 1; j++) { iTextSharp.text.Image imgzhengwen = iTextSharp.text.Image.GetInstance(image[j]); if (imgzhengwen.Width > bankai || imgzhengwen.Height > 690 - subHeight) { imgzhengwen.ScalePercent(60); } imageHeight = (int)imgzhengwen.ScaledHeight; imgzhengwen.Alignment = iTextSharp.text.Image.ALIGN_CENTER; if (banmian) { if (imageHeight + imageTwo > 720 - subHeight) { if (column == 0) { column = 1; } else { column = 0; } if (column == 0) { NewPageNum(lift, cb, bf, pageNum, newspagenum, i); document.NewPage(); newsPageNum += 2; NewPageNum(lift, cb, bf, pageNum, newspagenum, i); } imageTwo = 0; cb.AddImage(imgzhengwen, imgzhengwen.ScaledWidth, 0, 0, imgzhengwen.ScaledHeight, (bankai - imgzhengwen.ScaledWidth) / 2 + 10 + imgWidth[column], 720 - imgzhengwen.ScaledHeight); imageTwo += imageHeight - 20; banmian = false; pageBool = true; } else { cb.AddImage(imgzhengwen, imgzhengwen.ScaledWidth, 0, 0, imgzhengwen.ScaledHeight, (bankai - imgzhengwen.ScaledWidth) / 2 + 10 + imgWidth[column], 720 - imgzhengwen.ScaledHeight - subHeight - imageTwo); imageTwo += imageHeight + 20; } } else { if (imageHeight + imageTwo > 656 - subHeight) { if (boolNewPage) { if (pageBool) { NewPageNum(lift, cb, bf, pageNum, newspagenum, i); } else { NewPageNum(lift, cb, bf, -1, newspagenum, i); } document.NewPage(); if (i == 0) { newsPageNum = 1; } else { newsPageNum += 2; } NewPageNum(lift, cb, bf, pageNum, newspagenum, i); } if (column == 0) { column = 1; } else { column = 0; } if (banmian) { } else { imageTwo = 0; } cb.AddImage(imgzhengwen, imgzhengwen.ScaledWidth, 0, 0, imgzhengwen.ScaledHeight, (bankai - imgzhengwen.ScaledWidth) / 2 + 10 + imgWidth[column], 720 - imgzhengwen.ScaledHeight - subHeight - imageTwo); banmian = true; imageTwo = 0; imageTwo += imageHeight + 20; } else { if (boolNewPage) { } else { if (column == 1) { boolNewPage = true; iTextSharp.text.Image img6 = NewBanMianImages(dt, cb, c, bf, column, i);//添加版面图 } } cb.AddImage(imgzhengwen, imgzhengwen.ScaledWidth, 0, 0, imgzhengwen.ScaledHeight, (bankai - imgzhengwen.ScaledWidth) / 2 + 10 + imgWidth[column], 660 - imgzhengwen.ScaledHeight - subHeight - imageTwo); imageTwo += imageHeight + 20; } } } } string kongge = " "; float konggechangdu = bf.GetWidthPoint(kongge, (float)12);//得到-的宽度 float banmianchangdu = bf.GetWidthPoint(dt.Rows[i]["banmianming"].ToString(),(float)12); StringBuilder stringBuilder = new StringBuilder(); for (int j = 0; j < (420-banmianchangdu)/konggechangdu; j++) { stringBuilder.Append(kongge); } ct.AddText(new Paragraph("/r" + dt.Rows[i]["Content"].ToString(), BaseFontAndSize(2, 11, "000000")));//在左栏写入正文 ct.AddText(new Paragraph("/n/r" + stringBuilder + dt.Rows[i]["banmianming"].ToString(), BaseFontAndSize(0, 11, "6c423c")));//在左栏写入版面名称 bool newbool = false; float[] height = { 660 - subHeight - imageTwo, 720 }; bool newtwobool = false; int newpagenum = 0; while ((status & ColumnText.NO_MORE_TEXT) == 0)//对内容进行循环定位 { if (newbool) { ct.SetSimpleColumn(left[column], 60, rightwidth[column], height[column], 16, Element.ALIGN_JUSTIFIED); status = ct.Go(); } else { if (newtwobool) { ct.SetSimpleColumn(left[column], 60, rightwidth[column], 720, 16, Element.ALIGN_JUSTIFIED); status = ct.Go(); } else { if (banmian) { ct.SetSimpleColumn(left[column], 60, rightwidth[column], 720 - subHeight - imageTwo, 16, Element.ALIGN_JUSTIFIED); status = ct.Go(); } else { ct.SetSimpleColumn(left[column], 60, rightwidth[column], 660 - subHeight - imageTwo, 16, Element.ALIGN_JUSTIFIED); status = ct.Go(); } } if (banmian) { } else { if (boolNewPage) { } else { if (column == 1) { iTextSharp.text.Image img6 = NewBanMianImages(dt, cb, c, bf, column, i);//添加版面图 NewPageNum(lift, cb, bf, pageNum, newspagenum, i);//添加页码 newbool = true; } } } } if ((status & ColumnText.NO_MORE_COLUMN) != 0)//判断内容是否需要添加到第二栏 { column++; newbool = true; newpagenum++; if (column > 1)//如果内容超过一页则新建一个页面继续进行添加 { cb.Stroke(); NewPageNum(lift, cb, bf, pageNum, newspagenum, i); document.NewPage(); column = 0; newspagenum += 2; newbool = false; newtwobool = true; } } } if (newbool) { } else { if (i + 1 == dt.Rows.Count) { }//到最后一页的时候不需要添加版面图 else { iTextSharp.text.Image img6 = NewBanMianImages(dt, cb, c, bf, column, i + 1); } } cb.Stroke(); NewPageNum(lift, cb, bf, pageNum, newspagenum, i); if (newbool) { if (newpagenum == 3) { pageNum = 0; } else if (newpagenum > 3) { pageNum++; } } } document.Close();//关闭document column = 0; newsPageNum = 0; } private static void NewPageNum(string lift, PdfContentByte cb, BaseFont bf, int pageNum, int newspagenum, int i)//添加页码 { iTextSharp.text.Image imglift = iTextSharp.text.Image.GetInstance(lift); imglift.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE; cb.AddImage(imglift, 21, 0, 0, 842, 30, -22); cb.Stroke(); cb.BeginText(); cb.SetFontAndSize(bf, 11); cb.SetTextMatrix(35, 45); cb.ShowText((i * 2 + 2 + pageNum + newspagenum+newsPageNum).ToString()); cb.EndText(); cb.Stroke(); cb.BeginText(); cb.SetFontAndSize(bf, 11); cb.SetTextMatrix(1146, 45); cb.ShowText((i * 2 + 3 + pageNum + newspagenum+newsPageNum).ToString());//右侧页码 cb.EndText(); } private static iTextSharp.text.Image NewBanMianImages(DataTable dt, PdfContentByte cb,Color c,BaseFont bf,int column, int i)//添加版面 { iTextSharp.text.Image img6 = iTextSharp.text.Image.GetInstance(dt.Rows[i]["LayoutPic"].ToString());//第一页右侧的版面图 img6.ScalePercent(70); img6.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE; cb.AddImage(img6, img6.ScaledWidth, 0, 0, img6.ScaledHeight, (bankai - img6.ScaledWidth) / 2 + imageWidth[column], bangao - img6.ScaledHeight / 2 + 20); cb.SetLineWidth(0f);//给版面图添加边线 cb.MoveTo((bankai - img6.ScaledWidth) / 2 + imageWidth[column], bangao - img6.ScaledHeight / 2 + img6.ScaledHeight + 20); cb.LineTo((bankai - img6.ScaledWidth) / 2 + imageWidth[column] + img6.ScaledWidth, bangao - img6.ScaledHeight / 2 + img6.ScaledHeight + 20);//上边框线 cb.MoveTo((bankai - img6.ScaledWidth) / 2 + imageWidth[column], bangao - img6.ScaledHeight / 2 + img6.ScaledHeight + 20); cb.LineTo((bankai - img6.ScaledWidth) / 2 + imageWidth[column], bangao - img6.ScaledHeight / 2 + 20);//左边框线 cb.MoveTo((bankai - img6.ScaledWidth) / 2 + imageWidth[column] + img6.ScaledWidth, bangao - img6.ScaledHeight / 2 + img6.ScaledHeight + 20); cb.LineTo((bankai - img6.ScaledWidth) / 2 + imageWidth[column] + img6.ScaledWidth, bangao - img6.ScaledHeight / 2 + 20);//右边框线 cb.MoveTo((bankai - img6.ScaledWidth) / 2 + imageWidth[column], bangao - img6.ScaledHeight / 2 + 20); cb.LineTo((bankai - img6.ScaledWidth) / 2 + imageWidth[column] + img6.ScaledWidth, bangao - img6.ScaledHeight / 2 + 20);//下边框线 cb.Stroke(); cb.BeginText(); cb.SetFontAndSize(bf, 11); cb.SetColorFill(c); cb.SetTextMatrix((bankai - img6.ScaledWidth) / 2 + imageWidth[column] + 200, bangao - img6.ScaledHeight / 2 - 20); cb.ShowText(dt.Rows[i]["banmianming"].ToString());//第一页右侧添加版面名称 cb.EndText(); cb.Stroke(); return img6; } private Font BaseFontAndSize(int type, int size, string color)//设置文字的大小类型和颜色 { BaseFont bf; Font font = null; Color c = new Color(int.Parse(color, System.Globalization.NumberStyles.AllowHexSpecifier)); switch (type) { case 0: bf = BaseFont.CreateFont(@"c:/windows/fonts/SIMLI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//字体:黑体 font = new Font(bf, size, Font.NORMAL, c); break; case 1: bf = BaseFont.CreateFont(@"c:/windows/fonts/STKAITI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//字体:楷体 font = new Font(bf, size, Font.NORMAL, c); break; case 2: bf = BaseFont.CreateFont(@"c:/windows/fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//字体:宋体 font = new Font(bf, size, Font.NORMAL, c); break; } return font; }