Home > Articles > C# Articles > How to print the PDF file and add barcodes to PDF in C#

How to print the PDF file and add barcodes to PDF in C#

by Jane Smith   on Jul 17, 2017   Category: C#  | Level: Intermediate  |  Views: 738    |  Points: 100   
Like this article? Bookmark and Share:
This article will demonstrate how to add a 1D barcode and a 2D barcode to the PDF file as where as we want and then print the PDF page in C#.

Introduction

Barcode is widely used to show data about the object which it is not easily to be read without special barcode scanner. It is universally used to automate supermarket checkout systems. We also know that PDF is very safe and can’t be edited easily. So draw the barcode on PDF page becomes more and more popular when we show information to others. This article will demonstrate how to add a 1D barcode and a 2D barcode to the PDF file as where as we want and then print the PDF page in C#.

Tools we need:

- Visual Studio

-Spire.PDF.dll

-Spire.Barcode.dll

Get Spire.PDF and Spire.Barcode from Free Spire.Office on NuGet easily.

Code snippets of how to draw Barcodes to the existing PDF file and then print the PDF page. Firstly, we need to generate the barcodes and save it as images. Then we need to insert the barcode images to the existing PDF file where we want. Finally, print the PDF through Microsoft XPS Document Writer (MXDW) and print it as a XPS file.

Step 1: Create a new PDFDocument instance and load the PDF from file.

PdfDocument doc = new PdfDocument();

doc.LoadFromFile("Sample.PDF");

//get the first page of the sample document

PdfPageBase page = doc.Pages[0];

Step 2: Generate a 1D and 2D barcodes and save them as image.

//Generate 1D barcode

BarcodeSettings bs = new BarcodeSettings();

bs.Type = BarCodeType.Code39;

bs.Data = "*ABC123*";

bs.ShowText = false;

BarCodeGenerator bg = new BarCodeGenerator(bs);

bg.GenerateImage().Save("Code39.png");

//Generate 2D barcode

BarcodeSettings settings = new BarcodeSettings();

settings.Type = BarCodeType.QRCode;

settings.Data = "Hello12345";

settings.Data2D = "QRCode";

settings.X = 1.5f;

settings.ShowText = true;

BarCodeGenerator generator = new BarCodeGenerator(settings);

generator.GenerateImage().Save("QRCode.png");

Step 3: Draw the barcode image to the PDF file and set its location.

PdfImage image1 = PdfImage.FromFile("Code39.png");

page.Canvas.DrawImage(image1, 0, 20, 140, 70);

PdfImage image2 = PdfImage.FromFile("QRCode.png");

page.Canvas.DrawImage(image2, 400, 250, 80, 80);

Step 5: Set the Printer to Microsoft XPS Document Writer (MXDW) and print it as a XPS file.

doc.PrintDocument.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";

doc.PrintDocument.PrinterSettings.PrintToFile = true;

doc.PrintDocument.PrinterSettings.PrintFileName = "PrintPDF.xps";

doc.PrintDocument.Print();

Effective screenshot:



Full codes of print the barcode image in C#

static void Main(string[] args)

{

    PdfDocument doc = new PdfDocument();

    doc.LoadFromFile("Sample.PDF");

    PdfPageBase page = doc.Pages[0];

    //add 1D barcode

    BarcodeSettings bs = new BarcodeSettings();

    bs.Type = BarCodeType.Code39;

    bs.Data = "*ABC123*";

    bs.ShowText = false;

    BarCodeGenerator bg = new BarCodeGenerator(bs);

    bg.GenerateImage().Save("Code39.png");

    //draw 2D barcode

    BarcodeSettings settings = new BarcodeSettings();

    settings.Type = BarCodeType.QRCode;

    settings.Data = "Hello12345";

    settings.Data2D = "QRCode";

    settings.X = 1.5f;

    settings.ShowText = true;

    BarCodeGenerator generator = new BarCodeGenerator(settings);

    generator.GenerateImage().Save("QRCode.png");

    //Draw the image to pdf

    PdfImage image1 = PdfImage.FromFile("Code39.png");

    page.Canvas.DrawImage(image1, 0, 20, 140, 70);

 

 

    PdfImage image2 = PdfImage.FromFile("QRCode.png");

    page.Canvas.DrawImage(image2, 400, 250, 80, 80);

    //print the PDF file

    doc.PrintDocument.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";

    doc.PrintDocument.PrinterSettings.PrintToFile = true;

    doc.PrintDocument.PrinterSettings.PrintFileName = "PrintPDF.xps";

    doc.PrintDocument.Print();

}  

 




Like this article? Bookmark and Share:

Most viewed Articles

User Comments


No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback

Latest Posts

1 Mature purlieus New! Forum