SupportedBarcodes.cs
// 
// このコードは、DioDocs for PDF のサンプルの一部として提供されています。
// © MESCIUS inc. All rights reserved.
// 
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Barcode;

namespace DsPdfWeb.Demos.Barcodes
{
    // ライブラリで出力可能なすべてのバーコードシンボルのサンプルを描画します。
    public class SupportedBarcodes
    {
        public int CreatePDF(Stream stream)
        {
            // レイアウトパラメーター
            const float margin = 72 / 2;
            const float pad = 4;
            const float gap = 10;
            
            var doc = new GcPdfDocument();
            Page page;
            GcGraphics g;
            PointF ip;
            // 新しいページの追加、挿入ポイントの初期位置を設定
            void newPage()
            {
                page = doc.NewPage();
                g = page.Graphics;
                ip = new PointF(margin, margin);
            }
            newPage();
            // バーコードの名前と値を表示するためのTextFormat
            var tfCaption = new TextFormat()
            {
                Font = StandardFonts.Times,
                FontSize = 12,
            };
            var tfBarcode = new TextFormat()
            {
                Font = StandardFonts.Helvetica,
                FontSize = 9,
            };
            // バーコードインスタンス
            // 変更されないプロパティを設定
            var barcode = new GcBarcode()
            {
                TextFormat = tfBarcode,
                ScaleFactor = 1.5f,
            };
            barcode.Options.CaptionPosition = BarCodeCaptionPosition.Below;
            barcode.Options.SizeOptions.NarrowWideRatio = 0;
            // 指定バーコードタイプとオプションのCC-Aを使用してバーコードを描画
            void drawBarcode(CodeType symbology, string value, string cca)
            {
                // バーコードのシンボル、値、その他のプロパティを設定
                var caption = $"{symbology}:\r\n{value}";
                if (string.IsNullOrEmpty(cca))
                    barcode.Options.GS1Composite.Type = GS1CompositeType.None;
                else
                {
                    // 複合コンポーネント A (CC-A) の指定
                    caption += $"\r\nDependent CCA: {cca}";
                    barcode.Options.GS1Composite.Type = GS1CompositeType.CCA;
                    barcode.Options.GS1Composite.Value = cca;
                }
                // すべてのバーコードがチェックサムをサポートしているわけではありません
                barcode.Options.CheckSumEnabled = symbology != CodeType.Code25intlv && symbology != CodeType.Code_2_of_5 && symbology != CodeType.Matrix_2_of_5;
                var csize = g.MeasureString(caption, tfCaption);
                barcode.CodeType = symbology;
                barcode.Text = value;
                var size = g.MeasureBarcode(barcode);
                size.Height = Math.Max(size.Height, csize.Height);
                var border = new RectangleF(ip, new SizeF(page.Size.Width - margin * 2, size.Height + pad * 2));
                // 必要に応じて新しいページを追加
                if (ip.Y + border.Height > page.Size.Height - margin)
                {
                    newPage();
                    border = new RectangleF(ip, border.Size);
                }
                // バーコードを描画
                g.DrawRectangle(border, Color.Gray);
                g.DrawString(caption, tfCaption, new PointF(border.Left + pad, border.Top + pad));
                g.DrawBarcode(barcode, new RectangleF(border.Right - size.Width - pad, border.Top + pad, size.Width, size.Height));
                ip.Y = border.Bottom + gap;
            }
            // サポートされているすべてのバーコードシンボルを描画
            drawBarcode(CodeType.Ansi39, "*DIODOCS*", null);
            drawBarcode(CodeType.Ansi39x, "*DioDocs*", null);
            drawBarcode(CodeType.Codabar, "A12041961D", null);
            drawBarcode(CodeType.Code25intlv, "1234567890", null); //2 Interleaved 2 of 5 (ITF)
            drawBarcode(CodeType.Code39, "*DSBARCODE*", null);
            drawBarcode(CodeType.Code39x, "*DioDocs*", null);
            drawBarcode(CodeType.Code49, "MESCIUS+DioDocs", null);
            drawBarcode(CodeType.Code93x, "MESCIUS+DioDocs", null);
            drawBarcode(CodeType.Code_93, "DSBARCODE", null);
            drawBarcode(CodeType.Code_128_A, "DSPDF-2023", null);
            drawBarcode(CodeType.Code_128_B, "DsPdf-2023", null);
            drawBarcode(CodeType.Code_128_C, "1234567890", null);
            drawBarcode(CodeType.Code_128auto, "DsPdf-2023", null);
            drawBarcode(CodeType.Code_2_of_5, "1234567890", null);
            drawBarcode(CodeType.DataMatrix, "MESCIUS+DioDocs", null);
            drawBarcode(CodeType.QRCode, "メシウス DioDocs for PDF", null);
            drawBarcode(CodeType.EAN_8, "1234567", null);
            drawBarcode(CodeType.EAN_13, "469" + "87654" + "3210", null);
            drawBarcode(CodeType.EAN128FNC1, "MESCIUS\nDioDocs", null);
            drawBarcode(CodeType.IntelligentMail, "00300999999000000001", null);
            drawBarcode(CodeType.JapanesePostal, "9813205", null);
            drawBarcode(CodeType.PostNet, "152063949", null);
            drawBarcode(CodeType.RM4SCC, "SE17PB9Z", null);
            drawBarcode(CodeType.Matrix_2_of_5, "1234567890", null);
            drawBarcode(CodeType.MSI, "1234567890", null);
            drawBarcode(CodeType.MicroPDF417, "DioDocs", null);
            drawBarcode(CodeType.Pdf417, "DioDocs", null);
            drawBarcode(CodeType.RSS14, "1234567890", null);
            drawBarcode(CodeType.RSS14Stacked, "1234567890", null);
            drawBarcode(CodeType.RSS14Stacked, "1234567890", "12345");
            drawBarcode(CodeType.RSS14StackedOmnidirectional, "1234567890", null);
            drawBarcode(CodeType.RSS14Truncated, "1234567890", null);
            drawBarcode(CodeType.RSSExpanded, "12345678901234", null);
            drawBarcode(CodeType.RSSExpandedStacked, "12345678901234", null);
            drawBarcode(CodeType.RSSLimited, "1234567890", null);
            drawBarcode(CodeType.RSSLimited, "1234567890", "12345");
            drawBarcode(CodeType.UCCEAN128, "MESCIUS+DioDocs", null);
            drawBarcode(CodeType.UPC_A, "123456789012", null);
            drawBarcode(CodeType.UPC_E0, "123456789012", null);
            drawBarcode(CodeType.UPC_E1, "123456789012", null);
            // PDF を保存します。
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}