using it.stefanochizzolini.clown.documents;
using it.stefanochizzolini.clown.documents.contents.composition;
using it.stefanochizzolini.clown.documents.contents.entities;
using it.stefanochizzolini.clown.documents.contents.fonts;
using it.stefanochizzolini.clown.documents.contents.xObjects;
using it.stefanochizzolini.clown.files;
using System;
using System.Collections.Generic;
using System.Drawing;
namespace it.stefanochizzolini.clown.samples{
/**
<summary>This sample demonstrates how to show bar codes in a PDF file.</summary>
*/
public class BarcodeSample
: ISample
{
#region static
#region fields
private const float Margin = 36;
#endregion
#endregion
#region dynamic
#region interface
#region public
#region ISample
public void Run(
SampleLoader loader
)
{
// 1. PDF file instantiation.
File file = new File();
// 2. Content creation.
Document document = file.Document;
Populate(document,loader);
// (boilerplate metadata insertion -- ignore it)
loader.BuildAccessories(document,this.GetType(),"Barcode","showing barcodes");
// 3. Serialize the PDF file (again, boilerplate code -- see the SampleLoader class source code)!
loader.Serialize(file,this.GetType().Name,false);
}
#endregion
#endregion
#region private
/**
<summary>Populates a PDF file with contents.</summary>
*/
private void Populate(
Document document,
SampleLoader loader
)
{
// Get the abstract barcode entity!
EAN13Barcode barcode = new EAN13Barcode("8012345678901");
// Create the reusable barcode within the document!
XObject barcodeXObject = barcode.ToXObject(document);
Pages pages = document.Pages;
// Page 1.
{
Page page = new Page(document);
pages.Add(page);
Size pageSize = page.Size.Value;
PrimitiveFilter builder = new PrimitiveFilter(page);
{
BlockFilter blockFilter = new BlockFilter(builder);
blockFilter.Hyphenation = true;
blockFilter.Begin(
new RectangleF(
Margin,
Margin,
pageSize.Width - Margin * 2,
pageSize.Height - Margin * 2
),
AlignmentXEnum.Left,
AlignmentYEnum.Top
);
StandardType1Font bodyFont = new StandardType1Font(
document,
StandardType1Font.FamilyNameEnum.Courier,
true,
false
);
builder.SetFont(bodyFont,32);
blockFilter.ShowText("Barcode sample"); blockFilter.ShowBreak();
builder.SetFont(bodyFont,16);
blockFilter.ShowText("Showing the EAN-13 Bar Code on different compositions:"); blockFilter.ShowBreak();
blockFilter.ShowText("- page 1: on the lower right corner of the page, 100pt wide;"); blockFilter.ShowBreak();
blockFilter.ShowText("- page 2: on the middle of the page, 1/3-page wide, 25 degree counterclockwise rotated;"); blockFilter.ShowBreak();
blockFilter.ShowText("- page 3: filled page, 90 degree clockwise rotated."); blockFilter.ShowBreak();
blockFilter.End();
}
// Show the barcode!
builder.ShowXObject(
barcodeXObject,
new PointF(
pageSize.Width - Margin,
pageSize.Height - Margin
),
new Size(100,0),
AlignmentXEnum.Right,
AlignmentYEnum.Bottom,
0
);
builder.Flush();
}
// Page 2.
{
Page page = new Page(document);
pages.Add(page);
Size pageSize = page.Size.Value;
PrimitiveFilter builder = new PrimitiveFilter(page);
// Show the barcode!
builder.ShowXObject(
barcodeXObject,
new PointF(
pageSize.Width / 2,
pageSize.Height / 2
),
new Size((int)pageSize.Width/3,0),
AlignmentXEnum.Center,
AlignmentYEnum.Middle,
25
);
builder.Flush();
}
// Page 3.
{
Page page = new Page(document);
pages.Add(page);
Size pageSize = page.Size.Value;
PrimitiveFilter builder = new PrimitiveFilter(page);
// Show the barcode!
builder.ShowXObject(
barcodeXObject,
new PointF(
pageSize.Width / 2,
pageSize.Height / 2
),
new Size((int)pageSize.Height,(int)pageSize.Width),
AlignmentXEnum.Center,
AlignmentYEnum.Middle,
-90
);
builder.Flush();
}
}
#endregion
#endregion
#endregion
}
}
|