Add QR Code to PowerPoint Presentation using C#

Add QR Code to PowerPoint Presentation using C#

PowerPoint is a popular tool used to present information in the form of a slide show. Where each slide contains specific information/ data in the form of text, list, image, video, graphs, etc. In a PowerPoint presentation, we can embed any specific URL, presenter’s social media account link, or an email address using a QR code. So that, people with a QR reader on their smartphone can capture the URL without having to write it down. In this article, we will learn how to add a QR code to a PowerPoint presentation using C#.

The following topics shall be covered in this article:

What is QR Code

A (Quick Response) QR code is a type of matrix barcode consisting of an array of black and white squares. As the name shows, the QR code allows the users to access information instantly when scanned. It is used to store encoded alphanumeric information that can be decoded by QR code readers available on digital devices, such as cell phones.

C# API to Add QR Code to PowerPoint Presentation

For generating a QR code and adding it to PPT or PPTX files, we will follow a two-step procedure. Firstly, we will be using the Aspose.Slides for .NET API to create or load a PowerPoint presentation, then we will generate and add the QR code image to the presentation slides using the Aspose.BarCode for .NET API.

Aspose.Slides for .NET API enables to read, write, protect, modify and convert presentations of the supported formats in .NET applications without using MS PowerPoint. It offers various classes to perform different operations. The Presentation class of the API allows creating a new presentation or loading an existing presentation file in the application. The Save() method of this class saves the presentation in the provided format on the given file path. The ISlide interface of the API represents a slide in a presentation.

We will be using the Aspose.BarCode for .NET API to generate a QR code. It allows generating various types of supported barcodes. For this purpose, it provides the BarcodeGenerator class to generate the QR code or barcode of the specified EncodeType. We can save the generated QR/barcode image using the Save() method of this class. The API also provides the BarCodeImageFormat enumeration to specify the save formats. We can read the barcode or QR code from images using the BarCodeReader class of the API.

Please either download the DLLs of the APIs or install them using NuGet.

PM> Install-Package Aspose.BarCode
PM> Install-Package Aspose.Cells

Generate and Add QR Code to Presentation in C#

We can create a new presentation and add a QR code image to the presentation slide by following the steps given below:

  1. Firstly, create an empty presentation using the Presentation class.
  2. Next, create an instance of the BarcodeGenerator class. It takes the Encode Type as an argument.
  3. Then, add text to encode and set other optional barcode properties.
  4. Next, create an instance of the memory stream object.
  5. Then, call the BarcodeGenerator.Save() method to save the QR code image to the memory stream.
  6. Next, call the AddImage() method to add the image to the presentation’s image collection.
  7. Then, call the InsertEmptySlide() method to insert an empty slide at the specified index with a title layout.
  8. Next, get the newly inserted slide by its index.
  9. After that, insert the QR code image on the slide using the AddPictureFrame() method.
  10. Optionally, set the picture frame formatting such as fill type, color, width, rotation, etc.
  11. Finally, call the Presentation.Save() method. It takes the output PPTX file path and the SaveFormat as arguments.

The following code example shows how to generate and add a QR to a new presentation slide using C#.

Generate and Add QR Code to Presentation in C#
Generate and Add QR Code to Presentation in C#.

Add QR Code to Existing Presentation in C#

We can load an existing presentation and add a QR code image by following the steps given below:

  1. Firstly, load an existing presentation using the Presentation class.
  2. Next, create an instance of the BarcodeGenerator class. It takes the Encode Type and text to encode as arguments.
  3. Then, create an instance of the memory stream object.
  4. After that, call the BarcodeGenerator.Save() method to save the QR code image to the memory stream.
  5. Next, call the AddImage() method to add the image to the presentation’s image collection.
  6. Then, get the specific slide by its index.
  7. After that, insert the QR code image on the slide using the AddPictureFrame() method.
  8. Finally, call the Presentation.Save() method. It takes the output PPTX file path and the SaveFormat as arguments.

The following code example shows how to generate and add a QR to an existing presentation using C#.

Add QR Code to Existing Presentation in C#
Add QR Code to Existing Presentation in C#.

Add Barcode to PowerPoint Presentation in C#

Similarly, we can also generate a barcode code image and add it to the presentation by following the steps mentioned earlier. However, we just need to set the EncodeType other than QR or GS1QR in the second step. We may also need to adjust the image position in step 7.

The following code example demonstrates how to add a barcode to a PowerPoint presentation using C#.

Add Barcode to PowerPoint Presentation in C#.
Add Barcode to PowerPoint Presentation in C#.

Read QR Code from Presentation in C#

We can recognize any barcode or QR code image available on any of the slides in the presentation by following the steps given below:

  1. Firstly, load an existing presentation using the Presentation class.
  2. Next, loop through all the images in the presentation’s image collection.
  3. Then, save the image to the stream.
  4. Next, create an instance of the BarCodeReader class with image stream and DecodeType as arguments.
  5. After that, call the ReadBarCodes() method to get the BarCodeResult object.
  6. Finally, show the QR code information.

The following code example shows how to read a QR code image from a presentation using C#.

Codetext found: https://www.aspose.com
Symbology: QR

Get a Free License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

In this article, we have learned how to:

  • create a PowerPoint presentation programmatically;
  • add a new slide in the presentation with a predefined slide layout;
  • generate a QR code image and add it to the presentation;
  • create a barcode and insert it into the presentation;
  • read a QR image from a PowerPoint presentation using C#.

Besides, you can learn more about Aspose.BarCode for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also