Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MigraDoc support for rendering barcodes and PDFSharp support for Code 128 barcodes #93

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// PDFsharp - A .NET library for processing PDF
// See the LICENSE file in the solution root for more information.

using System;
using PdfSharp.Drawing;

namespace MigraDoc.Rendering
{
/// <summary>
/// Formatting information for an barcode.
/// </summary>
internal class BarcodeFormatInfo : ShapeFormatInfo
{
internal BarcodeFormatInfo()
{
}

internal XUnit Width;
internal XUnit Height;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// PDFsharp - A .NET library for processing PDF
// See the LICENSE file in the solution root for more information.

using System;

namespace MigraDoc.Rendering
{
/// <summary>
/// Represents rendering information for barcodes.
/// </summary>
internal class BarcodeRenderInfo : ShapeRenderInfo
{
public BarcodeRenderInfo()
{
}

public override FormatInfo FormatInfo
{
get => _formatInfo;
internal set => _formatInfo = (BarcodeFormatInfo)value;
}
BarcodeFormatInfo _formatInfo = new();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// PDFsharp - A .NET library for processing PDF
// See the LICENSE file in the solution root for more information.

using System;
using System.IO;
using System.Diagnostics;
using MigraDoc.DocumentObjectModel;
using PdfSharp.Drawing;
using PdfSharp.Drawing.BarCodes;
using MigraDoc.DocumentObjectModel.Shapes;
using MigraDoc.Rendering.Resources;

namespace MigraDoc.Rendering
{
/// <summary>
/// Renders barcodes.
/// </summary>
// Adapted from https://forum.pdfsharp.net/viewtopic.php?p=3332#p3332
internal class BarcodeRenderer : ShapeRenderer
{
internal BarcodeRenderer(XGraphics gfx, Barcode barcode, FieldInfos fieldInfos)
: base(gfx, barcode, fieldInfos)
{
this._barcode = barcode;
BarcodeRenderInfo renderInfo = new BarcodeRenderInfo();
renderInfo.DocumentObject = this._shape;
this._renderInfo = renderInfo;
}

internal BarcodeRenderer(XGraphics gfx, RenderInfo renderInfo, FieldInfos fieldInfos)
: base(gfx, renderInfo, fieldInfos)
{
this._barcode = (Barcode)renderInfo.DocumentObject;
}

internal override void Format(Area area, FormatInfo previousFormatInfo)
{
BarcodeFormatInfo formatInfo = (BarcodeFormatInfo)this._renderInfo.FormatInfo;

formatInfo.Height = this._barcode.Height.Point;
formatInfo.Width = this._barcode.Width.Point;

base.Format(area, previousFormatInfo);
}

protected override XUnit ShapeHeight
{
get
{
BarcodeFormatInfo formatInfo = (BarcodeFormatInfo)this._renderInfo.FormatInfo;
return formatInfo.Height + this._lineFormatRenderer.GetWidth();
}
}

protected override XUnit ShapeWidth
{
get
{
BarcodeFormatInfo formatInfo = (BarcodeFormatInfo)this._renderInfo.FormatInfo;
return formatInfo.Width + this._lineFormatRenderer.GetWidth();
}
}

internal override void Render()
{
RenderFilling();

BarcodeFormatInfo formatInfo = (BarcodeFormatInfo)this._renderInfo.FormatInfo;
Area contentArea = this._renderInfo.LayoutInfo.ContentArea;
XRect destRect = new XRect(contentArea.X, contentArea.Y, formatInfo.Width, formatInfo.Height);

BarCode gfxBarcode = null;

if (this._barcode.Type == BarcodeType.Barcode39)
gfxBarcode = new Code3of9Standard();
else if (this._barcode.Type == BarcodeType.Barcode25i)
gfxBarcode = new Code2of5Interleaved();
else if (this._barcode.Type == BarcodeType.Barcode128)
gfxBarcode = new Code128();

// if gfxBarcode is null, the barcode type is not supported
if (gfxBarcode != null)
{
gfxBarcode.Text = this._barcode.Code;
gfxBarcode.Direction = CodeDirection.LeftToRight;
gfxBarcode.Size = new XSize(ShapeWidth, ShapeHeight);

this._gfx.DrawBarCode(gfxBarcode, XBrushes.Black, destRect.Location);
}

RenderLine();
}

Barcode _barcode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ internal FieldInfos FieldInfos
renderer = new ChartRenderer(gfx, chart, fieldInfos);
else if (documentObject is Image image)
renderer = new ImageRenderer(gfx, image, fieldInfos);
else if (documentObject is Barcode)
renderer = new BarcodeRenderer(gfx, (Barcode)documentObject, fieldInfos);

if (renderer != null)
renderer._documentRenderer = documentRenderer;
Expand Down Expand Up @@ -172,6 +174,8 @@ internal static Renderer Create(XGraphics gfx, DocumentRenderer documentRenderer
// renderer = new ChartRenderer(gfx, renderInfo, fieldInfos);
else if (renderInfo.DocumentObject is Image)
renderer = new ImageRenderer(gfx, renderInfo, fieldInfos);
else if (renderInfo.DocumentObject is Barcode)
renderer = new BarcodeRenderer(gfx, renderInfo, fieldInfos);

if (renderer != null)
renderer._documentRenderer = documentRenderer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@ internal static string DataMatrixInvalid(int columns, int rows)
{
return $"'{rows}'x'{columns}' is an invalid ecc200 DataMatrix size.";
}

internal static string InvalidCode128(int index)
{
return $"Invalid character for Code 128 at index {index}.";
}
}
}
Loading