彩票走势图

Barcode Professional SDK for .NET使用教程(四):在PDF文件中插入高质量条形码

原创|使用教程|编辑:龚雪|2016-03-17 16:44:49.000|阅读 908 次

概述:本次教程将向您展示如何使用Barcode Professional SDK for .NET在PDF文档中插入EAN/UPC复合型条形码,2D PDF417条形码和GS1线性条形码。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

相关链接:

通过Barcode Professional SDK生成的条形码具有高分辨率以使其获得更利于输出打印的质量。

以下为PDF文档插入条形码前后的图像:

发票

详细步骤:

  1. 下载并安装最新版Barcode Professional SDK for .NET
  2. 下载并安装
  3. 打开Visual Studio新建一个项目,例如,一个控制台应用程序
  4. 在创建的项目中,添加一个引用到itextsharp.dllNeodynamic.SDK.Barcode.dll程序集
  5. 下载PDF文档示例到C:\temp,命名为SampleDoc.pdf
  6. 复制/粘贴以下代码在您的项目中:

VB

Dim pdfFile As String = "C:\Temp\SampleDoc.pdf"
Dim pdfFileOut As String = "C:\Temp\SampleDoc_barcode.pdf"
Dim dpi As Integer = 300

'1. Open the PDF file using a PdfReader
Dim pdfReader As New iTextSharp.text.pdf.PdfReader(pdfFile)
'2. Create a PdfStamper object for inserting or stamping the barcodes...
Dim pdfStamper As New iTextSharp.text.pdf.PdfStamper(pdfReader, new System.IO.FileStream(pdfFileOut, System.IO.FileMode.Create, System.IO.FileAccess.Write))
'3. Get the page content from the PDF file by creating a PdfContentByte object           
Dim pdfPage As iTextSharp.text.pdf.PdfContentByte = pdfStamper.GetOverContent(1)
Dim pageWidth As Single = pdfReader.GetPageSize(1).Width
Dim pageHeight As Single = pdfReader.GetPageSize(1).Height
'4. Generate the barcode images using Barcode Professional SDK

'4.1. Generate the UPC-A CCB composite barcode.
Using bc1 As New Neodynamic.SDK.Barcode.BarcodeProfessional()
    'set the desired barcode symbology e.g. UPC-A CCB
    bc1.Symbology = Neodynamic.SDK.Barcode.Symbology.UpcACCB
    'set the value to encode e.g. Primary Data = 01234567890 and Secondary Data = 991234-abcd
    bc1.Code = "01234567890|991234-abcd"
    'set the barcode unit and sizes...
    bc1.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch
    bc1.BarWidth = 0.013 'the narrow bar width a.k.a. X value
    bc1.BarHeight = 1 'the height of the barcode bars 
    bc1.GuardBarHeight = bc1.BarHeight + 5 * bc1.BarWidth 'the height of the guard bars only available for EAN/UPC barcodes
    bc1.QuietZoneWidth = 9 * bc1.BarWidth 'the quiet zone for UPC-A is 9 times the BarWidth value per GS1 spec

    'Generate the barcode image content for inserting it into the PDF page
    'For high quality, you can generated the barcode at, for instance, 300 dpi
    Dim barcodeImage1 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bc1.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png, dpi))
    'set the position of the barcode image. PDF uses Points as unit (1 point = 1/72 inch)
    'the starting point (x=0, y=0) in the PDF coord is the bottom-left corner!!!
    barcodeImage1.SetAbsolutePosition(pageWidth - 4.25F * 72F + ((3.25F - (barcodeImage1.Width / dpi))/2) * 72F , pageHeight - ((barcodeImage1.Height / dpi) * 72F) - (2.5F * 72F))
    'scale the image based on the image dpi
    barcodeImage1.ScalePercent(72F / CSng(dpi) * 100F)
    'add the image object to the pdf page
    pdfPage.AddImage(barcodeImage1)            
End Using

'4.2. Generate the 2D Micro PDF417 barcode.
Using bc2 As New Neodynamic.SDK.Barcode.BarcodeProfessional()
    'set the desired barcode symbology e.g. Micro PDF417
    bc2.Symbology = Neodynamic.SDK.Barcode.Symbology.MicroPdf417
    'set the value to encode e.g. ABCDE-1234567890-PDF
    bc2.Code = "ABCDE-1234567890-PDF"
    'set the barcode unit and sizes...
    bc2.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch
    bc2.BarWidth = 0.01 'the narrow bar width a.k.a. X value
    bc2.BarRatio = 3 'in MicroPdf417 each bars' heigh in a row is calculated as BarWidth * BarRatio
                
    'Generate the barcode image content for inserting it into the PDF page
    'For high quality, you can generated the barcode at, for instance, 300 dpi
    Dim barcodeImage2 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bc2.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png, dpi))
    'set the position of the barcode image. PDF uses Points as unit (1 point = 1/72 inch)
    'the starting point (x=0, y=0) in the PDF coord is the bottom-left corner!!!
    barcodeImage2.SetAbsolutePosition(72F + ((3.25F - (barcodeImage2.Width / dpi)) / 2) * 72F, pageHeight - ((barcodeImage2.Height / dpi) * 72F) - (6F * 72F));
    'scale the image based on the image dpi
    barcodeImage2.ScalePercent(72F / CSng(dpi) * 100F)
    'add the image object to the pdf page
    pdfPage.AddImage(barcodeImage2)
End Using

'4.3. Generate the GS1 DataBar-14 Stacked barcode.
Using bc3 As New Neodynamic.SDK.Barcode.BarcodeProfessional()
    'set the desired barcode symbology e.g. DataBar-14 stacked
    bc3.Symbology = Neodynamic.SDK.Barcode.Symbology.GS1DataBar14Stacked
    'set the value to encode e.g. 0061414199999
    bc3.Code = "0061414199999"
    'set the barcode unit and sizes...
    bc3.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch
    bc3.BarWidth = 0.0208 'the narrow bar width a.k.a. X value
                
    'Generate the barcode image content for inserting it into the PDF page
    'For high quality, you can generated the barcode at, for instance, 300 dpi
    Dim barcodeImage3 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bc3.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png, dpi))
    'set the position of the barcode image. PDF uses Points as unit (1 point = 1/72 inch)
    'the starting point (x=0, y=0) in the PDF coord is the bottom-left corner!!!
    barcodeImage3.SetAbsolutePosition(pageWidth - 4.25F * 72F + ((3.25F - (barcodeImage3.Width / dpi)) / 2) * 72F, pageHeight - ((barcodeImage3.Height / dpi) * 72F) - (6f * 72F));
    'scale the image based on the image dpi
    barcodeImage3.ScalePercent(72F / CSng(dpi) * 100F)
    'add the image object to the pdf page
    pdfPage.AddImage(barcodeImage3)
End Using

'close PdfStamper
pdfStamper.Close()
'close PdfReader
pdfReader.Close()

													

 

C#

string pdfFile = @"C:\Temp\SampleDoc.pdf";
string pdfFileOut = @"C:\Temp\SampleDoc_barcode.pdf";
int dpi = 300;

//1. Open the PDF file using a PdfReader
iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(pdfFile);
//2. Create a PdfStamper object for inserting or stamping the barcodes...
iTextSharp.text.pdf.PdfStamper pdfStamper = new iTextSharp.text.pdf.PdfStamper(pdfReader, new System.IO.FileStream(pdfFileOut, System.IO.FileMode.Create, System.IO.FileAccess.Write));
//3. Get the page content from the PDF file by creating a PdfContentByte object           
iTextSharp.text.pdf.PdfContentByte pdfPage = pdfStamper.GetOverContent(1);
float pageWidth = pdfReader.GetPageSize(1).Width;
float pageHeight = pdfReader.GetPageSize(1).Height;
//4. Generate the barcode images using Barcode Professional SDK

//4.1. Generate the UPC-A CCB composite barcode.
using (Neodynamic.SDK.Barcode.BarcodeProfessional bc1 = new Neodynamic.SDK.Barcode.BarcodeProfessional())
{
    //set the desired barcode symbology e.g. UPC-A CCB
    bc1.Symbology = Neodynamic.SDK.Barcode.Symbology.UpcACCB;
    //set the value to encode e.g. Primary Data = 01234567890 and Secondary Data = 991234-abcd
    bc1.Code = "01234567890|991234-abcd";
    //set the barcode unit and sizes...
    bc1.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch;
    bc1.BarWidth = 0.013; //the narrow bar width a.k.a. X value
    bc1.BarHeight = 1; //the height of the barcode bars 
    bc1.GuardBarHeight = bc1.BarHeight + 5 * bc1.BarWidth; //the height of the guard bars only available for EAN/UPC barcodes
    bc1.QuietZoneWidth = 9 * bc1.BarWidth; //the quiet zone for UPC-A is 9 times the BarWidth value per GS1 spec

    //Generate the barcode image content for inserting it into the PDF page
    //For high quality, you can generated the barcode at, for instance, 300 dpi
    iTextSharp.text.Image barcodeImage1 = iTextSharp.text.Image.GetInstance(bc1.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png, dpi));
    //set the position of the barcode image. PDF uses Points as unit (1 point = 1/72 inch)
    //the starting point (x=0, y=0) in the PDF coord is the bottom-left corner!!!
    barcodeImage1.SetAbsolutePosition(pageWidth - 4.25f * 72f + ((3.25f - (barcodeImage1.Width / dpi))/2) * 72f , pageHeight - ((barcodeImage1.Height / dpi) * 72f) - (2.5f * 72f));
    //scale the image based on the image dpi
    barcodeImage1.ScalePercent(72f / (float)dpi * 100f);
    //add the image object to the pdf page
    pdfPage.AddImage(barcodeImage1);                
}

//4.2. Generate the 2D Micro PDF417 barcode.
using (Neodynamic.SDK.Barcode.BarcodeProfessional bc2 = new Neodynamic.SDK.Barcode.BarcodeProfessional())
{
    //set the desired barcode symbology e.g. Micro PDF417
    bc2.Symbology = Neodynamic.SDK.Barcode.Symbology.MicroPdf417;
    //set the value to encode e.g. ABCDE-1234567890-PDF
    bc2.Code = "ABCDE-1234567890-PDF";
    //set the barcode unit and sizes...
    bc2.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch;
    bc2.BarWidth = 0.01; //the narrow bar width a.k.a. X value
    bc2.BarRatio = 3; //in MicroPdf417 each bars' heigh in a row is calculated as BarWidth * BarRatio
                
    //Generate the barcode image content for inserting it into the PDF page
    //For high quality, you can generated the barcode at, for instance, 300 dpi
    iTextSharp.text.Image barcodeImage2 = iTextSharp.text.Image.GetInstance(bc2.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png, dpi));
    //set the position of the barcode image. PDF uses Points as unit (1 point = 1/72 inch)
    //the starting point (x=0, y=0) in the PDF coord is the bottom-left corner!!!
    barcodeImage2.SetAbsolutePosition(72f + ((3.25f - (barcodeImage2.Width / dpi)) / 2) * 72f, pageHeight - ((barcodeImage2.Height / dpi) * 72f) - (6f * 72f));
    //scale the image based on the image dpi
    barcodeImage2.ScalePercent(72f / (float)dpi * 100f);
    //add the image object to the pdf page
    pdfPage.AddImage(barcodeImage2);
}

//4.3. Generate the GS1 DataBar-14 Stacked barcode.
using (Neodynamic.SDK.Barcode.BarcodeProfessional bc3 = new Neodynamic.SDK.Barcode.BarcodeProfessional())
{
    //set the desired barcode symbology e.g. DataBar-14 stacked
    bc3.Symbology = Neodynamic.SDK.Barcode.Symbology.GS1DataBar14Stacked;
    //set the value to encode e.g. 0061414199999
    bc3.Code = "0061414199999";
    //set the barcode unit and sizes...
    bc3.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch;
    bc3.BarWidth = 0.0208; //the narrow bar width a.k.a. X value
                
    //Generate the barcode image content for inserting it into the PDF page
    //For high quality, you can generated the barcode at, for instance, 300 dpi
    iTextSharp.text.Image barcodeImage3 = iTextSharp.text.Image.GetInstance(bc3.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png, dpi));
    //set the position of the barcode image. PDF uses Points as unit (1 point = 1/72 inch)
    //the starting point (x=0, y=0) in the PDF coord is the bottom-left corner!!!
    barcodeImage3.SetAbsolutePosition(pageWidth - 4.25f * 72f + ((3.25f - (barcodeImage3.Width / dpi)) / 2) * 72f, pageHeight - ((barcodeImage3.Height / dpi) * 72f) - (6f * 72f));
    //scale the image based on the image dpi
    barcodeImage3.ScalePercent(72f / (float)dpi * 100f);
    //add the image object to the pdf page
    pdfPage.AddImage(barcodeImage3);
}

//close PdfStamper
pdfStamper.Close();
//close PdfReader
pdfReader.Close();

													

 


标签:条码生成

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


为你推荐

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


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP