最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# 利用PdfSharp生成Pdf文件的示例

 更新時(shí)間:2021年04月02日 09:19:57   作者:Alan.hsiang  
這篇文章主要介紹了C# 利用PdfSharp生成Pdf文件的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下

PdfSharp一款開源的用于創(chuàng)建,操作PDF文檔的.Net類庫,本文以一個(gè)簡單的小例子,簡述如何通過PdfSharp進(jìn)行創(chuàng)建PDF文檔,僅供學(xué)習(xí)分享使用,如有不足之處,還請(qǐng)指正。

PdfSharp下載

在本例中,主要通過NuGet包管理器進(jìn)行下載安裝,目前PdfSharp版本為v1.5.0.5147,如下所示:

涉及知識(shí)點(diǎn)

在生成PDF文檔過程中,主要知識(shí)點(diǎn)如下:

  1. PdfDocument : 表示一個(gè)PDF文檔對(duì)象,調(diào)用save方法保存文檔到指定路徑。
  2. PdfPage : 表示PDF文檔中的一頁。
  3. XGraphics:表示頁面上的繪制對(duì)象,所有的頁面內(nèi)容,都可通過此對(duì)象繪制。如:DrawString繪制文本內(nèi)容,DrawLine繪制直線等。
  4. XFont:繪制文本的字體,字體名稱只能取C:\Windows\Fonts目錄下的ttf字體文件,不能是ttc格式的字體。
  5. XTextFormatter:表示一個(gè)簡單的文本字體格式,如識(shí)別文本的換行符而實(shí)現(xiàn)自動(dòng)換行等內(nèi)容。

文檔示例圖

在本例中,主要是將頁面內(nèi)容寫入PDF文件中,頁面如下所示:

生成的PDF文件,如下所示:

核心代碼

在本例中,核心代碼主要包括如下幾個(gè)部分:

  • 創(chuàng)建文檔
  • 繪制文本
  • 繪制直線
  • 設(shè)置紙張大小,
  • 設(shè)置邊界
  • 文本的自動(dòng)換行

具體代碼,如下所示:

/// <summary>
    /// 生成Pdf
    /// </summary>
    /// <param name="filePath"></param>
    /// <param name="bo"></param>
    /// <returns></returns>
    public bool GeneratePdf(string filePath, PdfBo bo) {
      int margin_left_right = 30;//左右邊距
      int margin_top_bottom = 30;//上下邊距
      //1. 定義文檔對(duì)象
      PdfDocument document = new PdfDocument();
      //2. 新增一頁
      PdfPage page = document.AddPage();
      // 設(shè)置紙張大小
      page.Size = PageSize.A4;
      //3. 創(chuàng)建一個(gè)繪圖對(duì)象
      XGraphics gfx = XGraphics.FromPdfPage(page);
      XFont font = new XFont("華文宋體", 40, XFontStyle.Bold);
      //定制化內(nèi)容開始
      int cur_x = 0 + margin_left_right;
      int cur_y = 0 + margin_top_bottom;
      //標(biāo)題1
      gfx.DrawString(bo.Head1, font, XBrushes.Red, new XRect(cur_x, cur_y, page.Width-2*cur_x, 80), XStringFormats.Center);
      //序號(hào)
      font = new XFont("華文宋體", 12, XFontStyle.Regular);
      cur_y = cur_y + 80;
      gfx.DrawString(bo.No, font, XBrushes.Black, new XRect(cur_x, cur_y, 100, 20), XStringFormats.CenterLeft);
      //密級(jí)
      cur_x = cur_x + 200;
      gfx.DrawString(string.Format("密級(jí)[{0}]",bo.Private), font, XBrushes.Black, new XRect(cur_x, cur_y, 100, 20), XStringFormats.CenterLeft);
      //緩級(jí)
      cur_x = cur_x + 100;
      gfx.DrawString(string.Format("緩級(jí)[{0}]", bo.Speed), font, XBrushes.Black, new XRect(cur_x, cur_y, 100, 20), XStringFormats.CenterLeft);
      //簽發(fā)人
      cur_x = cur_x + 100;
      gfx.DrawString(string.Format("簽發(fā)人:{0}", bo.Person), font, XBrushes.Black, new XRect(cur_x, cur_y, 100, 20), XStringFormats.CenterLeft);
      //一條橫線
      cur_x = 0 + margin_left_right;
      cur_y = cur_y + 20;
      XPen pen = new XPen(XColor.FromKnownColor(XKnownColor.Black), 1);
      gfx.DrawLine(pen, cur_x, cur_y, page.Width-cur_x, cur_y+2);
      //標(biāo)題2
      font = new XFont("華文宋體", 20, XFontStyle.Regular);
      cur_y = cur_y + 10;
      gfx.DrawString(bo.Head2, font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width-2*cur_x, 40), XStringFormats.Center);
      //抬頭
      font = new XFont("華文宋體", 15, XFontStyle.Bold);
      cur_y = cur_y + 40;
      gfx.DrawString(bo.Title, font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width, 40), XStringFormats.CenterLeft);
      //正文 ,自動(dòng)換行
      cur_y = cur_y + 40;
      XTextFormatter tf = new XTextFormatter(gfx);
      font = new XFont("華文宋體", 12, XFontStyle.Regular);

      //測量當(dāng)前內(nèi)容下,一行可以多少個(gè)漢字
      int cnt = 0;
      int height = 0;
      for (int i = 0; i < bo.Content.Length; i++) {
        XSize xsize=gfx.MeasureString(bo.Content.Substring(0,i+1), font, XStringFormats.TopLeft);
        double width = xsize.Width;
        if (width >= page.Width - 2 * cur_x) {
          cnt = i; //表示一行可以放多少個(gè)漢字。
          height =(int) xsize.Height;
          break;
        }
      }
      cnt = cnt > 0 ? cnt : bo.Content.Length;//每一行多少漢字
      string[] arrContent = bo.Content.Split('\n');
      string new_content = "";
      int total_lines = 0;
      foreach (string content in arrContent) {
        if (content.Length <= cnt)
        {
          new_content+=string.Format("{0}\n",content);
          total_lines++;
        }
        else {
          string tmpContent = content;
          int lines = content.Length / cnt + 1;
          for (int j = 0; j < lines; j++) {
            tmpContent = tmpContent.Insert(j * cnt, "\n");
            total_lines++;
          }
          new_content += string.Format("{0}\n", tmpContent);
        }
      }
      int num = new_content.Length - new_content.Replace("\r", "").Length;
      //計(jì)算矩形
      XRect rect = new XRect(cur_x, cur_y, page.Width - 2 * cur_x, (total_lines+num)*(height+2));
      tf.DrawString(new_content, font, XBrushes.Black, rect, XStringFormats.TopLeft);
      //主題詞
      cur_y = cur_y + (total_lines + num) * (height + 2) + 20;
      font = new XFont("華文宋體", 12, XFontStyle.Bold);
      gfx.DrawString(string.Format("主題詞:{0}",bo.Keyword), font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width, 40), XStringFormats.CenterLeft);
      //再加一條橫線
      cur_y = cur_y + 40;
      gfx.DrawLine(pen, cur_x, cur_y, page.Width - cur_x, cur_y + 2);
      cur_y = cur_y + 2;
      font = new XFont("華文宋體", 10, XFontStyle.Regular);
      gfx.DrawString(string.Format("{0}{1}",bo.Company, bo.Dept), font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width-2*cur_x, 40), XStringFormats.CenterLeft);
      gfx.DrawString(DateTime.Now.ToString("yyyy 年 MM 月 dd 日 印發(fā)"), font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width-2*cur_x, 40), XStringFormats.CenterRight);
      //水印開始
      font = new XFont("華文宋體", 20, XFontStyle.BoldItalic);
      // 計(jì)算長度
      var size = gfx.MeasureString(bo.Watermark, font);

      // 定義旋轉(zhuǎn)中心
      gfx.TranslateTransform(page.Width / 2, page.Height / 2);
      gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
      gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);

      // 字符樣式
      var format = new XStringFormat();
      format.Alignment = XStringAlignment.Near;
      format.LineAlignment = XLineAlignment.Near;

      //畫刷
      XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
      for (int i = 0; i < 3; i++) {
        gfx.DrawString(bo.Watermark, font, brush,
        new XPoint((page.Width - size.Width) / (1.5+i*0.5), (page.Height - size.Height) / (1.5 + i * 0.5)),
        format);
      }
      //水印結(jié)束
      //6. 保存文檔
      document.Save(filePath);
      return true;
    }

以上就是C# 利用PdfSharp生成Pdf文件的示例的詳細(xì)內(nèi)容,更多關(guān)于c# 生成Pdf文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#窗體間常用的幾種傳值方式及委托與事件詳解

    C#窗體間常用的幾種傳值方式及委托與事件詳解

    這篇文章主要給大家介紹了關(guān)于C#窗體間常用的幾種傳值方式及委托與事件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用小程序具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • C# wx獲取token的基本方法

    C# wx獲取token的基本方法

    這篇文章主要為大家詳細(xì)介紹了C# wx獲取token的基本方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • C#并查集(union-find)算法詳解

    C#并查集(union-find)算法詳解

    本文詳細(xì)講解了C#并查集(union-find)算法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • C#使用oledb操作excel文件的方法

    C#使用oledb操作excel文件的方法

    這篇文章主要介紹了C#使用oledb操作excel文件的方法,涉及C#中oledb操作excel的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-05-05
  • C# 實(shí)現(xiàn)在控制臺(tái)上換行輸出與不換行輸出

    C# 實(shí)現(xiàn)在控制臺(tái)上換行輸出與不換行輸出

    這篇文章主要介紹了C# 實(shí)現(xiàn)在控制臺(tái)上換行輸出與不換行輸出,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • C#獲取機(jī)器碼的方法詳解(機(jī)器名,CPU編號(hào),硬盤編號(hào),網(wǎng)卡mac等)

    C#獲取機(jī)器碼的方法詳解(機(jī)器名,CPU編號(hào),硬盤編號(hào),網(wǎng)卡mac等)

    這篇文章主要介紹了C#獲取機(jī)器碼的方法,結(jié)合實(shí)例形式詳細(xì)分析了C#獲取硬件機(jī)器名、CPU編號(hào)、硬盤編號(hào)、網(wǎng)卡mac等信息的相關(guān)實(shí)現(xiàn)方法,需要的朋友可以參考下
    2016-07-07
  • c#爬蟲爬取京東的商品信息

    c#爬蟲爬取京東的商品信息

    這篇文章主要給大家介紹了關(guān)于利用c#爬蟲爬取京東商品信息的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • C#將隱私信息(銀行賬戶,身份證號(hào)碼)中間部分特殊字符替換成*

    C#將隱私信息(銀行賬戶,身份證號(hào)碼)中間部分特殊字符替換成*

    大家在銀行交易某些業(yè)務(wù)時(shí),都可以看到無論是身份證、銀行賬號(hào)中間部分都是用*號(hào)替換的,下面這篇文章主要介紹C#將隱私信息(銀行賬戶,身份證號(hào)碼)中間部分特殊字符替換成*的相關(guān)資料,需要的朋友可以參考下
    2015-08-08
  • C#中利用LINQ to XML與反射把任意類型的泛型集合轉(zhuǎn)換成XML格式字符串的方法

    C#中利用LINQ to XML與反射把任意類型的泛型集合轉(zhuǎn)換成XML格式字符串的方法

    本文主要介紹了C#中利用LINQ to XML與反射把任意類型的泛型集合轉(zhuǎn)換成XML格式字符串的方法:利用反射,讀取一個(gè)類型的所有屬性,然后再把屬性轉(zhuǎn)換成XML元素的屬性或者子元素。下面注釋比較完整,需要的朋友可以看下
    2016-12-12
  • c#可以創(chuàng)建任意控件的拖動(dòng)方法

    c#可以創(chuàng)建任意控件的拖動(dòng)方法

    下面小編就為大家分享一篇c#可以創(chuàng)建任意控件的拖動(dòng)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03

最新評(píng)論

松溪县| 井陉县| 洱源县| 丽水市| 孟州市| 高阳县| 许昌县| 南华县| 博湖县| 伊金霍洛旗| 津市市| 宁陕县| 紫云| 怀来县| 进贤县| 砚山县| 班戈县| 乐都县| 和平区| 宜兴市| 桓台县| 马山县| 肥东县| 中西区| 紫云| 靖远县| 合阳县| 田东县| 苍梧县| 满洲里市| 东辽县| 千阳县| 桓台县| 云南省| 南平市| 呼和浩特市| 西峡县| 城步| 常州市| 博罗县| 巴里|