wheelibin / SharpPDFLabel

Create sheets of labels in PDF format from any .NET/ASP.NET application
24 stars 21 forks source link

Font Family Does Not Respond To Changes #1

Closed rwcatalano closed 12 years ago

rwcatalano commented 12 years ago

First off - thank you for posting a great code base.

I am having a bit of a problem. The font face does not change when I change the value from Verdana to anything else.

To ensure I am typing the font names correctly, I am pulling them directly from the InstalledFontCollection.

System.Drawing.Text.InstalledFontCollection C = new System.Drawing.Text.InstalledFontCollection(); FontFamily[] ff = C.Families;

I have also stepped through the code to ensure something wasn't hardcoded, but have yet to be able to generate a pdf displaying a different font.

Additionally, I am saving the stream to a pdf vai a FileStream.write command.

wheelibin commented 12 years ago

Hi, I'm glad the library may be of some use!

Thanks for finding this issue, it seems that by default iTextSharp only supports a very limited set of fonts.

However there is a command to register all the fonts on the current system which are then made available to the library.

I will add this fix properly when I get a little more time but for now you can insert the following line in the LabelCreator constructor:

FontFactory.RegisterDirectories();

So the constructor would look like this:

public LabelCreator(Label label)
{
    FontFactory.RegisterDirectories();
    _label = label;
    _images = new List<byte[]>();
    _textChunks = new List<TextChunk>();
    IncludeLabelBorders = false;
}

I have tested this and it allows different fonts to be specified (any font you have installed on your system).

e.g. Wingdings (very useful I know!)

var labels = new SharpPDFLabel.LabelCreator(new SharpPDFLabel.Labels.A4Labels.Avery.L7160());
labels.AddText("hello", "Wingdings", 12, true);

var output = labels.CreatePDF();

var fileStream = System.IO.File.Create("d:\\test.pdf");
output.CopyTo(fileStream);
fileStream.Close();

Remember that you might have to embed the font if there is a chance it won't exist on the the reader's computer.

Hope that helps

Cheers

p.s. feel free to fork the repository, make this change and then send a pull request!

rwcatalano commented 12 years ago

Killer! I'll give it a try tonight!

I'm printing up barcodes for a custom POS that I'm working on... I tried to do a work around where I wrote the barcode to a png/jpg and add it as an image - but there was too much loss when when saving to a .pdf so it doesn't scan correctly.

Thanks for the prompt response!

rwcatalano commented 12 years ago

Works great now - I can use any font on my system!

Thanks!