These sample source codes on this page below are demonstrating how to generate word document with barcodes in C#. What is ByteScout Barcode SDK? It is the robost library (Software Development Kit) that is designed for automatic generation of high-quality barcodes for printing, electronic documents and pdf. All popular barcode types are supported from Code 39 and Code 129 to QR Code, UPC, GS1, GS-128, Datamatrix, PDF417, Maxicode and many others. Provides support for full customization of fonts, colors, output and printing sizes. Special tools are included to verify output quality and printing quality. Can add generated barcode into new or existing documents, images and PDF. It can help you to generate word document with barcodes in your C# application.
The SDK samples like this one below explain how to quickly make your application do generate word document with barcodes in C# with the help of ByteScout Barcode SDK. Just copy and paste the code into your C# application’s code and follow the instruction. Use of ByteScout Barcode SDK in C# is also explained in the documentation included along with the product.
Trial version of ByteScout Barcode SDK is available for free. Source code samples are included to help you with your C# app.
On-demand (REST Web API) version:
Web API (on-demand version)
On-premise offline SDK for Windows:
60 Day Free Trial (on-premise)
// This example uses Word Automation to create a document, add some text, add a table,
// fill it with random data and generate barcode images for it.
using System;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Reflection;
using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
using Bytescout.BarCode;
namespace GenerateWordDocumentWithBarcodes
{
class Program
{
static void Main(string[] args)
{
object optional = Missing.Value;
object endOfDocBookmark = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
// start Word
_Application word = new Application();
word.Visible = false;
// create new document
_Document document = word.Documents.Add(ref optional, ref optional, ref optional, ref optional);
// insert a paragraph at the beginning of the document
Paragraph paragraph1 = document.Content.Paragraphs.Add(ref optional);
paragraph1.Range.Text = "Heading 1";
paragraph1.Range.Font.Bold = 1;
paragraph1.Format.SpaceAfter = 24; // 24 pt spacing after paragraph
paragraph1.Range.InsertParagraphAfter();
// insert another paragraph
object range = document.Bookmarks.get_Item(ref endOfDocBookmark).Range;
Paragraph paragraph2 = document.Content.Paragraphs.Add(ref range);
paragraph2.Range.Text = "This is a sentence of normal text. Now here is a table:";
paragraph2.Range.Font.Bold = 0;
paragraph2.Format.SpaceAfter = 24;
paragraph2.Range.InsertParagraphAfter();
// insert a 5 x 2 table, make the first header row bold and italic
range = document.Bookmarks.get_Item(ref endOfDocBookmark).Range;
Table table = document.Tables.Add((Range) range, 5, 2, ref optional, ref optional);
table.Cell(1, 1).Range.Text = "Value"; // 1st column header
table.Cell(1, 2).Range.Text = "Barcode"; // 2nd column header
table.Rows[1].Range.Font.Bold = 1;
table.Rows[1].Range.Font.Italic = 1;
AddBorders(table.Cell(1, 1).Range);
AddBorders(table.Cell(1, 2).Range);
Random random = new Random();
string tempImage = Path.Combine(Path.GetTempPath(), "tempImage.png");
// create barcode object
Barcode barcode = new Barcode("demo", "demo");
barcode.Symbology = SymbologyType.Code128;
barcode.DrawCaption = false;
// fill the table with random data and add barcode images
for (int row = 2; row <= 5; row++)
{
string randomValue = random.Next().ToString(CultureInfo.InvariantCulture);
Range cell = table.Cell(row, 1).Range;
cell.Text = randomValue;
AddBorders(cell);
// generate barcode and save it to temporary image file
barcode.Value = randomValue;
barcode.SaveImage(tempImage);
// put barcode image to second column
cell = table.Cell(row, 2).Range;
cell.InlineShapes.AddPicture(tempImage, ref optional, ref optional, ref optional);
AddBorders(cell);
}
// save document
object fileName = @"sample.doc"; // use full file path in your app
document.SaveAs(ref fileName, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional);
// quit Word
object saveChanges = true;
word.Quit(ref saveChanges, ref optional, ref optional);
System.Diagnostics.Process.Start((string) fileName);
}
// Adds borders to provided Range
static void AddBorders(Range cell)
{
cell.Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleSingle;
cell.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle;
cell.Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleSingle;
cell.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
}
}
}
60 Day Free Trial or Visit ByteScout Barcode SDK Home Page
Explore ByteScout Barcode SDK Documentation
Explore Samples
Sign Up for ByteScout Barcode SDK Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
60 Day Free Trial or Visit ByteScout Barcode SDK Home Page
Explore ByteScout Barcode SDK Documentation
Explore Samples
Sign Up for ByteScout Barcode SDK Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
also available as: