| PDF documents have become a popular medium for generating reports or other official documents in many organizations. Aspose Pty Ltd. provides a component, Aspose.Pdf to generate PDF documents on the fly. Developers can add barcodes to their PDF documents by integrating Aspose.BarCode with Aspose.Pdf.
This article will briefly describe the technique to integrate these two products together for adding high quality barcode images to PDF documents. In this article, we will discuss about creating a barcode image using Aspose.BarCode and then embed that barcode image to a PDF document generated by Aspose.Pdf. |
Creating the Barcode
First of all, we will create an object of BarCodeBuilder class.
The desired data to be encoded into barcode, will be assigned to CodeText property of the BarCodeBuilder object. The Code128 symbology will be selected from the Symbology enumeration and then assigned to SymbologyType property of the BarCodeBuilder object as shown below:
[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
The main purpose of this step is to create an instance of BarCodeBuilder and configure its properties.
First of all, we will create an object of BarCodeBuilder class.
The desired data to be encoded into barcode, will be assigned to CodeText property of the BarCodeBuilder object. The Code128 symbology will be selected from the Symbology enumeration and then assigned to SymbologyType property of the BarCodeBuilder object as shown below:
//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;
'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
The main purpose of this step is to create an instance of BarCodeBuilder and configure its properties.
Saving Barcode to MemoryStream
Once the properties of BarCodeBuilder object are configured then we can create and save the barcode image to a MemoryStream.
For this purpose, we will create an instance of MemoryStream and then store the barcode image to this MemoryStream by calling Save method of the BarCodeBuilder.BarCodeImage property as shown below:
[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)
Once the properties of BarCodeBuilder object are configured then we can create and save the barcode image to a MemoryStream.
For this purpose, we will create an instance of MemoryStream and then store the barcode image to this MemoryStream by calling Save method of the BarCodeBuilder.BarCodeImage property as shown below:
//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);
'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)
Creating a PDF Document using Aspose.Pdf
After the barcode image is saved to a MemoryStream, we are done with the barcode image. The only thing now needed is to add this barcode image (saved in the MemoryStream) to the PDF document.So, now it's time to create a PDF document using Aspose.Pdf so that we can add this newly generated barcode image to it.
Let's start by creating an instance of Pdf class and then add a Section to this Pdf instance as shown below:
[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()
After the barcode image is saved to a MemoryStream, we are done with the barcode image. The only thing now needed is to add this barcode image (saved in the MemoryStream) to the PDF document.So, now it's time to create a PDF document using Aspose.Pdf so that we can add this newly generated barcode image to it.
Let's start by creating an instance of Pdf class and then add a Section to this Pdf instance as shown below:
//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();
'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()
Adding Barcode Image to the PDF Document
Now, we can create an Image object inheriting characteristics from the Section added to the Pdf document. After the creation of Image object, it is required to set some properties of Image object. First of all ImageFileType property of the Image is set to MemoryBmp and OpenType property to ImageOpenType.Memory, indicating that a Bitmap (Bmp) image will be loaded from memory. As we know that our barcode image is stored in the MemoryStream as binary data. So, to read this binary formatted image, we use BinaryReader class to read image bytes from the MemoryStream by calling ReadBytes method of the BinaryReader class.
Once the image bytes are stored in MemoryData property of the Image object then it is added to the Paragraphs collection of the Section and finally the Pdf document is saved by calling its Save method as shown below:
[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")
If you only pass the name of the output PDF file to the Save method then the output PDF file will be created in the same folder where the assembly (exe or dll) will be located.
Now, we can create an Image object inheriting characteristics from the Section added to the Pdf document. After the creation of Image object, it is required to set some properties of Image object. First of all ImageFileType property of the Image is set to MemoryBmp and OpenType property to ImageOpenType.Memory, indicating that a Bitmap (Bmp) image will be loaded from memory. As we know that our barcode image is stored in the MemoryStream as binary data. So, to read this binary formatted image, we use BinaryReader class to read image bytes from the MemoryStream by calling ReadBytes method of the BinaryReader class.
Once the image bytes are stored in MemoryData property of the Image object then it is added to the Paragraphs collection of the Section and finally the Pdf document is saved by calling its Save method as shown below:
//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");
'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")
If you only pass the name of the output PDF file to the Save method then the output PDF file will be created in the same folder where the assembly (exe or dll) will be located.
Conclusion 
Aspose.BarCode can easily be integrated with any of the Aspose Components to create rich applications for fulfilling maximum requirements of the users. The most interesting thing is that any change in Aspose.BarCode version would not effect on it's integration with other Aspose Components because Aspose.Pdf only deals with the barcode image produced by the Aspose.BarCode and not its internal structure.
| Aspose.BarCode can easily be integrated with any of the Aspose Components to create rich applications for fulfilling maximum requirements of the users. The most interesting thing is that any change in Aspose.BarCode version would not effect on it's integration with other Aspose Components because Aspose.Pdf only deals with the barcode image produced by the Aspose.BarCode and not its internal structure. |
About Author
This article is written by Khawaja Salman Sarfraz (Team Leader, Aspose Pty Ltd.) who obtained his Masters degree in Computer Sciences. He has also been providing training and consultancy services on J2EE and .NET platforms.
Contact Author
Download Complete Source Code for this Article
This article is written by Khawaja Salman Sarfraz (Team Leader, Aspose Pty Ltd.) who obtained his Masters degree in Computer Sciences. He has also been providing training and consultancy services on J2EE and .NET platforms.
Contact Author
Download Complete Source Code for this Article

