How to generate barcode and sign document using C#
Barcodes represent textual information as a quite small image which could be automatically scanned. Such approach brings new opportunities in computer processing of documents like sorting or moving through a business work-flow. In this article, we will discuss useful way to sign electronic documents with data encrypted in Bar or QR codes using C# in .NET applications.
There are following topics could be read in this article:
- Native .NET API for Electronic Signatures
- How to sign PDF files with barcode
- Signing document with Codabar in C#
- Signing document with Event QR-code in C#
- Barcode and Qr-code image generation
Native .NET API for Electronic Signatures
GroupDocs.Signature for .NET provides API for signing wide range of documents formats like PDF, MS Word, MS Presentations, PNG, JPEG, and other. Moreover, API includes special abilities for additional documents content processing. Supported formats are PDF, MS Word, MS Presentations, MS Excel, PNG, JPEG, and many others.
Use the downloads section to obtain API DLLs or MSI installer or NuGet:
PM> Install-Package GroupDocs.Signature
How to sign PDF files with barcode
It is common problem to add additional data in various types of business documents. Such document may looks as in the picture below. The one of the most useful ways is to place a barcode directly on the document page.
Signing document with Codabar in C#
To sign a particular document with just generated barcode:
- Instantiate Signature class providing path to source document or document stream.
- Create the BarcodeSignOptions instance and set up all demanded fields.
- Invoke Sign method to process the document, providing output file path and sign options.
// instantiating the signature object
using (Signature signature = new Signature(@"source.pdf"))
{
// setup barcode signature options
BarcodeSignOptions signOptions = new BarcodeSignOptions()
{
HorizontalAlignment = HorizontalAlignment.Right,
Top = 150,
EncodeType = BarcodeTypes.Codabar,
Text = "Approved_19/06/2022"
};
// sign document
signature.Sign(@"signed_codabar.pdf", signOptions);
}
Signed with Codabar document might looks like in the picture below. Codabar format was developed for printed documents and might be useful in office document flow.
GroupDocs.Signature App provides opportunity to try document signing with barcodes for free.
GroupDocs.Signature App gives opportunity to try document signing with QR-codes for free.
Barcode and image generation in C#
Another way to improve documents is to generate barcode firstly and add it to documents using third-party tools. For this case it is possible to generate code as an image.
- Create the BarcodeSignOptions instance and set up all demanded fields.
- Instantiate the PreviewSignatureOptions providing methods for creation and releasing.
- Invoke GenerateSignaturePreview method to obtain barcode image as a stream.
- Use result barcode stream in any possible way.
MemoryStream result = new MemoryStream();
// setup barcode signature options
BarcodeSignOptions signOptions = new BarcodeSignOptions()
{
EncodeType = BarcodeTypes.Code93Extended,
Text = "Case 148.01"
};
//Instantiate preview options
var previewOptions = new PreviewSignatureOptions(
signOptions,
delegate (PreviewSignatureOptions options) { return result; },
delegate (PreviewSignatureOptions options, Stream signatureStream) { }
);
//Generate image to stream
GroupDocs.Signature.Signature.GenerateSignaturePreview(previewOptions);
Image containing generated barcode might looks in this way:
Get a Free API License
In order to use the API without evaluation limitations, you can get a free temporary license.
Conclusion
To sum up, some useful ways of processing documents and barcodes were discussed in this article. Using C# with .NET applications improves productivity of such actions dramatically. In addition, you can use Online Applications to sign your files from GroupDocs.Signature for free.
Moreover, it is possible to learn how to use GroupDocs.Signature in your .NET applications with documentation. Also, you may discuss any questions or issues at Groupdocs forum.