码迷,mamicode.com
首页 > 其他好文 > 详细

iTextSharp 简单用法

时间:2017-10-24 18:27:36      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:nbsp   conf   element   tco   write   用法   table   pre   not   

//字体
private static BaseFont bfChinese = BaseFont.CreateFont(@"C:\Windows\Fonts\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); private Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, new BaseColor(0, 0, 0)); private Font fontHeader = new Font(bfChinese, 14, Font.BOLD, new BaseColor(0, 0, 0)); private Font fontTitle = new Font(bfChinese, 18, Font.BOLD, new BaseColor(0, 0, 0));
Document document = new Document(PageSize.A4.Rotate(), 5, 5, 10, 10);//创建纸张为A4,横向的纸张
using (MemoryStream pdfStream = new MemoryStream())
            {
                PdfWriter pdfWriter = PdfWriter.GetInstance(document, pdfStream);
                document.Open();
                Paragraph header = new Paragraph(config.Title, fontTitle);
                header.Alignment = 1;
                header.SpacingAfter = 10;//段落后空10行
                document.Add(header);

                Paragraph table = new Paragraph();
                document.Add(SetPdfTable(config, type, list));

                document.Close();
                pdfWriter.Close();

                return pdfStream.ToArray();
            }
private PdfPTable SetPdfTable()
        {
            int columCount=5
            PdfPTable table = new PdfPTable(5);
            float[] columnsWidth = new float[5];

            PdfPCell cell;

            while (index < 18)
            {
                cell = new PdfPCell(new Phrase(index.ToString(), fontChinese));
                index = index + 1;
                SetAlign(cell);
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase(string.Empty));
                for (int i = 0; i < columCount; i++)
                {
                    table.AddCell(cell);
                }
                listCount = listCount + 1;
            }

            cell = new PdfPCell(new Paragraph("调查人:", fontChinese));
            cell.Colspan = columCount + 1;
            cell.Border = 0;
            cell.HorizontalAlignment = Element.ALIGN_RIGHT;
            cell.PaddingRight = 80;
            cell.PaddingTop = 10;
            table.AddCell(cell);

            table.SetTotalWidth(columnsWidth);
            return table;
        }

        private void SetAlign(PdfPCell cell)
        {
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.Padding = 4.0f;
        }

 

iTextSharp 简单用法

标签:nbsp   conf   element   tco   write   用法   table   pre   not   

原文地址:http://www.cnblogs.com/tangchun/p/7724757.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!