iTextSharp 导出 PDF 文件

发布时间:2022-06-27 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了iTextSharp 导出 PDF 文件脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

 引用DLL

using iTextSharp.text;using iTextSharp.text.pdf;

创建表格PDF

private void CreateTablePdf(DataTable dt_tooling)
{
    string fName ="myPdf.pdf";
    var fileName = HttpUtility.UrlEncode(fName, System.Text.Encoding.UTF8);
    //绝对路径
    var filePath = WebAppSettings.ErpUploadRootPath + "\BizImages\YaliJi\" + fName;

    FileStream fs = new FileStream(filePath, FileMode.Create);

    //每页宽高
    Rectangle rec = new Rectangle(2000, 800);
    Document document = new Document(rec);

    document.SetMargins(0f, 0f, 0f, 0f);
    PdfWriter pdfwriter = PdfWriter.GetInstance(document, fs);
    document.Open();

    var colCount = 27;
    var leftCount = 12;
    var centerCount = 8;
    var rightCount = 7;
    PdfPTable pdftable = new PdfPTable(colCount);
    pdftable.HorizontalAlignment = 1;

    float[] cellwidth = { 5f, 6.5f, 16f, 16f, 4.6f, 4.6f, 4.6f, 4.6f, 4.6f, 4.8f, 4.8f, 5.8f
                            , 6.5f, 4.5f, 4.5f, 4.5f, 4.8f
                            , 6.5f, 4.5f, 4.5f, 4.5f, 4.8f
                            , 6.5f, 4.5f, 4.5f, 4.5f, 4.8f };
    pdftable.SetWidths(cellwidth);

    //添加表头   
    Font pdffont_i = FontFactory.GetFont("Times New Roman", 14, Font.ITALIC);
    Font pdffont_b = FontFactory.GetFont("Arial", 22, Font.BOLD);
    Font pdffont_h = FontFactory.GetFont("Arial", 12);
    Font pdffont = FontFactory.GetFont("Arial", 10);

    BaseFont baseFont = BaseFont.CreateFont("C:\WINDOWS\FONTS\STSONG.TTF",
    BaseFont.IDENTITY_H,
    BaseFont.NOT_EMBEDDED);
    iTextSharp.text.Font pdffont_c = new iTextSharp.text.Font(baseFont, 12);

    PdfPCell cell = new PdfPCell(new Phrase(("压试块"), pdffont_c));
    cell.FixedHeight = 40f;
    cell.Colspan = leftCount;
    cell.Rowspan = 4;
    cell.Border = 0;
    pdftable.AddCell(cell);

    cell = new PdfPCell(new Phrase(("ICOQuote"), pdffont_b));
    cell.Colspan = centerCount;
    cell.Rowspan = 2;
    cell.Border = 0;
    cell.BackgroundColor = new BaseColor(0xFF, 0xFA, 0xCD);
    cell.HorizontalAlignment = 1;
    pdftable.AddCell(cell);

    cell = new PdfPCell(new Phrase("nQuote: GF95869nDate: " + DateTime.Now.ToString("MM/dd/yyyy") + "nn", pdffont));
    cell.Colspan = rightCount;
    cell.Rowspan = 2;
    cell.Border = 0;
    cell.BackgroundColor = new BaseColor(0xFF, 0xFA, 0xCD);
    cell.HorizontalAlignment = 1;
    pdftable.AddCell(cell);

    cell = new PdfPCell(new Phrase(""));
    cell.Colspan = rightCount;
    cell.Rowspan = 2;
    cell.Border = 0;
    cell.VerticalAlignment = 1;
    pdftable.AddCell(cell);

    cell = new PdfPCell(new Phrase("ICO Products, LLCn" + "6415 Angola Rdn" + "Holland,OH 43528n" + "phone:419-867-3900n" + "Fax:419-867-7200", pdffont));
    cell.Colspan = centerCount;
    cell.Rowspan = 2;
    cell.Border = 0;
    cell.VerticalAlignment = 1;
    pdftable.AddCell(cell);

    cell = new PdfPCell(new Phrase("Tooling", pdffont_i));
    cell.BackgroundColor = new BaseColor(0xC0, 0xC0, 0xC0);
    cell.Colspan = colCount;
    cell.Padding = 5f;
    cell.HorizontalAlignment = 1;
    pdftable.AddCell(cell);

    List<string> tooling_header = new List<string>();
    string[] arrMain = { "编号", "成型日期", "工程", "部位", "标号", "", "水泥", "矿粉", "煤灰", "", "", "减水剂" };
    tooling_header.AddRange(arrMain);
    string[] arrInsp = { "试压日期", "压力值", "3d/Mpa", "试压日期", "压力值", "7d/Mpa", "试压日期", "压力值", "28d/Mpa" };
    string[] arrInsp1 = { "1", "2", "3", "强度", "1", "2", "3", "强度", "1", "2", "3", "强度" };
    tooling_header.AddRange(arrInsp);
    tooling_header.AddRange(arrInsp1);

    for (int i = 0; i < tooling_header.Count; i++)
    {
        string col = tooling_header[i];
        cell = new PdfPCell(new Phrase(col, pdffont_c));
        if (col == "压力值")
        {
            cell.Colspan = 3;
        }
        else if (col == "1" || col == "2" || col == "3" || col == "强度" || col.Contains("Mpa"))
        { }
        else
        {
            cell.Rowspan = 2;
        }

        cell.HorizontalAlignment = 1;
        cell.VerticalAlignment = Element.ALIGN_MIDDLE;
        cell.BackgroundColor = new BaseColor(0xC0, 0xC0, 0xC0);
        pdftable.AddCell(cell);
    }

    //写入数据
    for (int i = 0; i < dt_tooling.Rows.Count; i++)
    {
        foreach (DataColumn item in dt_tooling.Columns)
        {
            var use_font = pdffont;
            if (item.ColumnName == "工程" || item.ColumnName == "部位" || item.ColumnName == "标号")
            {
                use_font = pdffont_c;
            }
            var cellData = dt_tooling.Rows[i][item.ColumnName].ToString();
            if (string.IsNullOrEmpty(cellData))
            {
                cellData = "/";
            }
            cell = new PdfPCell(new Phrase(cellData, use_font));
            cell.HorizontalAlignment = 1;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.PaddingBottom = 4f;
            cell.PaddingTop = 4f;
            pdftable.AddCell(cell);
        }
    }

    cell = new PdfPCell(new Phrase("n"));
    cell.Colspan = colCount;
    cell.HorizontalAlignment = 1;
    pdftable.AddCell(cell);

    cell = new PdfPCell(new Phrase("Parts", pdffont_i));
    cell.BackgroundColor = new BaseColor(0xC0, 0xC0, 0xC0);
    cell.Padding = 5f;
    cell.Colspan = colCount;
    cell.HorizontalAlignment = 1;
    pdftable.AddCell(cell);

    PdfPTable pdftable2 = new PdfPTable(1);
    pdftable2.HorizontalAlignment = 1;
    PdfPCell cell2 = new PdfPCell(new Phrase("Quotation Notes:", pdffont_h));
    cell2.Colspan = colCount;
    cell2.DisableBorderSide(2);
    pdftable2.AddCell(cell2);

    //读取数据库note
    //string sqlnote = "select value from caculationfactor where NameInProgram='Note'";
    //string note = CommonMySql.ICOMold_MySql_Select_string(sqlnote);
    //cell2 = new PdfPCell(new Phrase(note + "n", pdffont));
    //cell2.Colspan = colCount;
    //cell2.DisableBorderSide(1);
    //pdftable2.AddCell(cell2);

    PdfPTable pdftable3 = new PdfPTable(1);
    pdftable3.HorizontalAlignment = 1;
    PdfPCell cell3 = new PdfPCell(new Phrase("ICOMold - Standard Terms and Conditions of Sale", pdffont_h));
    cell3.DisableBorderSide(2);
    cell3.HorizontalAlignment = 1;
    pdftable3.AddCell(cell3);

    //string sqlterms = "select value from caculationfactor where NameInProgram='TermsandConditions'";
    //string terms = CommonMySql.ICOMold_MySql_Select_string(sqlterms);
    //cell3 = new PdfPCell(new Phrase("nn" + terms, pdffont));
    //cell3.DisableBorderSide(1);
    //pdftable3.AddCell(cell3);

    document.Add(pdftable);
    //按照表格高度分页
    float tableheight = pdftable.CalculateHeights();
    if (tableheight < 600 && tableheight > 450)
    {
        document.NewPage();
    }
    document.Add(pdftable2);
    document.NewPage();
    document.Add(pdftable3);
    document.Close();

    //pdf 下载
    //FileStream filestream = new FileStream(HttpContext.Current.Server.MapPath(savePath), FileMode.Open);
    //long filesize = filestream.Length;
    //var bytes = new byte[(int)filesize];
    //filestream.Read(bytes, 0, bytes.Length);
    //filestream.Close();

    ////删除生成的文件
    //File.Delete(HttpContext.Current.Server.MapPath(savePath));

    //HttpContext.Current.Response.ContentType = "application/octet-stream";
    //HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(pdfName, System.Text.Encoding.UTF8));
    //HttpContext.Current.Response.AddHeader("Content-Length", filesize.ToString());
    //HttpContext.Current.Response.BinaryWrite(bytes);
    //HttpContext.Current.Response.Flush();
    //HttpContext.Current.Response.End();
    //HttpContext.Current.Response.Close();

}

 

脚本宝典总结

以上是脚本宝典为你收集整理的iTextSharp 导出 PDF 文件全部内容,希望文章能够帮你解决iTextSharp 导出 PDF 文件所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:数据库