C# Winfrom iTextSharp 导出pdf 带二维码 表格嵌套 简单Dome

        private void btn3_Click(object sender, EventArgs e)
        {
            var PDFFile = @"D:\Users\win10-zhiyong\Desktop\TT\X02.pdf";
            //默认页面大小
            Document document = new Document();
            document.SetPageSize(PageSize.A4);
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PDFFile, FileMode.OpenOrCreate));
            document.Open();//设置字体,支持中文
            BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font fontHead = new iTextSharp.text.Font(bfChinese, 18, iTextSharp.text.Font.BOLD, new BaseColor(25, 25, 25));
            iTextSharp.text.Font fontLabel = new iTextSharp.text.Font(bfChinese, 12, iTextSharp.text.Font.BOLD, new BaseColor(25, 25, 25));
            iTextSharp.text.Font fontValue = new iTextSharp.text.Font(bfChinese, 12, iTextSharp.text.Font.NORMAL, new BaseColor(55, 55, 55));

            int columnCount = 4;
            PdfPTable table = new PdfPTable(columnCount);
            table.WidthPercentage = 100; // percentage          
            table.DefaultCell.Padding = 1;
            table.DefaultCell.BorderWidth = 1;
            table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
            //table.DefaultCell.UseAscender = true; 这里写垂直居中对Para不起作用
            //table.DefaultCell.UseDescender = true;
            //table.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;

            PdfPCell cell = new PdfPCell();

            var strHeaderText = "";
            Paragraph para = new Paragraph("规格清单");
            para.Alignment = Element.ALIGN_CENTER;
            para.Font = fontHead;
            cell.AddElement(para);
            cell.Colspan = 4;
            cell.HorizontalAlignment = Element.ALIGN_CENTER;

            cell.UseAscender = true;

            cell.UseDescender = true;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            table.AddCell(cell);

            //表单数据
            //for (int i = 0; i < dtSource.Rows.Count; i++)
            //{
            //    for (int j = 0; j < dtSource.Columns.Count; j += 2)
            //    {
            //        table.AddCell(new Phrase(dtSource.Rows[i][j].ToString(), fontLabel));
            //        table.AddCell(new Phrase(dtSource.Rows[i][j + 1].ToString(), fontValue));
            //    }
            //}

            //审批数据
            //审批意见标签
            PdfPCell cellShenPiYiJianLabel = new PdfPCell();
            Paragraph paraShenPiYiJianLabel = new Paragraph("审批意见");
            paraShenPiYiJianLabel.Alignment = Element.ALIGN_CENTER;
            paraShenPiYiJianLabel.Font = fontLabel;
            cellShenPiYiJianLabel.AddElement(paraShenPiYiJianLabel);
            cellShenPiYiJianLabel.Rowspan = 4;
            cellShenPiYiJianLabel.HorizontalAlignment = Element.ALIGN_CENTER;
            cellShenPiYiJianLabel.UseAscender = true;
            cellShenPiYiJianLabel.UseDescender = true;
            cellShenPiYiJianLabel.VerticalAlignment = Element.ALIGN_MIDDLE;
            table.AddCell(cellShenPiYiJianLabel);



            for (int j = 0; j < 3; j++)
            {
                Paragraph paraShenPiYiJianContent = new Paragraph("审批内容123123");
                paraShenPiYiJianContent.Alignment = Element.ALIGN_CENTER;
                paraShenPiYiJianContent.Font = fontValue;

                PdfPCell cellShenPiYiJianContent = new PdfPCell();
                cellShenPiYiJianContent.Colspan = 3;
                cellShenPiYiJianContent.AddElement(paraShenPiYiJianContent);
                cellShenPiYiJianContent.UseAscender = true;
                cellShenPiYiJianContent.UseDescender = true;
                cellShenPiYiJianContent.VerticalAlignment = Element.ALIGN_MIDDLE;

                table.AddCell(cellShenPiYiJianContent);
            }


            var bmp = QrCodeHelper.CreateQrCode("132461321", 5, 7, "", 10, 5, true);
            System.Drawing.Image imgtt = bmp;
            iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imgtt, BaseColor.BLACK, true);
            //img.SetAbsolutePosition(0, 0);
            //writer.DirectContent.AddImage(img);

            var paragraph = new Paragraph();
            paragraph.Add(new Chunk(img, 0, 0));
            var cellTT = new PdfPCell { PaddingLeft = 5, PaddingTop = 5, PaddingBottom = 5, PaddingRight = 5 };
            //cellTT.HorizontalAlignment = Element.ALIGN_CENTER;
            //cellTT.AddElement(paragraph);
            cellTT.Colspan = 4;
            cellTT.AddElement(img);
            table.AddCell(cellTT);


            document.Add(table);
            document.Close();
            writer.Close();
            return;
        }

posted @ 2020-12-03 14:54  上帝视角  阅读(487)  评论(0编辑  收藏  举报