C#/ VB.NET Add barcode and QR code in word

Time:2021-2-18

This paper introduces how to use C # and VB.NET Code to add barcode and QR code in word document. Code will be divided into the word body paragraph, header and footers medium situation to add.

Using tool: Free Spire.Office For. Net (free version)

Tool introduction:This is the collection package of free products under all. Net platforms of spire, including Spire.Barcode.dll 、 Spire.DataExport.dll 、 Spire.Pdf.dll 、 Spire.Doc.dll 、 Spire.DocViewer.Forms .dll 、 Spire.PdfViewer.Forms .dll 、 Spire.Presentation.dll 、 Spire.XLS.dll DLL can be used to operate word / PDF / Excel / PPT and other files.

In this paper, adding bar code and two-dimensional code needs to add reference in vs programSpire.Doc.dll AndSpire.Barcode.dll These two DLL files.

DLL add reference effect:

 

 

 

be careful:

1. needdownloadInstall to the local specified path. The DLL file can be obtained in the bin folder under the installation path.

2. The generated bar code and two-dimensional code will have watermark words, which can be downloaded throughThis methodremove.

 


 

Code examples

1. Add barcode to word

C#

using Spire.Doc;
using Spire.Barcode;
using System.Drawing;
using System.IO;
using Spire.Doc.Documents;

namespace AddBarcode
{
    class Program
    {
        static void Main(string[] args)
        {
            //Call the method applykey (string key) to remove the watermark text
            Spire.Barcode.BarcodeSettings . applykey ("enter the serial number to remove the watermark here");

            //Create a document object and load a word document
            Document doc = new Document();
            doc.LoadFromFile("test.docx");

            //Get section 2
            Section section = doc.Sections[1];

            //Use Spire.Barcode The barcodesettings and barcodegenerator classes of create barcodes and save them as images
            BarcodeSettings settings = new BarcodeSettings();
            settings.Type = BarCodeType.Code128;
            settings.Data = "123456789";
            settings.Data2D = "123456789";
            settings.ShowText = false;
            settings.BarHeight = 4;
            settings.X = 0.3f;
            settings.HasBorder = true;
            settings.BorderWidth = 0.5f;
            settings.BorderColor = Color.AliceBlue;
            settings.BackColor = Color.Wheat;
            BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
            Image Image = barCodeGenerator.GenerateImage();
            // Image.Save (" Barcode.png ",  System.Drawing.Imaging . ImageFormat.Png ); // if you need to save the generated barcode image, you can perform this step

            //Add barcode to text paragraph
            Paragraph paragraph = section.AddParagraph();
            paragraph.Text  ="Receiving code":;
            paragraph.AppendPicture(Image);
            paragraph.Format.HorizontalAlignment = HorizontalAlignment.Right;

            //Add barcode image to word footers
            HeaderFooter footer = section.HeadersFooters.Footer;
            Paragraph footerpara = footer.AddParagraph();
            footerpara.Text  ="Scan the code to know the true and false":;
            footerpara.AppendPicture(Image);
            footerpara.Format.HorizontalAlignment = HorizontalAlignment.Left;

            //Save document
            doc.SaveToFile("BarCodeToWord.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("BarCodeToWord.docx");
        }
    }
}

Barcode adding effect:

 

VB.NET

Imports Spire.Doc
Imports Spire.Barcode
Imports System.Drawing
Imports System.IO
Imports Spire.Doc.Documents


Namespace AddBarcode
    Class Program
        Private Shared Sub Main(args As String())
            'call the method applykey (string key) to remove the watermark text
            Spire.Barcode.BarcodeSettings . applykey ("enter the serial number to remove the watermark here")

            'create a document object and load a word document
            Dim doc As New Document()
            doc.LoadFromFile("test.docx")

            'get section 2
            Dim section As Section = doc.Sections(1)

            'use Spire.Barcode The barcodesettings and barcodegenerator classes of create barcodes and save them as images
            Dim settings As New BarcodeSettings()
            settings.Type = BarCodeType.Code128
            settings.Data = "123456789"
            settings.Data2D = "123456789"
            settings.ShowText = False
            settings.BarHeight = 4
            settings.X = 0.3F
            settings.HasBorder = True
            settings.BorderWidth = 0.5F
            settings.BorderColor = Color.AliceBlue
            settings.BackColor = Color.Wheat
            Dim barCodeGenerator As New BarCodeGenerator(settings)
            Dim Image As Image = barCodeGenerator.GenerateImage()
            ' Image.Save (" Barcode.png ",  System.Drawing.Imaging . ImageFormat.Png ); // if you need to save the generated barcode image, you can perform this step

            'add barcode to body paragraph
            Dim paragraph As Paragraph = section.AddParagraph()
            paragraph.Text  =Receiving code:
            paragraph.AppendPicture(Image)
            paragraph.Format.HorizontalAlignment = HorizontalAlignment.Right

            'add barcode image to word footers
            Dim footer As HeaderFooter = section.HeadersFooters.Footer
            Dim footerpara As Paragraph = footer.AddParagraph()
            footerpara.Text  ="Scan the code to see whether it's true or false":
            footerpara.AppendPicture(Image)
            footerpara.Format.HorizontalAlignment = HorizontalAlignment.Left

            'save the document
            doc.SaveToFile("BarCodeToWord.docx", FileFormat.Docx2013)
            System.Diagnostics.Process.Start("BarCodeToWord.docx")
        End Sub
    End Class
End Namespace

 

2. Add QR code to word

C#

using Spire.Doc;
using Spire.Barcode;
using System.Drawing;
using System.IO;
using Spire.Doc.Documents;
using System;


namespace AddQRCode
{
    class Program
    {
        static void Main(string[] args)
        {
            //Call the method applykey (string key) to remove the watermark text
            Spire.Barcode.BarcodeSettings . applykey ("enter the serial number to remove the watermark here");

            //Create a document object and load a word document
            Document doc = new Document();
            doc.LoadFromFile("test.docx");

            //Get section 2
            Section section = doc.Sections[1];

            //Use Spire.Barcode The barcodesettings and barcodegenerator classes of create a QR code and save it as an image
            BarcodeSettings settings = new BarcodeSettings();
            settings.Type = BarCodeType.QRCode;
            settings.ImageWidth = 50;
            settings.ImageHeight = 50;
            settings.Data = "123456";
            settings.Data2D = "123456";
            settings.X =0.7f;
            settings.LeftMargin = 1;
            settings.ShowTextOnBottom = true;
            settings.QRCodeECL = QRCodeECL.Q;
            settings.QRCodeDataMode = QRCodeDataMode.Numeric;
            BarCodeGenerator generator = new BarCodeGenerator(settings);
            Image image = generator.GenerateImage();
            // image.Save (" QRCode.png ",  System.Drawing.Imaging . ImageFormat.Png ); // if you need to save the generated QR code image, you can perform this step

            //Add QR code to text paragraph
            Paragraph paragraph = section.AddParagraph();          
            paragraph.AppendPicture(image);
            paragraph.Format.HorizontalAlignment = HorizontalAlignment.Right;

            //Add QR code picture to word header
            HeaderFooter header = section.HeadersFooters.Header;
            //HeaderFooter footer =  section.HeadersFooters.Footer ; // get page footers
            Paragraph headerpara = header.AddParagraph();
            headerpara.AppendPicture(image);
            headerpara.Format.HorizontalAlignment = HorizontalAlignment.Center;
            

            //Save document
            doc.SaveToFile("QRCodeToHeader.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("QRCodeToHeader.docx");
        }
    }
}

QR code adding effect:

 

VB.NET

Imports Spire.Doc
Imports Spire.Barcode
Imports System.Drawing
Imports System.IO
Imports Spire.Doc.Documents


Namespace AddQRCode
    Class Program
        Private Shared Sub Main(args As String())
            'call the method applykey (string key) to remove the watermark text
            Spire.Barcode.BarcodeSettings . applykey ("enter the serial number to remove the watermark here")

            'create a document object and load a word document
            Dim doc As New Document()
            doc.LoadFromFile("test.docx")

            'get section 2
            Dim section As Section = doc.Sections(1)

            'use Spire.Barcode The barcodesettings and barcodegenerator classes of create a QR code and save it as an image
            Dim settings As New BarcodeSettings()
            settings.Type = BarCodeType.QRCode
            settings.ImageWidth = 50
            settings.ImageHeight = 50
            settings.Data = "123456"
            settings.Data2D = "123456"
            settings.X = 0.7F
            settings.LeftMargin = 1
            settings.ShowTextOnBottom = True
            settings.QRCodeECL = QRCodeECL.Q
            settings.QRCodeDataMode = QRCodeDataMode.Numeric
            Dim generator As New BarCodeGenerator(settings)
            Dim image As Image = generator.GenerateImage()
            ' image.Save (" QRCode.png ",  System.Drawing.Imaging . ImageFormat.Png ); // if you need to save the generated QR code image, you can perform this step

            'add QR code to text paragraph
            Dim paragraph As Paragraph = section.AddParagraph()
            paragraph.AppendPicture(image)
            paragraph.Format.HorizontalAlignment = HorizontalAlignment.Right

            'add QR code picture to word header
            Dim header As HeaderFooter = section.HeadersFooters.Header
            'HeaderFooter footer =  section.HeadersFooters.Footer ; // get page footers
            Dim headerpara As Paragraph = header.AddParagraph()
            headerpara.AppendPicture(image)
            headerpara.Format.HorizontalAlignment = HorizontalAlignment.Center


            'save the document
            doc.SaveToFile("QRCodeToHeader.docx", FileFormat.Docx2013)
            System.Diagnostics.Process.Start("QRCodeToHeader.docx")
        End Sub
    End Class
End Namespace

 

(end)

Recommended Today

The selector returned by ngrx store createselector performs one-step debugging of fetching logic

Test source code: import { Component } from ‘@angular/core’; import { createSelector } from ‘@ngrx/store’; export interface State { counter1: number; counter2: number; } export const selectCounter1 = (state: State) => state.counter1; export const selectCounter2 = (state: State) => state.counter2; export const selectTotal = createSelector( selectCounter1, selectCounter2, (counter1, counter2) => counter1 + counter2 ); // […]