实例讲解如何整合Aspose.BarCode for .NET与Aspose.Pdf

原创|其它|编辑:郝浩|2011-09-29 14:21:24.000|阅读 862 次

概述:本文将简要介绍一下Aspose.Pdf组件与Aspose.BarCode组件的整合,以及在整合后如何添加高质量的条码图像到PDF文档中。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

  PDF文档是一种流行的用于生成报表和官方文档的媒体。Aspose公司提供的Aspose.Pdf组件可动态生成PDF文档。一旦Aspose.Pdf组件与Aspose.BarCode组件集成后,开发者可添加条码到PDF文档中。

  本文将简要介绍一下Aspose.Pdf组件与Aspose.BarCode组件的整合,以及在整合后如何添加高质量的条码图像到PDF文档中。

  创建条形码

  首先,我们将创建一个BarCodeBuilder类对象。条形码编码所需的数据将被分配到BarCodeBuilder的CodeText属性。Code128码将从符号枚举中被筛选出来,然后被分配到BarCodeBuilder的SymbologyType属性,代码如下:

  [C#]


  //Instantiate linear barcode object

  BarCodeBuilder builder = new BarCodeBuilder();

  //Set the Code text for the barcode

  builder.CodeText = "1234567";

  //Set the symbology type to Code128

  builder.SymbologyType = Symbology.Code128;

  [VB.NET]


  'Instantiate linear barcode object

  Dim builder As BarCodeBuilder = New BarCodeBuilder()

  'Set the Code text for the barcode

  builder.CodeText = "1234567"

  'Set the symbology type to Code128

  builder.SymbologyType = Symbology.Code128

  这一步的目的是为了创建一个BarCodeBuilder实例,并配置其属性。

  条形码保存到MemoryStream

  一旦配置了BarCodeBuilder属性,我们就可以创建条形码并将条形码图像保存到MemoryStream。为了实现这一目的,我们首先要创建一个MemoryStream实例,然后将条形码图像保存到MemoryStream。代码如下:

  [C#]


  //Creating memory stream

  System.IO.MemoryStream ms = new System.IO.MemoryStream();

  //Saving barcode image to memory stream

  builder.BarCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

  [VB.NET]


  'Creating memory stream

  Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream()

  'Saving barcode image to memory stream

  builder.BarCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)

  利用Aspose.Pdf创建PDF文档

  在条形码图像保存到MemoryStream后,我们需要将该条形码添加到PDF文档。因此,我们首先要利用Aspose.Pdf创建PDF文档,这样我们才能够将生成的条形码添加到PDF文档。
代码如下:

  [C#]


  //Create Pdf document

  Aspose.Pdf.Pdf pdf1 = new Aspose.Pdf.Pdf();

  //Add a section to the Pdf document

  Aspose.Pdf.Section sec1 = pdf1.Sections.Add();

  [VB.NET]


  'Create Pdf document

  Dim pdf1 As Aspose.Pdf.Pdf = New Aspose.Pdf.Pdf()

  'Add a section to the Pdf document

  Dim sec1 As Aspose.Pdf.Section = pdf1.Sections.Add()

  添加条码图像到PDF文档中

  现在,我们可以创建一个Image对象,该对象拥有添加到PDF文档部分的特性。在创建了Image对象后,需要为Image对象设置一些属性。我们知道条码图像作为二进制数据存在MemoryStream中。所以,为了读取这个二进制格式的图像,我们需要使用BinaryReader类来读取MemoryStream中的图像字节。

  [C#]


  //Create an image object inheriting properties from the section

  Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(sec1);

  //Load image data from memory stream to the image object

  image1.ImageInfo.ImageFileType = ImageFileType.MemoryBmp;

  image1.ImageInfo.OpenType = ImageOpenType.Memory;

  image1.ImageScale = 0.5F;

  System.IO.BinaryReader reader = new System.IO.BinaryReader(ms);

  ms.Position = 0;

  image1.ImageInfo.MemoryData = reader.ReadBytes((int)ms.Length);

  //Add image to the paragraphs collection of the section

  sec1.Paragraphs.Add(image1);

  //Save the Pdf

  pdf1.Save("MyBarCode.pdf");

  [VB.NET]


  'Create an image object inheriting properties from the section

  Dim image1 As Aspose.Pdf.Image = New Aspose.Pdf.Image(sec1)

  'Load image data from memory stream to the image object

  image1.ImageInfo.ImageFileType = ImageFileType.MemoryBmp

  image1.ImageInfo.OpenType = ImageOpenType.Memory

  image1.ImageScale = 0.5F

  Dim reader As System.IO.BinaryReader = New System.IO.BinaryReader(ms)

  ms.Position = 0

  image1.ImageInfo.MemoryData = reader.ReadBytes(CType(ms.Length, Integer))

  'Add image to the paragraphs collection of the section

  sec1.Paragraphs.Add(image1)

  'Save the Pdf

  pdf1.Save("MyBarCode.pdf")

  结论

  Aspose.BarCode易于与其他Aspose组件集成,从而创建丰富的应用程序,最大限度地满足用户的需求。而且值得一提的是, Aspose.BarCode版本中的任何改动也不影响与其他Aspose组件的集成,因为 Aspose.Pdf组件只处理了Aspose.BarCode生成的条码图像,而不是其内部结构。


(慧都控件网版权所有,转载请注明出处,否则追究法律责任)
标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:慧都控件网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP