1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#でQRコードを生成し、PDF・Word・Excelに挿入する方法

1
Posted at

QRコードは、リンク誘導、個人認証、トラッキングなど多くの用途で使用されています。この記事では、C#を使用して、ロゴ付きQRコードの作成と、作成したQRコードをPDF・Word・Excelに挿入する方法を紹介します。

  • C#でロゴ付きQRコードを生成する
  • QRコードをPDF、Word、Excelに挿入する

使用ライブラリ:Free Spire.Barcode for .NET(QRコードの生成用)


C#でロゴ付きQRコードを生成する

Spire.Barcodeを使えば、QRコードのテキスト、エンコーディング、エラーレベル、ロゴ挿入などを簡単に設定できます。

主な手順:

  1. QRコードの内容やスタイルを設定
  2. ロゴ画像を中心に埋め込み
  3. QRコードを画像として生成・保存

サンプルコード:

using Spire.Barcode;
using System.Drawing;
using System.Drawing.Imaging;

class Program
{
    static void Main(string[] args)
    {
        BarcodeSettings settings = new BarcodeSettings();
        settings.Type = BarCodeType.QRCode;
        settings.Data = "https://www.example.com/";
        settings.Data2D = "Scan to go to example.com";
        settings.ShowTextOnBottom = true;
        settings.TextFont = new Font(FontFamily.GenericSansSerif, 16f);
        settings.QRCodeDataMode = QRCodeDataMode.Auto;
        settings.QRCodeECL = QRCodeECL.H; // 高エラーレベル(ロゴ対応)
        settings.QRCodeLogoImage = Image.FromFile("Logo.png"); // ロゴを挿入
        settings.X = 3.0f; // モジュールサイズ

        BarCodeGenerator generator = new BarCodeGenerator(settings);
        Image qr = generator.GenerateImage();
        qr.Save("QR Code.png", ImageFormat.Png);
    }
}

出力例:

QR Code.png

中央にロゴを含み、下部にテキスト付きのQRコードが生成されます。


QRコードをPDF・Word・Excelに挿入する

生成したQRコード画像は、PDF、Word、Excelなど様々な形式のドキュメントに埋め込むことができます。

PDFに挿入(Free Spire.PDF)

PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.AppendPage();

// QRコード画像を読み込み
Image qr = Image.FromFile("QR Code.png");

// 指定位置 (100, 400) に100×100サイズで挿入
page.Canvas.DrawImage(PdfImage.FromImage(qr), 100, 400, 100, 100);

pdf.SaveToFile("output.pdf");

Wordに挿入(Free Spire.Doc)

Document doc = new Document();
Section section = doc.AddSection();
Paragraph para = section.AddParagraph();

// 段落にQRコード画像を追加
DocPicture picture = para.AppendPicture(Image.FromFile("QR Code.png"));

doc.SaveToFile("output.docx", FileFormat.Docx);

Excelに挿入(Free Spire.XLS)

Workbook book = new Workbook();
Worksheet sheet = book.Worksheets[0];

// C3セル(行2, 列2)にQRコード画像を挿入
sheet.Pictures.Add(2, 2, Image.FromFile("QR Code.png"));

book.SaveToFile("output.xlsx", ExcelVersion.Version2016);

まとめ

Spireライブラリを利用することで、C#で以下の機能を効率的に実現できます:

  • ロゴ付きQRコードの生成
  • PDF・Word・Excel文書への挿入
  • 高品質な画像出力と幅広い用途対応

ビジネス文書、電子チケット、商品ラベルなど様々なシーンで応用可能です。開発や自動化ツールへの組み込みにも最適です。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?