当前位置:首页 > 开发教程 > IT博文 > 软件工程 >

C#生成PDF表格

时间:2013-09-27 10:50 来源:互联网 作者:源码搜藏 收藏

//#region ExportToPdf ///// summary ///// ///// /summary ///// param name="dt"数据/param ///// param name="ht"ColumnName/param ///// param name="htMain"文件名称/param ///// param name="fileName"文件名称/param //private void ExportToPdf(Dat

//#region ExportToPdf
    ///// <summary>
    ///// 
    ///// </summary>
    ///// <param name="dt">数据</param>
    ///// <param name="ht">ColumnName</param>
    ///// <param name="htMain">文件名称</param>
    ///// <param name="fileName">文件名称</param>
    //private void ExportToPdf(DataTable dt, Hashtable ht, Hashtable htMain, string fileName)
    //{
    //    string caption = "";
    //    string subCaption1 = "";
    //    string subCaption2 = "";
    //    string subCaption3 = "";
    //    string imagePlace = "";
    //    if (htMain.Contains("Caption"))
    //    {
    //        caption = htMain["Caption"].ToString();
    //    }
    //    if (htMain.Contains("SubCaption1"))
    //    {
    //        subCaption1 = htMain["SubCaption1"].ToString();
    //    }
    //    if (htMain.Contains("SubCaption2"))
    //    {
    //        subCaption2 = htMain["SubCaption2"].ToString();
    //    }
    //    if (htMain.Contains("SubCaption3"))
    //    {
    //        subCaption3 = htMain["SubCaption3"].ToString();
    //    }
    //    bool bRotate = true;
    //    if (htMain.Contains("boolRotate"))
    //    {
    //        bRotate = htMain["boolRotate"].ToString() == "1";
    //    }

    //    MakePdf pdf = new MakePdf(this, fileName, PageSize.A4, bRotate);
    //    int columns = dt.Columns.Count;
    //    float[] floWidthHead = new float[1];
    //    floWidthHead[0] = 5f;
    //    iTextSharp.text.Table tbHead = pdf.PdfTable(1, floWidthHead, 100);
    //    tbHead.addCell(pdf.PdfCellHead(caption, Element.ALIGN_CENTER, 1, 1, 16f));
    //    pdf.AddHead(tbHead);
    //    pdf.SetHead(tbHead);
    //    if (htMain.Contains("ImagePlace"))
    //    {
    //        imagePlace = htMain["ImagePlace"].ToString();
    //    }
    //    if (imagePlace == "topSide")
    //    {
    //        iTextSharp.text.Table tbHead1 = pdf.PdfTable(1, floWidthHead, 100);
    //        tbHead1.addCell(pdf.PdfCellHead(subCaption1, Element.ALIGN_LEFT, 1, 1, 10f));
    //        tbHead1.addCell(pdf.PdfCellHead("", Element.ALIGN_LEFT, 1, 1, 10f));
    //        pdf.AddHead(tbHead1);
    //        pdf.SetHead(tbHead1);

    //        iTextSharp.text.Image je = iTextSharp.text.Image.getInstance(htMain["ImagePath"].ToString());//path就是函数入参,即你要转成PDF的图片路径 
    //        je.scaleAbsolute(500, 200);
    //        pdf.AddTable(je);
    //    }
    //    iTextSharp.text.Table tbHead2 = pdf.PdfTable(1, floWidthHead, 100);
    //    tbHead2.addCell(pdf.PdfCellHead(subCaption2, Element.ALIGN_LEFT, 1, 1, 10f));
    //    pdf.AddHead(tbHead2);
    //    pdf.SetHead(tbHead2);
    //    #region 表
    //    if (dt.Columns.Count == 0)
    //    {
    //        pdf.Export();
    //        return;
    //    }
    //    float[] floWidth = new float[columns + 1];
    //    floWidth[0] = 30f;
    //    for (int k = 0; k < columns; k++)
    //    {
    //        if (ht != null && ht.Contains(dt.Columns[k].ColumnName) == true)
    //        {
    //            Hashtable htStyle = GetStyleHashtable(ht[dt.Columns[k].ColumnName].ToString());

    //            if (htStyle.Contains("Width"))
    //            {
    //                floWidth[k + 1] = Utility.ParseToInt(htStyle["Width"].ToString().Replace("px", ""));
    //            }
    //        }
    //    }

    //    iTextSharp.text.Table tbl = pdf.PdfTable(columns + 1, floWidth);


    //    #region addHead
    //    if (Session["PrintMain.lstHead"] != null)
    //    {
    //        ArrayList al = Session["PrintMain.lstHead"] as ArrayList;

    //        string headName = string.Empty;
    //        string[] name = new string[al.Count];
    //        int[] top = new int[al.Count];
    //        int[] end = new int[al.Count];
    //        int index = 0;
    //        if (al.Count != 0)
    //        {

    //            for (int i = 0; i < al.Count; i++)
    //            {
    //                name[i] = al[i].ToString().Split(';')[0];
    //                top[i] = Utility.ParseToInt(al[i].ToString().Split(';')[1].Split(':')[0]);
    //                end[i] = Utility.ParseToInt(al[i].ToString().Split(';')[1].Split(':')[1]);
    //            }
    //            for (int i = 0; i < columns + 1; i++)
    //            {
    //                Cell cell = pdf.PdfCell(gvBody.Columns[i].HeaderText, Element.ALIGN_CENTER, 1, 2, 10f);
    //                if (index < al.Count)
    //                {
    //                    if (i == top[index] + 1)
    //                    {
    //                        cell = pdf.PdfCell(name[index], Element.ALIGN_CENTER, 1, 1, 10f);
    //                        cell.Colspan = end[index] - top[index] + 1;
    //                        i += end[index] - top[index];
    //                        index++;
    //                    }
    //                }
    //                tbl.addCell(cell);

    //            }
    //            for (int i = 0; i < al.Count; i++)
    //            {
    //                for (int j = top[i] + 1; j <= end[i] + 1; j++)
    //                {
    //                    tbl.addCell(pdf.PdfCell(gvBody.Columns[j].HeaderText, Element.ALIGN_CENTER, 1, 1, 10f));
    //                }
    //            }
    //        }
    //    }
    //    #endregion
    //    else
    //    {

    //        tbl.addCell(pdf.PdfCell("序号", Element.ALIGN_CENTER, 1, 1, 10f));

    //        for (int c = 0; c < columns; c++)
    //        {
    //            tbl.addCell(pdf.PdfCell(gvBody.Columns[c + 1].HeaderText, Element.ALIGN_CENTER, 1, 1, 10f));
    //        }
    //    }
    //    pdf.SetHead(tbl);
    //    for (int i = 0; i < dt.Rows.Count; i++)
    //    {
    //        tbl.addCell(pdf.PdfCell((i + 1).ToString(), Element.ALIGN_CENTER, 1, 1, 10f));
    //        for (int j = 0; j < columns; j++)
    //        {
    //            Hashtable htStyle = GetStyleHashtable(ht[dt.Columns[j].ColumnName].ToString());

    //            if (htStyle.Contains("HorizontalAlign"))
    //            {
    //                if (htStyle["HorizontalAlign"].ToString() == "right")
    //                {
    //                    tbl.addCell(pdf.PdfCell(dt.Rows[i][dt.Columns[j].ColumnName].ToString(), Element.ALIGN_RIGHT, 1, 1, 10f));

    //                }
    //                else
    //                {
    //                    tbl.addCell(pdf.PdfCell(dt.Rows[i][dt.Columns[j].ColumnName].ToString(), Element.ALIGN_LEFT, 1, 1, 10f));
    //                }
    //            }


    //        }
    //    }
    //    if (dt.Columns.Count > 0)
    //    {
    //        string DisplayIndexColumn = "1";
    //        if (htMain.Contains("DisplayIndexColumn"))
    //        {
    //            DisplayIndexColumn = htMain["DisplayIndexColumn"].ToString();
    //            if (DisplayIndexColumn == "0")
    //            {
    //                tbl.deleteColumn(0);
    //            }
    //        }
    //    }
    //    pdf.AddTable(tbl);

    //    #endregion
    //    if (imagePlace == "underSide")
    //    {
    //        iTextSharp.text.Table tbHead3 = pdf.PdfTable(1, floWidthHead, 100);
    //        tbHead3.addCell(pdf.PdfCellHead(subCaption1, Element.ALIGN_LEFT, 1, 1, 10f));
    //        tbHead3.addCell(pdf.PdfCellHead("", Element.ALIGN_LEFT, 1, 1, 10f));
    //        pdf.AddHead(tbHead3);
    //        pdf.SetHead(tbHead3);
    //        iTextSharp.text.Image je = iTextSharp.text.Image.getInstance(htMain["ImagePath"].ToString());//path就是函数入参,即你要转成PDF的图片路径 
    //        je.scaleAbsolute(500, 200);
    //        pdf.AddTable(je);
    //    }

    //    iTextSharp.text.Table tbHead4 = pdf.PdfTable(1, floWidthHead, 100);
    //    tbHead4.addCell(pdf.PdfCellHead(subCaption3, Element.ALIGN_LEFT, 1, 1, 10f));

    //    pdf.AddHead(tbHead4);
    //    pdf.SetHead(tbHead4);
    //    pdf.Export();

    //}

    //#region GetStyleHashtable
    //public Hashtable GetStyleHashtable(string style)
    //{
    //    Hashtable htStyle = new Hashtable();
    //    string[] arr = style.Split(';');
    //    for (int i = 0; i < arr.Length; i++)
    //    {
    //        string[] s = arr[i].Split(':');
    //        for (int j = 1; j < s.Length; j++)
    //        {
    //            htStyle[s[j - 1].ToString()] = s[j].ToString();
    //        }
    //    }
    //    return htStyle;
    //}
    //#endregion
    //#endregion
}


软件工程阅读排行

最新文章