使用Barcode Professional SDK for .NET为PDF文件插入条码

作者:控件中国网   出处:控件中国网   2015-07-28 15:32:33   阅读:15

很多时候客户需要把条码插入到现有的PDF文件, Barcode Professional SDK for .NET为开发人员提供了该方法,这篇文章主要是介绍怎么使用iTextSharp为PDF文件插入条码,具体如下:
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();

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat