asp.net 2.0 中產生PDF

來源:互聯網
上載者:User

    近期要用asp.net 2.0產生PDF,看了下書,查了下資料,發現可以有組件幫得上忙,可以下載itextsharp(https://sourceforge.net/projects/itextsharp)
下載,然後在工程中引用該控制項,舉例子如下

1  datatable 的內容轉換為PDF
      首先,建立一個datatable轉換為pdf的方法如下
 using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

/// <summary>
/// 將DataTable轉化為PDF檔案的方法
/// </summary>
public class TableToPDF
{
 public TableToPDF()
 {
 }
    /// <summary>
    /// 轉換資料表為PDF文檔
    /// </summary>
    /// <param name="Data">資料表資料</param>
    /// <param name="PDFFile">目標PDF檔案全路徑</param>
    /// <param name="FontPath">字型所在路徑</param>
    /// <param name="FontSize">字型大小</param>
    /// <returns>返回調用是否成功</returns>
    public static bool ConvertDataTableToPDF(DataTable datatable, string PDFFilePath, string FontPath, float FontSize)
    {
        //初始化一個目的文件類
        Document document = new Document();
        //調用PDF的寫入方法流
        //注意FileMode-Create表示如果目標檔案不存在,則建立,如果已存在,則覆蓋。
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PDFFilePath, FileMode.Create));
        //開啟目的文件對象
        document.Open();
        //建立PDF文檔中的字型
        BaseFont baseFont =BaseFont.CreateFont(
            FontPath,
            BaseFont.IDENTITY_H,
            BaseFont.NOT_EMBEDDED);
        //根據字型路徑和字型大小屬性建立字型
        Font font = new Font(baseFont, FontSize);
        //根據資料表內容建立一個PDF格式的表
        PdfPTable table = new PdfPTable(datatable.Columns.Count);
        //遍曆原table的內容
        for (int i = 0; i < datatable.Rows.Count; i++)
        {
            for (int j = 0; j < datatable.Columns.Count; j++)
            {
                table.AddCell(new Phrase(datatable.Rows[i][j].ToString(), font));
            }
        }
        //在目的文件中添加轉化後的表資料
        document.Add(table);
        //關閉目標檔案
        document.Close();
        //關閉寫入流
        writer.Close();
        return true;
    }
}

然後,在要調用轉換的按鈕的事件代碼中調用就可以了
  /將目標檔案儲存在此項目下
        //字型使用simsun
        //字型大小選擇14
     //mytb是資料datatable的名
        TableToPDF.ConvertDataTableToPDF(mytb, Server.MapPath(".") + @"\Table.pdf", "c:\\winnt\\FONTS\\simsun.ttc,1", 14); 

2  給出常值內容,產生PDF
    比如使用者輸入常值內容及要輸出PDF的儲存路徑的話,也可以輸出PDF
 ///<param="txt">:要輸出文本的內容</param>

private void CreateTxt(string txt,string filepath)
    {
        //建立文檔對象
        Document document = new Document();
        //執行個體化產生的文檔
  PdfWriter.GetInstance(document, new FileStream(filepath, FileMode.Create));
     //開啟文檔
        document.Open();
        //在文檔中添加常值內容
  document.Add(new Paragraph(txt));
        //關閉文檔對象
        document.Close();
    }

 3    加頁首頁尾
    private void CreatePDFheader(string filepath,string headertxt,string footertxt)
    {
        //建立文檔對象
        Document document = new Document();
        // 建立文檔寫入執行個體
        PdfWriter.GetInstance(document, new FileStream(filepath, FileMode.Create));

        // 添加頁尾
        HeaderFooter footer = new HeaderFooter(new Phrase(footertxt), true);
        footer.Border = Rectangle.NO_BORDER;
        document.Footer = footer;

        //開啟文檔內容對象
        document.Open();

        // 添加頁首
        HeaderFooter header = new HeaderFooter(new Phrase(headertxt), false);
        document.Header = header;
        //設計各頁的內容
        document.Add(new Paragraph("This is First Page"));
        //新添加一個頁
        document.NewPage();
        //第2頁中添加文本
        document.Add(new Paragraph("This is Second Page"));
        // 重設頁面數量
        document.ResetPageCount();
        //關閉文檔對象
        document.Close();
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.