iTextSharp.dll生成pdf 实战实例

47 篇文章 0 订阅
12 篇文章 1 订阅

http://blog.csdn.net/jiangyu912/article/details/4969376


最近忙了两个星期的任务了     iTextSharp.dll是个开源的用于生成pdf的类库了  项目中我们只需要引用就可以了    说实话确实很好用   因为自己的英文水平实在有限    所以没去看API了   找资料上费了不少功夫    因为网上的资料大部分都只是介绍一些基础的应用   所以很多地方还得靠自己慢慢来摸索了    现在将自己生成的pdf代码奉献给大家   希望对有用到的朋友一些帮助   给的代码中有些属性和方法不明白的需要大家自己去找资料参考了    代码里面就不解释了   

 

  代码量有一些   可能还不是很完善  毕竟自己的能力还是很有限   所以代码部分还有一些需要优化    不过生成是没问题的   就当给大家参考吧   当然有高人提些意见还是很不错的  

 

   先看图片吧 我需要生成的pdf是什么样的:

 

 

 

 

 

[c-sharp:nogutter] view plain copy
  1.    
  2.   
  3. using iTextSharp.text;  
  4. using iTextSharp.text.pdf;  
  5. using System.IO;  
  6. using System.Text;  
  7. using System.Xml;  
  8.   
  9. private static float bankai;//页面宽度的一半  
  10.         private static float bangao;//页面高度的一半  
  11.         private static float[] left = { 75, 1100 };  
  12.         private static float[] rightwidth = { 500, 675 };  
  13.         private static float[] imageWidth = { 600, 0 };  
  14.         private static float[] imgWidth = { 0, 600 };  
  15.         private static bool boo = true;//当目录数大于10则进行分栏  
  16.         private static int column = 0;//0为左边1为右边  
  17.   
  18. public void CreatePDF(DataTable dt,string imagePath,string pdfPath)//第一个参数是是生成pdf所需要的内容,第二个是生成pdf所需要的图片路径,第三个是生成pdf后存放的路径  
  19.         {  
  20.             int status = 1;  
  21.             //获取图片路径  
  22.             string lift = imagePath+ @"/lift.jpg";  
  23.             string right = imagePath+ @"/right.jpg";  
  24.             string top1 = imagePath + @"/top1.jpg";  
  25.             string top2 = imagePath + @"/top2.jpg";  
  26.             Document document = new Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);//定义pdf大小,设置上下左右边距  
  27.             Rectangle rect = new Rectangle(1190, 841);//设置整个版面的宽度和高度  
  28.             document.SetPageSize(rect);  
  29.             bankai = document.PageSize.Width / 2;  
  30.             bangao = document.PageSize.Height / 2;  
  31.             PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfPath + DateTime.Now.ToString("yyyyMMddhhmmss") + ".pdf", FileMode.Create));//生成pdf路径,创建文件流  
  32.             document.Open();  
  33.             PdfContentByte cb = writer.DirectContent;  
  34.             ColumnText ct = new ColumnText(cb);  
  35.             iTextSharp.text.Image img4 = iTextSharp.text.Image.GetInstance(top1);//定位背景图片1  
  36.             img4.ScalePercent(48);  
  37.             cb.AddImage(img4, bankai, 0, 0, img4.ScaledHeight, 0, 715);  
  38.             iTextSharp.text.Image img5 = iTextSharp.text.Image.GetInstance(top2);//定位背景图片2  
  39.             img5.ScalePercent(48);  
  40.             cb.AddImage(img5, img5.ScaledWidth, 0, 0, img5.ScaledHeight, 0, 683);  
  41.             Color c = new Color(int.Parse("6c423c", System.Globalization.NumberStyles.AllowHexSpecifier));//设置颜色  
  42.             BaseFont bf = BaseFont.CreateFont(@"c:/windows/fonts/SIMLI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//设置字体  
  43.             cb.Stroke();  
  44.             cb.BeginText();  
  45.             cb.SetFontAndSize(bf, 20);  
  46.             cb.SetColorFill(c);  
  47.             cb.SetTextMatrix(140, 610);  
  48.             cb.ShowText("卷一");//定位卷一  
  49.             cb.EndText();  
  50.             int pagenum = 0;  
  51.             bool boo = true;//当目录数大于10则进行分栏  
  52.             bool bo = true;  
  53.             int newpage = 0;  
  54.             int newpageHeight = 1;  
  55.             int Count = 0;  
  56.             int PageIndex = 2;  
  57.             bool pageBool = false;  
  58.             //对标题数目进行判断  当目录大于左栏的时候  定位于右栏   当标题大于右栏时新建一个页面  依次循环  
  59.             for (int i = 0; i < dt.Rows.Count; i++)//当目录数目过大,对目录进行分页  
  60.             {  
  61.                 if (i != 0)  
  62.                 {  
  63.                     if (i % 10 == 0)//每一栏为10个目录  
  64.                     {  
  65.                         if (bo == false)//当目录大于一页的时候新建一个页面  
  66.                         {  
  67.                             boo = true;  
  68.                             cb.Stroke();  
  69.                             document.NewPage();  
  70.                             newpage = -i;  
  71.                             newpageHeight = 70;  
  72.                             iTextSharp.text.Image imglift = iTextSharp.text.Image.GetInstance(lift);//定位左边的图片  
  73.                             imglift.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;  
  74.                             cb.AddImage(imglift, 21, 0, 0, 842, 30, -22);  
  75.                         }  
  76.                         else  
  77.                         {  
  78.                             boo = false;  
  79.                             newpage = -i;  
  80.                         }  
  81.                     }  
  82.                 }  
  83.                 bo = boo;  
  84.                 //因为不确定标题的长度为多少 所以不能固定死  而且因为pdf里面对字体的长度有限制比如-和字符在里面的显示长度是不同的   所以根据长度来显示肯定不行了   
  85.                 //因此只能判断他们在pdf中的像素来确定他们的长度  我的办法是先获得-的像素 然后获得标题的像素  然后用总像素减去标题的像素和页码像素度再除以-的像素  
  86.                 //就得到-的个数 然后再添加到标题和页码中就可以了  
  87.                 string fuhao = "-";  
  88.                 float fuhaochangdu = bf.GetWidthPoint(fuhao, (float)12);//得到-的像素  
  89.                 float biaotichangdu;  
  90.                 if (dt.Rows[i]["Title"].ToString().Length > 26)//判断目录长度是否大于30  
  91.                 {  
  92.                     string jiequTitle = dt.Rows[i]["Title"].ToString().Substring(0, 26) + "……";//大于30加......  
  93.                     biaotichangdu = bf.GetWidthPoint(jiequTitle, (float)12);//获得标题的像素  
  94.                     cb.Stroke();  
  95.                     cb.BeginText();  
  96.                     cb.SetFontAndSize(bf, 12);  
  97.                     cb.SetColorFill(c);  
  98.                     if (!bo) { cb.SetTextMatrix(140 + bankai, 530 + newpageHeight - ((i - 1 + newpage) * 50)); }//对标题进行定位,标题位于左栏  
  99.                     else { cb.SetTextMatrix(140, 530 + newpageHeight - ((i - 1 + newpage) * 50)); }//当标题大于左栏的时候定位于右栏  
  100.                     cb.ShowText(jiequTitle);  
  101.                     cb.EndText();  
  102.                 }  
  103.                 else//如果标题长度大于30则添加......  
  104.                 {  
  105.                     biaotichangdu = bf.GetWidthPoint(dt.Rows[i]["Title"].ToString(), (float)12);  
  106.                     cb.Stroke();  
  107.                     cb.BeginText();  
  108.                     cb.SetFontAndSize(bf, 12);  
  109.                     cb.SetColorFill(c);  
  110.                     if (!bo) { cb.SetTextMatrix(140 + bankai, 530 + newpageHeight - ((i - 1 + newpage) * 50)); }//对标题进行定位  
  111.                     else { cb.SetTextMatrix(140, 530 + newpageHeight - ((i - 1 + newpage) * 50)); }  
  112.                     cb.ShowText(dt.Rows[i]["Title"].ToString());//给目录添加标题  
  113.                     string abc = dt.Rows[i]["Title"].ToString();  
  114.                     cb.EndText();  
  115.                 }  
  116.                 int changdu = (int)((bankai - 210 - biaotichangdu) / fuhaochangdu);//获得-的个数  
  117.                 StringBuilder sb = new StringBuilder();  
  118.                 for (int j = 0; j < changdu; j++)//判断标题与索引之间所需要的-  
  119.                 {  
  120.   
  121.                     sb.Append(fuhao);//依次添加-  
  122.                 }  
  123.                 cb.Stroke();  
  124.                 cb.BeginText();  
  125.                 cb.SetFontAndSize(bf, 12);  
  126.                 cb.SetColorFill(c);  
  127.                 if (!bo) { cb.SetTextMatrix(140 + biaotichangdu + bankai, 530 + newpageHeight - ((i - 1 + newpage) * 50)); }//左栏定位-  
  128.                 else { cb.SetTextMatrix(140 + biaotichangdu, 530 + newpageHeight - ((i - 1 + newpage) * 50)); }//右栏定位-  
  129.                 cb.ShowText(sb.ToString());//给目录中间添加-  
  130.                 cb.EndText();  
  131.                 int page = PDFPageNum(dt, i);//获得内容的页码  
  132.                 cb.Stroke();  
  133.                 cb.BeginText();  
  134.                 cb.SetFontAndSize(bf, 10);  
  135.                 cb.SetColorFill(c);  
  136.                 if (!bo) { cb.SetTextMatrix(bankai - 70 + bankai, 530 + newpageHeight - ((i - 1 + newpage) * 50)); }//左栏定位页码  
  137.                 else { cb.SetTextMatrix(bankai - 70, 530 + newpageHeight - ((i - 1 + newpage) * 50)); }//右栏定位页码  
  138.                 cb.ShowText((PageIndex).ToString());//显示页码  
  139.                 cb.EndText();  
  140.                 if (page == 1) { pagenum = 0; PageIndex += 2; }//给目录添加页码  
  141.                 else if (page == 2) { pagenum = 1; PageIndex += 3; }  
  142.                 else { pagenum = 1; PageIndex += page + 1; }//目录的页数  
  143.                 Color color = new Color(int.Parse("9f9fa0", System.Globalization.NumberStyles.AllowHexSpecifier));  
  144.                 cb.Stroke();  
  145.                 cb.BeginText();  
  146.                 cb.SetFontAndSize(bf, 10);  
  147.                 cb.SetColorFill(color);  
  148.                 if (!bo) { cb.SetTextMatrix(140 + bankai, 510 + newpageHeight - ((i - 1 + newpage) * 50)); }  
  149.                 else { cb.SetTextMatrix(140, 505 + newpageHeight - ((i - 1 + newpage) * 50)); }  
  150.                 cb.ShowText(dt.Rows[i]["banmianming"].ToString());//给目录添加版面名称  
  151.                 cb.EndText();  
  152.             }  
  153.             //因为我的pdf要求是先版面然后文章了   如果目录在左边的话版面就在右边了  第二页左边就为文章  右边又为版面   一次类推了  如果目录占到了右边  
  154.             //那么第二页左边为版面右边为文章了    
  155.             if (!boo)//判断目录是否生成新的页面  
  156.             {  
  157.                 column = 1;  
  158.                 pageBool = false;  
  159.             }  
  160.             else//没有生成页面则添加版面和页码  
  161.             {  
  162.                 iTextSharp.text.Image img6 = NewBanMianImages(dt, cb, c, bf, 0, 0);  
  163.                 cb.Stroke();  
  164.                 cb.BeginText();  
  165.                 cb.SetFontAndSize(bf, 11);  
  166.                 cb.SetTextMatrix(1147, 45);  
  167.                 cb.ShowText("1");  
  168.                 cb.EndText();  
  169.             }  

 

    后面将对这个目录进行补充   关于其中的int page = PDFPageNum(dt, i);//获得内容的页码  方法所要做的是什么

这篇介绍的是对前篇目录的补充也就是int page = PDFPageNum(dt, i);//获得内容的页码   这个方法里面的内容     这是我做pdf后遇到的最后一个问题了   因为页面内容是变化的   我不确定每篇文章到底有多大  所以肯定不能固定死了     当初问了公司里面的高手   给的意见就是弄一个控制台程序也就是exe程序     对每篇文章大小内容进行判断然后返回一个页码    个人感觉这样太麻烦了   估计效率上肯定也会有问题    想了很久之后想到了这个办法    弄一个方法  将每篇文章按照自己生成的格式来生成    不过我生成在内存中   然后对其进行判断   生成多页就返回它的页码

 

  1. //按照自己需要生成pdf的格式生成   对其页码进行判断  返回给目录进行添加  
  2.         private int PDFPageNum(DataTable dt, int i)//获得内容的页码  
  3.         {  
  4.             int pageNum = 1;  
  5.             Document document = new Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);  
  6.             Rectangle rect = new Rectangle(1190, 841);  
  7.             document.SetPageSize(rect);  
  8.             bankai = document.PageSize.Width / 2;//获得栏的宽度  
  9.             bangao = document.PageSize.Height / 2;//获得页面的半高  
  10.             PdfWriter writer = PdfWriter.GetInstance(document, new MemoryStream());//写入内存(重点就在这了)  
  11.             document.Open();  
  12.             PdfContentByte cb = writer.DirectContent;  
  13.             BaseFont bf = BaseFont.CreateFont(@"c:/windows/fonts/SIMLI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//设置字体  
  14.             ColumnText ct = new ColumnText(cb);  
  15.             if (dt.Rows[i]["IntroTitle"].ToString() != "")//在左栏写入引题  
  16.             {  
  17.                 ct.AddText(new Paragraph(dt.Rows[i]["IntroTitle"].ToString(), BaseFontAndSize(0, 17, "9f9fa0")));  
  18.                 ct.Indent = 20;  
  19.                 ct.SetSimpleColumn(50, 100, 560, 760, 16, Element.ALIGN_CENTER);  
  20.                 ct.Go();  
  21.             }  
  22.             ct.AddText(new Paragraph(dt.Rows[i]["Title"].ToString() + "/n", BaseFontAndSize(0, 22, "000000")));//在左栏写入标题  
  23.             ct.Indent = 20;  
  24.             ct.SetSimpleColumn(50, 100, 560, 720, 16, Element.ALIGN_CENTER);  
  25.             ct.Go();  
  26.             int subHeight = 0;  
  27.             if (dt.Rows[i]["SubTitle"].ToString() != "")  
  28.             {  
  29.                 int subCount = dt.Rows[i]["SubTitle"].ToString().Length / 80;//判断字符串的长度  
  30.                 ct.AddText(new Paragraph(dt.Rows[i]["SubTitle"].ToString(), BaseFontAndSize(0, 15, "9f9fa0")));//在左栏写入副题  
  31.                 ct.SetSimpleColumn(50, 60, 590, 675, 16, Element.ALIGN_CENTER);  
  32.                 ct.Go();  
  33.                 subHeight = (subCount + 1) * 50;//加高  
  34.             }  
  35.             ct.AddText(new Paragraph(dt.Rows[i]["Author"].ToString(), BaseFontAndSize(0, 10, "000000")));//在左栏写入作者  
  36.             ct.SetSimpleColumn(0, 60, 590, 690 - subHeight, 16, Element.ALIGN_CENTER);  
  37.             ct.Go();  
  38.             int imageHeight = 0;  
  39.             int imageTwo = 0;  
  40.             int column = 0;  
  41.             bool boolNewPage = false;  
  42.             bool banmian = false;  
  43.             int newspagenum = 0;  
  44.             //文章里面可能会有多张图片  需要对其进行判断  如果图片大于一页则需要新建一页进行添加  
  45.             if (dt.Rows[i]["Image"].ToString() != "")//在左栏写入正文图片  
  46.             {  
  47.                 string[] image = dt.Rows[i]["Image"].ToString().Split(',');//获取多张图片路径  
  48.                 for (int j = 0; j < image.Length - 1; j++)//循环取得每张图片路径  
  49.                 {  
  50.                     iTextSharp.text.Image imgzhengwen = iTextSharp.text.Image.GetInstance(image[j]);//添加图片  
  51.                     if (imgzhengwen.Width > bankai || imgzhengwen.Height > 690 - subHeight)//如果图片的宽和高大于栏的宽和高则对图片进行缩略,我这固定死了   正确的话应该是按照图片的实际高度进行缩略  
  52.                     {  
  53.                         imgzhengwen.ScalePercent(60);  
  54.                     }  
  55.                     imageHeight = (int)imgzhengwen.ScaledHeight;//获得图片的实际高度,因为图片可能被缩略了  
  56.                     imgzhengwen.Alignment = iTextSharp.text.Image.ALIGN_CENTER;//图片居中,应该没什么用  因为采用的是绝对定位肯定会居中了    
  57.                     if (banmian)//判断文章是否产生多栏  
  58.                     {  
  59.                         if (imageHeight + imageTwo > 720 - subHeight)//多张图片  如果图片大于右栏则新建一个页面继续添加  
  60.                         {  
  61.                             //因为不确定文章的大小 column为0的时候定位在左栏   column为1的时候定位在右栏  
  62.                             if (column == 0) { column = 1; }  
  63.                             else { column = 0; }//因为到了下一个页面 所以需要改变column的值  
  64.                             if (column == 0)//因为column值本来为1也就是在右栏 当高度大于右栏的时候就需要新建一个页面继续添加了  
  65.                             {  
  66.                                 document.NewPage();  
  67.                                 newspagenum += 1;  
  68.                             }  
  69.                             //下面这个变量存放的是每个图片的高度  因为图片定位的话是按照左边距和下边距来定位的  所以必须知道图片 标题以及副标题的高度  
  70.                             imageTwo = 0;  
  71.                             cb.AddImage(imgzhengwen, imgzhengwen.ScaledWidth, 0, 0, imgzhengwen.ScaledHeight, (bankai - imgzhengwen.ScaledWidth) / 2 + 10 + imgWidth[column], 720 - imgzhengwen.ScaledHeight);  
  72.                             imageTwo += imageHeight - 20;//因为这是在一篇文章里面 第二页的时候不需要标题  为了界面美观 所以高度应该往上  
  73.                             banmian = false;  
  74.                         }  
  75.                         else//没有大于右栏的情况处理  
  76.                         {  
  77.                             cb.AddImage(imgzhengwen, imgzhengwen.ScaledWidth, 0, 0, imgzhengwen.ScaledHeight, (bankai - imgzhengwen.ScaledWidth) / 2 + 10 + imgWidth[column], 720 - imgzhengwen.ScaledHeight - subHeight - imageTwo);  
  78.                             imageTwo += imageHeight + 20;  
  79.                         }  
  80.                     }  
  81.                     //文章可能只有一页也有可能有多页   不过是否产生多栏 都从左栏开始  因为前面做了判断如果产生多栏则新建一个页面  
  82.                     else  
  83.                     {  
  84.                         if (imageHeight + imageTwo > 656 - subHeight)//判断图片是否大于左栏 如果大于则添加到右栏  
  85.                         {  
  86.                             //当一篇文章新建一页哦   将图片高度清空  继续添加  
  87.                             if (banmian) { } else { imageTwo = 0; }  
  88.                             cb.AddImage(imgzhengwen, imgzhengwen.ScaledWidth, 0, 0, imgzhengwen.ScaledHeight, (bankai - imgzhengwen.ScaledWidth) / 2 + 10 + 600, 720 - imgzhengwen.ScaledHeight - subHeight - imageTwo);  
  89.                             imageTwo += imageHeight + 20;  
  90.                             banmian = true;//让下次添加到右栏  
  91.                             column = 1;  
  92.                             newspagenum += 1;//将页码次数加一  
  93.                         }  
  94.                         else//没有大于左栏就添加  
  95.                         {  
  96.                             cb.AddImage(imgzhengwen, imgzhengwen.ScaledWidth, 0, 0, imgzhengwen.ScaledHeight, (bankai - imgzhengwen.ScaledWidth) / 2 + 10, 660 - imgzhengwen.ScaledHeight - subHeight - imageTwo);  
  97.                             imageTwo += imageHeight + 20;  
  98.                         }  
  99.                     }  
  100.                 }  
  101.             }  
  102.             string kongge = " ";  
  103.             float konggechangdu = bf.GetWidthPoint(kongge, (float)12);//得到-的宽度  
  104.             float banmianchangdu = bf.GetWidthPoint(dt.Rows[i]["banmianming"].ToString(), (float)12);  
  105.             StringBuilder stringBuilder = new StringBuilder();  
  106.             for (int j = 0; j < (420 - banmianchangdu) / konggechangdu; j++)  
  107.             {  
  108.                 stringBuilder.Append(kongge);  
  109.             }  
  110.             ct.AddText(new Paragraph("/r" + dt.Rows[i]["Content"].ToString(), BaseFontAndSize(2, 11, "000000")));//在左栏写入正文  
  111.             ct.AddText(new Paragraph("/n/r" + stringBuilder + dt.Rows[i]["banmianming"].ToString(), BaseFontAndSize(0, 11, "6c423c")));//在左栏写入版面名称  
  112.             int status = 0;  
  113.             int pagenum = 1;  
  114.             //对内容进行判断  因为内容也会产生多栏或者多页的情况  
  115.             while ((status & ColumnText.NO_MORE_TEXT) == 0)//对内容进行判断  
  116.             {  
  117.                 if (column == 1)//如果文章在右栏则新建一个页面继续添加  
  118.                 {  
  119.                     ct.SetSimpleColumn(rightwidth[column], 60, left[column], 720 - subHeight - imageTwo, 16, Element.ALIGN_JUSTIFIED);  
  120.                     status = ct.Go();//iTextSharp里面的一个方法  如果产生分栏则返回2如果不分栏返回1  
  121.                     document.NewPage();//新建一个页面  
  122.                     imageTwo = 0;//图片高度清空  
  123.                 }  
  124.                 else//文章在左栏继续添加  
  125.                 {  
  126.                     //因为内容是跟在正文图片后面的  所以必须减去图片的高度  不然文字会定位在图片上  
  127.                     ct.SetSimpleColumn(rightwidth[column], 60, left[column], 660 - subHeight - imageTwo, 16, Element.ALIGN_JUSTIFIED);  
  128.                     status = ct.Go();  
  129.                 }  
  130.                 if ((status & ColumnText.NO_MORE_COLUMN) != 0)//判断是否产生第二栏  
  131.                 {  
  132.                     column++;  
  133.                     pagenum++;  
  134.                     if (column > 1)//如果column大于1则将它赋值为0 因为已经到了右栏  
  135.                     {  
  136.                         column = 0;  
  137.                     }  
  138.                     imageTwo = 0;  
  139.                 }  
  140.             }  
  141.             pageNum = pagenum + newspagenum;//页码数等于正文图片产生的页码数加上内容产生的页码数 因为只有这两个才会产生多个页码  
  142.             return pageNum;//返回页码  
  143.         } 


生成pdf的重点了    先看图片吧    可能会出现的情况  :

 

 

     正常情况下文章和图片的排列

 

  当文章内容大于左栏的时候 

 

 

 

 

  图片在左   文章在右

   正文图片大于左栏甚至是右栏的情况

   文字在左   图片在右

 

   下面贴代码   代码有点多    可能某些部分还不够优化:

 

  1.     int pageNum = 0;//根据是否分栏添加页数  
  2.     int newspagenum = 0;  
  3.     for (int i = 0; i < dt.Rows.Count; i++)//将所有的内容和版面进行输出  
  4.     {  
  5.         document.NewPage();//新建一个页面  
  6.         status = 0;  
  7.         if (dt.Rows[i]["IntroTitle"].ToString() != "")//在左栏写入引题  
  8.         {  
  9.             ct.AddText(new Paragraph(dt.Rows[i]["IntroTitle"].ToString(), BaseFontAndSize(0, 17, "9f9fa0")));  
  10.             ct.Indent = 20;  
  11.             ct.SetSimpleColumn(left[column], 100, rightwidth[column], 760, 16, Element.ALIGN_CENTER);  
  12.             ct.Go();  
  13.         }  
  14.         ct.AddText(new Paragraph(dt.Rows[i]["Title"].ToString() + "/n", BaseFontAndSize(0, 22, "000000")));//在左栏写入标题  
  15.         ct.Indent = 20;  
  16.         ct.SetSimpleColumn(left[column], 100, rightwidth[column], 720, 16, Element.ALIGN_CENTER);  
  17.         ct.Go();  
  18.         int subHeight = 0;  
  19.         if (dt.Rows[i]["SubTitle"].ToString() != "")  
  20.         {  
  21.             int subCount = dt.Rows[i]["SubTitle"].ToString().Length / 80;//判断字符串的长度  
  22.             ct.AddText(new Paragraph(dt.Rows[i]["SubTitle"].ToString(), BaseFontAndSize(0, 15, "9f9fa0")));//在左栏写入副题  
  23.             ct.SetSimpleColumn(left[column], 60, rightwidth[column], 675, 16, Element.ALIGN_CENTER);  
  24.             ct.Go();  
  25.             subHeight = (subCount + 1) * 50;//加高  
  26.         }  
  27.         ct.AddText(new Paragraph(dt.Rows[i]["Author"].ToString(), BaseFontAndSize(0, 10, "000000")));//在左栏写入作者  
  28.         ct.SetSimpleColumn(left[column], 60, rightwidth[column], 690 - subHeight, 16, Element.ALIGN_CENTER);  
  29.         ct.Go();  
  30.         int imageHeight = 0;  
  31.         int imageTwo = 0;  
  32.         bool banmian = false;  
  33.         bool boolNewPage = false;  
  34.         if (dt.Rows[i]["Image"].ToString() != "")//在左栏写入正文图片  
  35.         {  
  36.             string[] image = dt.Rows[i]["Image"].ToString().Split(',');  
  37.             for (int j = 0; j < image.Length - 1; j++)  
  38.             {  
  39.                 iTextSharp.text.Image imgzhengwen = iTextSharp.text.Image.GetInstance(image[j]);  
  40.                 if (imgzhengwen.Width > bankai || imgzhengwen.Height > 690 - subHeight)  
  41.                 {  
  42.                     imgzhengwen.ScalePercent(60);  
  43.                 }  
  44.                 imageHeight = (int)imgzhengwen.ScaledHeight;  
  45.                 imgzhengwen.Alignment = iTextSharp.text.Image.ALIGN_CENTER;  
  46.                 if (banmian)  
  47.                 {  
  48.                     if (imageHeight + imageTwo > 720 - subHeight)  
  49.                     {  
  50.                         if (column == 0) { column = 1; }  
  51.                         else { column = 0; }  
  52.                         if (column == 0)  
  53.                         {  
  54.                             NewPageNum(lift, cb, bf, pageNum, newspagenum, i);  
  55.                             document.NewPage();  
  56.                             newsPageNum += 2;  
  57.                             NewPageNum(lift, cb, bf, pageNum, newspagenum, i);  
  58.                         }  
  59.                         imageTwo = 0;  
  60.                         cb.AddImage(imgzhengwen, imgzhengwen.ScaledWidth, 0, 0, imgzhengwen.ScaledHeight, (bankai - imgzhengwen.ScaledWidth) / 2 + 10 + imgWidth[column], 720 - imgzhengwen.ScaledHeight);  
  61.                         imageTwo += imageHeight - 20;  
  62.                         banmian = false;  
  63.                         pageBool = true;  
  64.                     }  
  65.                     else  
  66.                     {  
  67.                         cb.AddImage(imgzhengwen, imgzhengwen.ScaledWidth, 0, 0, imgzhengwen.ScaledHeight, (bankai - imgzhengwen.ScaledWidth) / 2 + 10 + imgWidth[column], 720 - imgzhengwen.ScaledHeight - subHeight - imageTwo);  
  68.                         imageTwo += imageHeight + 20;  
  69.                     }  
  70.                 }  
  71.                 else  
  72.                 {  
  73.                     if (imageHeight + imageTwo > 656 - subHeight)  
  74.                     {  
  75.                         if (boolNewPage)  
  76.                         {  
  77.                             if (pageBool)  
  78.                             { NewPageNum(lift, cb, bf, pageNum, newspagenum, i); }  
  79.                             else { NewPageNum(lift, cb, bf, -1, newspagenum, i); }  
  80.                             document.NewPage();  
  81.                             if (i == 0)  
  82.                             { newsPageNum = 1; }  
  83.                             else { newsPageNum += 2; }  
  84.                             NewPageNum(lift, cb, bf, pageNum, newspagenum, i);  
  85.                         }  
  86.                         if (column == 0) { column = 1; }  
  87.                         else { column = 0; }  
  88.                         if (banmian) { } else { imageTwo = 0; }  
  89.                         cb.AddImage(imgzhengwen, imgzhengwen.ScaledWidth, 0, 0, imgzhengwen.ScaledHeight, (bankai - imgzhengwen.ScaledWidth) / 2 + 10 + imgWidth[column], 720 - imgzhengwen.ScaledHeight - subHeight - imageTwo);  
  90.                         banmian = true;  
  91.                         imageTwo = 0;  
  92.                         imageTwo += imageHeight + 20;  
  93.                     }  
  94.                     else  
  95.                     {  
  96.                         if (boolNewPage) { }  
  97.                         else  
  98.                         {  
  99.                             if (column == 1)  
  100.                             {  
  101.                                 boolNewPage = true;  
  102.                                 iTextSharp.text.Image img6 = NewBanMianImages(dt, cb, c, bf, column, i);//添加版面图  
  103.                             }  
  104.                         }  
  105.                         cb.AddImage(imgzhengwen, imgzhengwen.ScaledWidth, 0, 0, imgzhengwen.ScaledHeight, (bankai - imgzhengwen.ScaledWidth) / 2 + 10 + imgWidth[column], 660 - imgzhengwen.ScaledHeight - subHeight - imageTwo);  
  106.                         imageTwo += imageHeight + 20;  
  107.                     }  
  108.                 }  
  109.             }  
  110.         }  
  111.         string kongge = " ";  
  112.         float konggechangdu = bf.GetWidthPoint(kongge, (float)12);//得到-的宽度  
  113.         float banmianchangdu = bf.GetWidthPoint(dt.Rows[i]["banmianming"].ToString(),(float)12);  
  114.         StringBuilder stringBuilder = new StringBuilder();  
  115.         for (int j = 0; j < (420-banmianchangdu)/konggechangdu; j++)  
  116.         {  
  117.             stringBuilder.Append(kongge);  
  118.         }  
  119.         ct.AddText(new Paragraph("/r" + dt.Rows[i]["Content"].ToString(), BaseFontAndSize(2, 11, "000000")));//在左栏写入正文  
  120.         ct.AddText(new Paragraph("/n/r" + stringBuilder + dt.Rows[i]["banmianming"].ToString(), BaseFontAndSize(0, 11, "6c423c")));//在左栏写入版面名称  
  121.         bool newbool = false;  
  122.         float[] height = { 660 - subHeight - imageTwo, 720 };  
  123.         bool newtwobool = false;  
  124.         int newpagenum = 0;  
  125.         while ((status & ColumnText.NO_MORE_TEXT) == 0)//对内容进行循环定位  
  126.         {  
  127.             if (newbool)  
  128.             {  
  129.                 ct.SetSimpleColumn(left[column], 60, rightwidth[column], height[column], 16, Element.ALIGN_JUSTIFIED);  
  130.                 status = ct.Go();  
  131.             }  
  132.             else  
  133.             {  
  134.                 if (newtwobool)  
  135.                 {  
  136.                     ct.SetSimpleColumn(left[column], 60, rightwidth[column], 720, 16, Element.ALIGN_JUSTIFIED);  
  137.                     status = ct.Go();  
  138.                 }  
  139.                 else  
  140.                 {  
  141.                     if (banmian)  
  142.                     {  
  143.                         ct.SetSimpleColumn(left[column], 60, rightwidth[column], 720 - subHeight - imageTwo, 16, Element.ALIGN_JUSTIFIED);  
  144.                         status = ct.Go();  
  145.                     }  
  146.                     else  
  147.                     {  
  148.                         ct.SetSimpleColumn(left[column], 60, rightwidth[column], 660 - subHeight - imageTwo, 16, Element.ALIGN_JUSTIFIED);  
  149.                         status = ct.Go();  
  150.                     }  
  151.                 }  
  152.                 if (banmian)  
  153.                 { }  
  154.                 else  
  155.                 {  
  156.                     if (boolNewPage) { }  
  157.                     else  
  158.                     {  
  159.                         if (column == 1)  
  160.                         {  
  161.                             iTextSharp.text.Image img6 = NewBanMianImages(dt, cb, c, bf, column, i);//添加版面图  
  162.                             NewPageNum(lift, cb, bf, pageNum, newspagenum, i);//添加页码  
  163.                             newbool = true;  
  164.                         }  
  165.                     }  
  166.                 }  
  167.             }  
  168.             if ((status & ColumnText.NO_MORE_COLUMN) != 0)//判断内容是否需要添加到第二栏  
  169.             {  
  170.                 column++;  
  171.                 newbool = true;  
  172.                 newpagenum++;  
  173.                 if (column > 1)//如果内容超过一页则新建一个页面继续进行添加  
  174.                 {  
  175.                     cb.Stroke();  
  176.                     NewPageNum(lift, cb, bf, pageNum, newspagenum, i);  
  177.                     document.NewPage();  
  178.                     column = 0;  
  179.                     newspagenum += 2;  
  180.                     newbool = false;  
  181.                     newtwobool = true;  
  182.                 }  
  183.             }  
  184.         }  
  185.         if (newbool)  
  186.         {  
  187.         }  
  188.         else  
  189.         {  
  190.             if (i + 1 == dt.Rows.Count) { }//到最后一页的时候不需要添加版面图  
  191.             else { iTextSharp.text.Image img6 = NewBanMianImages(dt, cb, c, bf, column, i + 1); }  
  192.         }  
  193.         cb.Stroke();  
  194.         NewPageNum(lift, cb, bf, pageNum, newspagenum, i);  
  195.         if (newbool)  
  196.         {  
  197.             if (newpagenum == 3)  
  198.             {  
  199.                 pageNum = 0;  
  200.             }  
  201.             else if (newpagenum > 3)  
  202.             {  
  203.                 pageNum++;  
  204.   
  205.             }  
  206.         }  
  207.     }  
  208.     document.Close();//关闭document  
  209.     column = 0;  
  210.     newsPageNum = 0;  
  211.      
  212. }  
  213.   
  214. private static void NewPageNum(string lift, PdfContentByte cb, BaseFont bf, int pageNum, int newspagenum, int i)//添加页码  
  215. {  
  216.     iTextSharp.text.Image imglift = iTextSharp.text.Image.GetInstance(lift);  
  217.     imglift.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;  
  218.     cb.AddImage(imglift, 21, 0, 0, 842, 30, -22);  
  219.     cb.Stroke();  
  220.     cb.BeginText();  
  221.     cb.SetFontAndSize(bf, 11);  
  222.     cb.SetTextMatrix(35, 45);  
  223.     cb.ShowText((i * 2 + 2 + pageNum + newspagenum+newsPageNum).ToString());  
  224.     cb.EndText();  
  225.     cb.Stroke();  
  226.     cb.BeginText();  
  227.     cb.SetFontAndSize(bf, 11);  
  228.     cb.SetTextMatrix(1146, 45);  
  229.     cb.ShowText((i * 2 + 3 + pageNum + newspagenum+newsPageNum).ToString());//右侧页码  
  230.     cb.EndText();  
  231. }  
  232.   
  233. private static iTextSharp.text.Image NewBanMianImages(DataTable dt, PdfContentByte cb,Color c,BaseFont bf,int column, int i)//添加版面  
  234. {  
  235.     iTextSharp.text.Image img6 = iTextSharp.text.Image.GetInstance(dt.Rows[i]["LayoutPic"].ToString());//第一页右侧的版面图  
  236.     img6.ScalePercent(70);  
  237.     img6.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;  
  238.     cb.AddImage(img6, img6.ScaledWidth, 0, 0, img6.ScaledHeight, (bankai - img6.ScaledWidth) / 2 + imageWidth[column], bangao - img6.ScaledHeight / 2 + 20);  
  239.     cb.SetLineWidth(0f);//给版面图添加边线  
  240.     cb.MoveTo((bankai - img6.ScaledWidth) / 2 + imageWidth[column], bangao - img6.ScaledHeight / 2 + img6.ScaledHeight + 20);  
  241.     cb.LineTo((bankai - img6.ScaledWidth) / 2 + imageWidth[column] + img6.ScaledWidth, bangao - img6.ScaledHeight / 2 + img6.ScaledHeight + 20);//上边框线  
  242.     cb.MoveTo((bankai - img6.ScaledWidth) / 2 + imageWidth[column], bangao - img6.ScaledHeight / 2 + img6.ScaledHeight + 20);  
  243.     cb.LineTo((bankai - img6.ScaledWidth) / 2 + imageWidth[column], bangao - img6.ScaledHeight / 2 + 20);//左边框线  
  244.     cb.MoveTo((bankai - img6.ScaledWidth) / 2 + imageWidth[column] + img6.ScaledWidth, bangao - img6.ScaledHeight / 2 + img6.ScaledHeight + 20);  
  245.     cb.LineTo((bankai - img6.ScaledWidth) / 2 + imageWidth[column] + img6.ScaledWidth, bangao - img6.ScaledHeight / 2 + 20);//右边框线  
  246.     cb.MoveTo((bankai - img6.ScaledWidth) / 2 + imageWidth[column], bangao - img6.ScaledHeight / 2 + 20);  
  247.     cb.LineTo((bankai - img6.ScaledWidth) / 2 + imageWidth[column] + img6.ScaledWidth, bangao - img6.ScaledHeight / 2 + 20);//下边框线  
  248.     cb.Stroke();  
  249.     cb.BeginText();  
  250.     cb.SetFontAndSize(bf, 11);  
  251.     cb.SetColorFill(c);  
  252.     cb.SetTextMatrix((bankai - img6.ScaledWidth) / 2 + imageWidth[column] + 200, bangao - img6.ScaledHeight / 2 - 20);  
  253.     cb.ShowText(dt.Rows[i]["banmianming"].ToString());//第一页右侧添加版面名称  
  254.     cb.EndText();  
  255.     cb.Stroke();  
  256.     return img6;  
  257. }  
  258.   
  259. private Font BaseFontAndSize(int type, int size, string color)//设置文字的大小类型和颜色  
  260. {  
  261.     BaseFont bf;  
  262.     Font font = null;  
  263.     Color c = new Color(int.Parse(color, System.Globalization.NumberStyles.AllowHexSpecifier));  
  264.     switch (type)  
  265.     {  
  266.         case 0:  
  267.             bf = BaseFont.CreateFont(@"c:/windows/fonts/SIMLI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//字体:黑体   
  268.             font = new Font(bf, size, Font.NORMAL, c);  
  269.             break;  
  270.         case 1:  
  271.             bf = BaseFont.CreateFont(@"c:/windows/fonts/STKAITI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//字体:楷体  
  272.             font = new Font(bf, size, Font.NORMAL, c);  
  273.             break;  
  274.         case 2:  
  275.             bf = BaseFont.CreateFont(@"c:/windows/fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//字体:宋体  
  276.             font = new Font(bf, size, Font.NORMAL, c);  
  277.             break;  
  278.     }  
  279.     return font;  


  • 3
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在Delphi环境下使用iTextSharp.dll实现PDF文档的下载可以按照以下步骤进行: 1. 首先,您需要在网络上搜索并下载iTextSharp.dll文件。可以通过在搜索引擎中输入相关关键词来查找适用于Delphi的iTextSharp.dll文件。 2. 下载完成后,将iTextSharp.dll文件保存到您的计算机上。建议将其保存到您的Delphi项目的目录中,以便更好地管理和使用。 3. 打开Delphi集成开发环境(IDE)并加载您的项目。 4. 在Delphi中,您需要添加对iTextSharp.dll文件的引用,以便能够在代码中使用其功能。在Delphi IDE中,选择“项目”菜单,然后选择“添加到项目...”选项。 5. 在打开的对话框中,找到并选择您下载的iTextSharp.dll文件,然后单击“确定”按钮。这将向您的项目中添加对iTextSharp.dll文件的引用。 6. 添加引用后,您可以在Delphi代码中使用iTextSharp.dll提供的功能来生成和操作PDF文档。您可以调用iTextSharp.dll中定义的类和方法来创建和编辑PDF文档,然后保存为文件,供用户下载使用。您可以参考iTextSharp.dll的官方文档或在线教程来学习如何使用它的功能。 7. 最后,您可以通过在Delphi项目中相应的事件或按钮单击事件中编写代码,调用iTextSharp.dll生成或加载PDF文档,并通过适当的方式提供下载给用户。您可以使用类似于WebBroker或Indy等组件来进行文件下载操作。 总之,您需要下载、添加引用并使用iTextSharp.dll,然后编写相应的Delphi代码来实现PDF文档的下载。这样,用户就可以通过您的应用程序访问和下载生成PDF文件了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值