当前位置: 移动技术网 > IT编程>开发语言>.net > 详解高效而稳定的企业级.NET Office 组件Spire(.NET组件介绍之二)

详解高效而稳定的企业级.NET Office 组件Spire(.NET组件介绍之二)

2017年12月12日  | 移动技术网IT编程  | 我要评论

日本女排名单2017,香港周大福金价,致命复活下载

在项目开发中,尤其是企业的业务系统中,对文档的操作是非常多的,有时几乎给人一种错觉的是”这个系统似乎就是专门操作文档的“。毕竟现在的很多办公中大都是在pc端操作文档等软件,在这些庞大而繁重的业务中,单单依靠人力去做文档的操作需要的代价是巨大的,比如数据统计,数据分析等业务要求。这就需要我们在开发系统时,应该尽量减少使用者的一些工作量,例如将数据直接写入文档,获取网页信息后直接存为pdf保存,以便以后继续查看。软件开发的目地是对使用者便捷,但这一要求未必对开发者来说也是便捷的。

在前面介绍过一款开源免费的组件docx,这个组件主要是对文档进行操作。另一种对excel操作的组件npoi组件。今天介绍一款.net office操作组件spire,这是一个企业级的.net office操作组件,但是这是一款不免费也不开源的组件。可能很多人听到这里就不想再读下去了,的确,在国内毕竟免费才可以占用主流市场,因为很多客户希望减少成本,所以希望采用免费的工具。

作为开发者,我也会有这样的观点,不过有的时候也会思考收费与免费的工具到底哪一个好,其实这样的思考到最后似乎是没有意义的,因为事物存在既有价值,免费的可以减少成本,收费的可以获取稳定而安全的支持,各有优势和特点。任何一个软件的生成都是需要成本的,因为任何软件都是人员开发出来的,需要支付对应的成本,此处不收钱,其他的地方也会收费。无论收费与免费的哪一个好,技术总是没有错的,收费的东西,我们也可以了解,做一个技术储备。

今天要介绍的一款组件有收费的部分和免费的部分,但是这款软件的功能的确比较的强大,使用起来也比较简单,因为要收费的东西,毕竟需要做到人性化,不然谁会出钱去买,毕竟便宜而好用的东西很少。此组件的使用方式很简单,官方提供了比较完备的操作demo,所以今天的文章只做为一个引子。

由于spire的组件较多,今天就用spire.pdf for .net做个引子,不一定收费就没有客户会选择,如果需要稳定的服务支持,收费的组件是可以考虑的,或者遇到土豪客户,也可以在项目中使用一下,毕竟使用起来很便捷。

一.spire.pdf for .net组件概述:

spire.pdf for .net是一个专业的pdf组件,用于在.net应用程序中创建,编写,编辑,处理和阅读pdf文件,而不需要任何外部依赖。 使用这个.net pdf库,您可以实现丰富的功能从头开始创建pdf文件或完全通过c#/ vb.net处理现有的pdf文档,而无需安装adobe acrobat。

 .net pdf api支持许多丰富的功能,例如安全设置(包括数字签名),pdf文本/附件/图像提取,pdf合并/拆分,元数据更新,段,图像/图像绘制和插入,表创建 以及处理和导入数据等。

此外,spire.pdf for .net可以应用于使用c#/ vb.net以高质量轻松地将文本,图像和html转换为pdf。

以下是一个官方给出的组件解析图:

spire.pdf for .net支持将html,rtf,xps,文本和图像转换为具有高效性能的pdf文档。 开发人员可以将pdf转换为word,xps,svg,emf,jpg,png,bmp,tiff,文本格式。 此外,随着spire.doc for .net和spire.xls for .net,开发人员可以将word(doc / docx),excel(xls / xlsx)和xml转换为pdf。

此组件的功能还是非常强大的,每个开发人员都知道,产品做得不好,想要客户的钱还是很难的。看一下组件的主要功能:

1.文本格式,多语言支持,文本对齐等。

2.笔和画笔将形状元素,文本,图像绘制成pdf文档。

3.图层,透明图形,颜色空间和条形码创建可以呈现为pdf文档。

4.pdf / a-1b和pdf / x1a:2001合规性,可以应用这两种标准。

5.添加标量/矢量图像和掩码,并将它们放在指定的位置。

6.spire.pdf for .net可支持表和表样式

7.插入交互式元素,包括注释,操作,javascript,附件,书签和指定地点和外观。

以上对组件的相关背景做了一个简单的介绍,并且对组件的功能和使用情形做了简单的罗列。

二.spire.pdf for .net相关类的解析:

在这里主要介绍spire的spire.pdf组件部分,此组件有免费的和收费的两个版本,免费的版本在功能没有收费的多,但是稳定性和实用性还是较高的。我们具体看一下此组件的主要的类和方法,这里是主要介绍pdf的操作,就先看一下有关pdf的操作类和方法。

这里看以下命名空间的主要类:

以上的方法中只是操作pdf部分类,由于包含的类较多,过大的介绍篇幅就显得多余,在对pdf的操作中提供了较多的方法,因此在功能上会较为的丰富,使用起来也较为的便捷。

  1.pdfdocument类:声明pdf文档:

(1).pdfdocument类的构造函数:

public pdfdocument();
  public pdfdocument(string filename);
  public pdfdocument(byte[] bytes);
  public pdfdocument(stream stream);
  public pdfdocument(string filename, string password);
  public pdfdocument(byte[] bytes, string password);
  public pdfdocument(stream stream, string password);

该类提供了7个构造函数的重载版本,对应的参数类型就不做详细的介绍

(2).pdfdocument.loadfromhtml():加载html页面:

 public void loadfromhtml(string url, bool enablejavascript, bool enablehyperlinks, bool autodetectpagebreak)
{
  // this item is obfuscated and can not be translated.
  pdfhtmllayoutformat format;
  int num;
  goto label_001e;
label_008f:
  num = 0;
label_0002:
  switch (num)
  {
    case 0:
      break;

    case 1:
      if (!autodetectpagebreak)
      {
        format.layout = pdflayouttype.onepage;
        format.fittopage = clip.width;
        format.fittohtml = clip.height;
        num = 2;
      }
      else
      {
        num = 3;
      }
      goto label_0002;

    case 2:
      switch ((1 == 1))
      {
        case 2:
          goto label_008f;
      }
      if (0 != 0)
      {
      }
      break;

    case 3:
      format.layout = pdflayouttype.paginate;
      format.fittopage = clip.width;
      goto label_008f;

    default:
      goto label_001e;
      if (1 != 0)
      {
      }
      format = new pdfhtmllayoutformat();
      num = 1;
      goto label_0002;
  }
  this.sections.add().loadfromhtml(url, enablejavascript, enablehyperlinks, format);
}

2.htmlconverter名称空间:html转换器。

namespace spire.pdf.htmlconverter
{
  public enum aspectratio

  public enum clip

  [toolboxitem(false)]
  public class htmlconverter : usercontrol, sprᰐ, sprᶪ, sprṳ, sprẝ, sprẏ

  public enum imagetype

  public class pdfhtmllayoutformat
}

private metafile ();
  static htmlconverter();
  public htmlconverter();
  public int authenticate(ref intptr phwnd, ref intptr pszusername, ref intptr pszpassword);
  public htmltopdfresult convert(string url, imagetype type, int width, int height, aspectratio aspectratio);
  public htmltopdfresult convert(string html, string baseurl, imagetype type, int width, int height, aspectratio aspectratio);
  public htmltopdfresult convert(string url, imagetype type, int width, int height, aspectratio aspectratio, string username, string password);
  public image converttoimage(string url, imagetype type);
  public image converttoimage(stream stream, encoding encoding, imagetype type);
  public image converttoimage(string url, imagetype type, int width);
  public image converttoimage(stream stream, encoding encoding, imagetype type, int width);
  public image converttoimage(string url, imagetype type, int width, int height);
  public image converttoimage(string url, imagetype type, string username, string password);
  public image converttoimage(stream stream, encoding encoding, imagetype type, int width, int height);
  public image converttoimage(string url, imagetype type, int width, int height, aspectratio aspectratio);
  public image converttoimage(string url, imagetype type, int width, string username, string password);
  public image converttoimage(stream stream, encoding encoding, imagetype type, int width, int height, aspectratio aspectratio);
  public image converttoimage(string url, imagetype type, int width, int height, string username, string password);
  public image converttoimage(string url, imagetype type, int width, int height, aspectratio aspectratio, string username, string password);
  [dispid(-5512)]
  public int customizedownload();
  protected override void dispose(bool disposing);
  public image fromstring(string html, imagetype type, int width);
  public image fromstring(string html, string baseurl, imagetype type);
  public image fromstring(string html, imagetype type, int width, int height);
  public image fromstring(string html, string baseurl, imagetype type, int width);
  public image fromstring(string html, imagetype type, int width, int height, aspectratio aspectratio);
  public image fromstring(string html, string baseurl, imagetype type, int width, int height);
  public image fromstring(string html, string baseurl, imagetype type, int width, int height, aspectratio aspectratio);
  public image fromstring(string html, string baseurl, imagetype type, int width, int height, aspectratio aspectratio, string username, string password);
  public image[] getimagesfromstring(string html, string baseurl, imagetype type);
  public int queryservice(ref guid guidservice, ref guid riid, out intptr ppvobject);int sprẏ.getsecurityid(string pwszurl, intptr pbsecurityid, ref uint pcbsecurityid, ref uint dwreserved);
  int sprẏ.getsecuritysite(out intptr psite);
  int sprẏ.getzonemappings(uint dwzone, out ienumstring ppenumstring, uint dwflags);
  int sprẏ.mapurltozone(string pwszurl, out uint pdwzone, uint dwflags);
  int sprẏ.processurlaction(string pwszurl, uint dwaction, intptr ppolicy, uint cbpolicy, intptr pcontext, uint cbcontext, uint dwflags, uint dwreserved);
  int sprẏ.querycustompolicy(string pwszurl, ref guid guidkey, out intptr pppolicy, out uint pcbpolicy, intptr pcontext, uint cbcontext, uint dwreserved);
  int sprẏ.setsecuritysite(intptr psite);
  int sprẏ.setzonemapping(uint dwzone, string lpszpattern, uint dwflags);
  int sprᶪ.getcontainer(object ppcontainer);
  int sprᶪ.getmoniker(uint dwassign, uint dwwhichmoniker, object ppmk);
  int sprᶪ.onshowwindow(bool fshow);
  int sprᶪ.requestnewobjectlayout();
  int sprᶪ.saveobject();
  int sprᶪ.showobject();

以上是对pdf操作的相关类和方法的查看,由于此软件为商业软件,只能查看部分对外公开的代码,但是从可以查看到的代码就可以看出其内部实现的复杂度。

三.spire.pdf for .net实例:

   由于本文主要讲解html页面转换为pdf文档,所以先提供一种get请求html页面,以及一种获取页面图片的操作方法。接着介绍创建pdf文档、text转化为pdf, xps转换为pdf,image转换为pdf等操作方法。

1.创建http的get请求,获取网页信息:

/// <summary>
    /// 指定路径发送get请求
    /// </summary>
    /// <param name="geturl"></param>
    /// <returns></returns>
    public static string httpget(string geturl)
    {
      try
      {
        if (string.isnullorempty(geturl))
          throw new argumentnullexception(geturl);
        var request = webrequest.create(geturl) as httpwebrequest;
        if (request == null)
          return null;
        var cookiecontainer = new cookiecontainer();
        request.cookiecontainer = cookiecontainer;
        request.allowautoredirect = true;
        request.method = "get";
        request.contenttype = "application/x-www-form-urlencoded";
        var response = request.getresponse() as httpwebresponse;
        if (response != null)
        {
          var instream = response.getresponsestream();
          if (instream == null)
            throw new argumentnullexception("geturl");
          string content;
          using (var sr = new streamreader(instream, encoding.utf8))
          {
            content = sr.readtoend();
          }
          return content;
        }
      }
      catch (exception er)
      {
        throw new exception(er.message);
      }
      return null;
    }

2.取得html中所有图片的 url:

/// <summary>  
    /// 取得html中所有图片的 url。  
    /// </summary>  
    /// <param name="url">html代码</param>
    /// <returns>图片的url列表</returns> 
    public static string htmlcoderequest(string url)
    {
      if (string.isnullorempty(url))
      {
        throw new argumentnullexception(url);
      }
      try
      {
        //创建一个请求
        var httprequst = (httpwebrequest)webrequest.create(url);
        //不建立持久性链接
        httprequst.keepalive = true;
        //设置请求的方法
        httprequst.method = "get";
        //设置标头值
        httprequst.useragent = "user-agent:mozilla/4.0 (compatible; msie 6.0; windows nt 5.2; .net clr 1.0.3705";
        httprequst.accept = "*/*";
        httprequst.headers.add("accept-language", "zh-cn,en-us;q=0.5");
        httprequst.servicepoint.expect100continue = false;
        httprequst.timeout = 5000;
        //是否允许302
        httprequst.allowautoredirect = true;
        servicepointmanager.defaultconnectionlimit = 30;
        //获取响应
        var webres = (httpwebresponse)httprequst.getresponse();
        //获取响应的文本流
        string content;
        using (var stream = webres.getresponsestream())
        {
          using (var reader = new streamreader(stream, encoding.getencoding("utf-8")))
          {
            content = reader.readtoend();
          }
        }
        //取消请求
        httprequst.abort();
        //返回数据内容
        return content;
      }
      catch (exception ex)
      {
        throw new exception(ex.message);
      }
    }

3.创建pdf文档:

pdfdocument doc = new pdfdocument();

      doc.loadfromhtml(url, false, true, true);

      doc.close();

以上没有将操作组装为一个方法,由于创建操作较为简单,所以不做详细介绍,url为网页路径地址。

htmlconverter.convert
("http://www.wikipedia.org/","htmltopdf.pdf",
 //enable javascript 
 true,
 //load timeout
* 1000,
//page size
new sizef(612, 792),
//page margins
new pdfmargins(0, 0));

4.text转化为pdf:

 public static void textlayout()
    {
      //create a pdf document.
      pdfdocument doc = new pdfdocument();

      // create one page
      pdfpagebase page = doc.pages.add();
      float pagewidth = page.canvas.clientsize.width;
      float y = 0;

      //page header
      pdfpen pen1 = new pdfpen(color.lightgray, 1f);
      pdfbrush brush1 = new pdfsolidbrush(color.lightgray);
      pdftruetypefont font1 = new pdftruetypefont(new font("arial", 8f, fontstyle.italic));
      pdfstringformat format1 = new pdfstringformat(pdftextalignment.right);
      string text = "demo of spire.pdf";
      page.canvas.drawstring(text, font1, brush1, pagewidth, y, format1);
      sizef size = font1.measurestring(text, format1);
      y = y + size.height + 1;
      page.canvas.drawline(pen1, 0, y, pagewidth, y);

      //title
      y = y + 5;
      pdfbrush brush2 = new pdfsolidbrush(color.black);
      pdftruetypefont font2 = new pdftruetypefont(new font("arial", 16f, fontstyle.bold));
      pdfstringformat format2 = new pdfstringformat(pdftextalignment.center);
      format2.characterspacing = 1f;
      text = "summary of science";
      page.canvas.drawstring(text, font2, brush2, pagewidth / 2, y, format2);
      size = font2.measurestring(text, format2);
      y = y + size.height + 6;

      //icon
      pdfimage image = pdfimage.fromfile(@"..\..\..\..\..\..\data\wikipedia_science.png");
      page.canvas.drawimage(image, new pointf(pagewidth - image.physicaldimension.width, y));
      float imageleftspace = pagewidth - image.physicaldimension.width - 2;
      float imagebottom = image.physicaldimension.height + y;

      //refenrence content
      pdftruetypefont font3 = new pdftruetypefont(new font("arial", 9f));
      pdfstringformat format3 = new pdfstringformat();
      format3.paragraphindent = font3.size * 2;
      format3.measuretrailingspaces = true;
      format3.linespacing = font3.size * 1.5f;
      string text1 = "(all text and picture from ";
      string text2 = "wikipedia";
      string text3 = ", the free encyclopedia)";
      page.canvas.drawstring(text1, font3, brush2, 0, y, format3);

      size = font3.measurestring(text1, format3);
      float x1 = size.width;
      format3.paragraphindent = 0;
      pdftruetypefont font4 = new pdftruetypefont(new font("arial", 9f, fontstyle.underline));
      pdfbrush brush3 = pdfbrushes.blue;
      page.canvas.drawstring(text2, font4, brush3, x1, y, format3);
      size = font4.measurestring(text2, format3);
      x1 = x1 + size.width;

      page.canvas.drawstring(text3, font3, brush2, x1, y, format3);
      y = y + size.height;

      //content
      pdfstringformat format4 = new pdfstringformat();
      text = system.io.file.readalltext(@"..\..\..\..\..\..\data\summary_of_science.txt");
      pdftruetypefont font5 = new pdftruetypefont(new font("arial", 10f));
      format4.linespacing = font5.size * 1.5f;
      pdfstringlayouter textlayouter = new pdfstringlayouter();
      float imageleftblockheight = imagebottom - y;
      pdfstringlayoutresult result
        = textlayouter.layout(text, font5, format4, new sizef(imageleftspace, imageleftblockheight));
      if (result.actualsize.height < imagebottom - y)
      {
        imageleftblockheight = imageleftblockheight + result.lineheight;
        result = textlayouter.layout(text, font5, format4, new sizef(imageleftspace, imageleftblockheight));
      }
      foreach (lineinfo line in result.lines)
      {
        page.canvas.drawstring(line.text, font5, brush2, 0, y, format4);
        y = y + result.lineheight;
      }
      pdftextwidget textwidget = new pdftextwidget(result.remainder, font5, brush2);
      pdftextlayout textlayout = new pdftextlayout();
      textlayout.break = pdflayoutbreaktype.fitpage;
      textlayout.layout = pdflayouttype.paginate;
      rectanglef bounds = new rectanglef(new pointf(0, y), page.canvas.clientsize);
      textwidget.stringformat = format4;
      textwidget.draw(page, bounds, textlayout);

      //save pdf file.
      doc.savetofile("textlayout.pdf");
      doc.close();

      //launching the pdf file.
      pdfdocumentviewer("textlayout.pdf");
    }

5.xps转换为pdf:

public void xpstopdf()
    {
      //xps file
      string file = @"..\..\..\..\..\..\data\sample4.xps";

      //open xps document
      pdfdocument doc = new pdfdocument();
      doc.loadfromxps(file);

      //convert to pdf file.
      doc.savetofile("sample4.pdf");
      doc.close();

      //launching the pdf file.
      pdfdocumentviewer("sample4.pdf");
    }

 6.image转换为pdf:

 public void imagetopdf()
    {
      //create a pdf document.
      pdfdocument doc = new pdfdocument();

      // create one page
      pdfpagebase page = doc.pages.add();

      //draw the text
      page.canvas.drawstring("hello, world!",
                  new pdffont(pdffontfamily.helvetica, 30f),
                  new pdfsolidbrush(color.black),
                  10, 10);
      //draw the image
      pdfimage image = pdfimage.fromfile(@"..\..\..\..\..\..\data\salesreportchart.png");
      float width = image.width * 0.75f;
      float height = image.height * 0.75f;
      float x = (page.canvas.clientsize.width - width) / 2;

      page.canvas.drawimage(image, x, 60, width, height);

      //save pdf file.
      doc.savetofile("image.pdf");
      doc.close();

      //launching the pdf file.
      pdfdocumentviewer("image.pdf");
    }

以上提供了对网站发起http请求,获取网站页面信息,以及采用spire.pdf组件创建pdf文档。如果有需要可以直接将http请求获取到的信息直接加载如spire.pdf的组件中,

由组件直接将网页信息转化为pdf文件,在这里就不再做更多的赘述,由于官方提供了很完善的demo和操作文档,在这里就不再过多的介绍使用方法。

四.总结:

 以上介绍了一款收费不开源的组件,没有更多的深入的去介绍,由于组件不开源,无法进行反编译,毕竟存在版权问题,如果需要使用到企业级的文档操作组件,并且公司不缺钱的话,可以使用一下此组件,组件的底层方法封装度较高,所以在使用的时候,开发者所需要考虑的是如何去使用组件完成功能。

   个人认为软件收费应该是趋势,毕竟任何软件都是需要投入,无论是人力成本,还是资金和时间成本。本文虽然是一篇介绍技术的文章,但是也提出了一个所有开发者都在想的问题,在项目开发中到底需不需要使用收费的软件,其实这个就是看使用环境。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网