How to store Font information in a database - c#

I am working on a Windows Application implemented in C#. One of the requirements is for the user to be able to select a set of fonts and save them into a database. Among the information that I need to store for the font are:
The name of the font
Size
Whether its bold, italic, underline
So far I have this information stored as separate fields in the database. Latter I build the Font object from this information. However, i find it impossible to create a new object with all the needed settings (e.g., a font that is both bold and italic).
To illustrate I do something like this (note that I work with EF, so the methods are just for illustration):
string font_name = GetFontNameFromDatabase();
string font_size = GetFontSizeFromDatabase();
...
Font f = new Font(font_name, font_size);
My question is whether there is a convenient way to store the font object in SLQ Server Database. Maybe as a binary field?

You can use the FontConverter class to serialize and deserialize your fonts:
var fc = new FontConverter();
Font f1 = new Font("Times New Roman", 12);
var fontAsString = fc.ConvertToInvariantString(f1); // "Times New Roman, 12pt"
Font f2 = (Font)fc.ConvertFromInvariantString(fontAsString);
Console.WriteLine(f2.ToString()); // [Font: Name=Times New Roman, Size=12, ...]

Related

PDF Sharp setting font styles

I have a font dialog to let user pick a font name, font size and also possibly a font style.
but this code doesn't work
XFont myFont = new XFont(txtP.Font.Name, txtP.Font.Size, txtP.Font.Style);
Error is: Error 2 Argument 3: cannot convert from
'System.Drawing.FontStyle' to 'PdfSharp.Drawing.XFontStyle' /
Error 1 The best overloaded method match for
'PdfSharp.Drawing.XFont.XFont(string, double,
PdfSharp.Drawing.XFontStyle)' has some invalid arguments
is there a workaround on this? I would really like to let users choose Font Style also not just name and size.
XFontStyle is an enum defined by PdfSharp. Like the error says, there is no way to convert System.Drawing.FontStyle' to 'PdfSharp.Drawing.XFontStyle'.
Options:
Let the user select directly from xFontStyle values.
Create a converter method between FontStyle and XFontStyle.
The second method is a little complicated because you have to map the values in the first enum to the second one, and as you can see they are not exactly the same, but it can be done.
I solved the issue by
XFont myFont = new XFont(txtP.Font.Name, txtP.Font.Size, (PdfSharp.Drawing.XFontStyle)fontStyle);

PrintableAreaHeight & PrintableAreaWidth

How to adjust System.Windows.Controls.PrintDialog.PrintableAreaHeight & System.Windows.Controls.PrintDialog.PrintableAreaWidth since they are read only?
PrintableAreaHeight and PrintableAreaWidth is calculated based on the PrintDialog.PrintTicket that is used. In other words they can't be adjusted because the printer that is printing the document specifies what values to use. If you really want to change the print area, which can cause the printer to print ink on it rollers if the size is larger than what can actually be printed, you could do:
var pd = new PrintDialog();
if(pd.ShowDialog() == true)
{
pd.PrintTicket.PageMediaSize = new PageMediaSize(newWidth, newHeight);
pd.PrintDocument(...);
}
In WPF you can use the PrintTicket Class of System.Windows.Controls.PrintDialog.
This class has a lot of properties to change the aspect of the page.
In Windows Forms you can use YourPrintDialog.PrinterSettings.DefaultPageSettings.PaperSize.
Here is a link to MSDN: PrinterSettings.PaperSizes Property

How to set CurrentCulture for PDF document ITextsharp c#

Document doc = new Document(iTextSharp.text.PageSize.LETTER.Rotate(), 10, 10, 5, 5);
string nazivPDFa = txt_datumFiskalnogIsecka.Text +" "+ txt_nazivKompanije.Text;
PdfWriter pdf = PdfWriter.GetInstance(doc, new FileStream(nazivPDFa + ".pdf", FileMode.CreateNew));
doc.Open();
Paragraph klijent = new Paragraph(ispisiKlijenta.Text);
PdfPTable tabelaNK = new PdfPTable(1);
PdfPCell kl = new PdfPCell(new Phrase(klijent));
kl.BorderColor = BaseColor.BLACK;
tabelaNK.AddCell(kl);
doc.Add(tabelaNK);
I have create PDF document with itextSharp and when I fill PDF with some text who is in Serbian, he doesnt show me chars like š,ć,č,đ,ž.
Example: I wrote "nešto" and I get "neto".
I have a lot of thinks at that PDF and it will take forever to give to all elements current culture.
You aren't using a font when you create your Paragraph. In that case, the Standard Type 1 font Helvetica will be used and it won't be embedded. As Helvetica only supports a limited set of characters, your glyphs won't appear. This is very well documented in the official documentation. It's a pity you try to run before you've learned how to walk.
Several things can be at play.
First, you need to make sure that the encoding of ispisiKlijenta.Text is correct. For instance, is that string in CP1250 or in Unicode? When you write KlijenT.Add("Tekući račun: " +txt_brRacunaKompanije.Text);, you are writing bad code (at least if you were writing Java) because you introduce special characters in your code that may disappear when the code is compiled or executed using a different environment using a different encoding.
Then, you need to provide a font program that knows how to draw the glyphs you need. For instance: Helvetica doesn't know about CP1250, but arial.ttf does (and so do many other fonts, but you need to check first).
Then, you need to decide how you'll use that font. Will you use embed the font as a simple font, as is done in the EncodingExample where we create this PDF, or will you embed the font as a composite font, as is done in the UnicodeExample where we create this PDF. Both PDFs may look identical to you, but they aren't. The choice you make will have an impact on the design of your application.
Once you've made a decision about the font and once you've create a Font object, e.g. named font, you need to use that object when creating a Paragraph.

C# iTextSharp multi fonts in a single cell

First off I'm not that great with C# and it's been a while since I've worked with it..
I'm making a windows form for a friend that delivers packages. So I want to transfer his current paper form, into a .pdf with the library iTextSharp. He still needs to print the form to get the customer signature and so on.
What I need:
I want the table to have a little headline, "Company name" for example, the text should be a little smaller than the text input from the windows form(richTextBox1)
Currently I'm using cells and was wondering if I can use 2 different font sizes within the same cell?
What I have:
table.AddCell("Static headline" + Chunk.NEWLINE + richTextBox1.Text);
What I "want":
var normalFont = FontFactory.GetFont(FontFactory.HELVETICA, 9);
var boldFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12);
table.AddCell("Static headline", boldFont + Chunk.NEWLINE + richTextBox1.Text, normalFont);
You're passing a String and a Font to the AddCell() method. That's not going to work. You need the AddCell() method that takes a Phrase object or a PdfPCell object as parameter.
A Phrase is an object that consists of different Chunks, and the different Chunks can have different font sizes. Please read chapter 2 of my book for more info about this object.
Phrase phrase = new Phrase();
phrase.Add(
new Chunk("Some BOLD text", new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD))
);
phrase.Add(new Chunk(", some normal text", new Font()));
table.AddCell(phrase);
A PdfPCell is an object to which you can add different objects, such as Phrases, Paragraphs, Images,...
PdfPCell cell = new PdfPCell();
cell.AddElement(new Paragraph("Hello"));
cell.AddElement(list);
cell.AddElement(image);
In this snippet list is of type List and image is of type Image.
The first snippet uses text mode; the second snippet uses composite mode. Cells behave very differently depending on the mode you use.
This is all explained in the documentation; you can find hundreds of C# examples here.

How do I get a list of duplicate fonts and their type (Type 1, TrueType, etc) in c#?

I'm trying to find duplicate fonts on a Windows 2008 machine. The tricky part is when you look in C:\Windows\Fonts that duplicate fonts may show up with different names. It is not until you double click on them and see the properties that the Typeface name is the same. We're having problems with fonts conflicting because we have a TrueType and a Type 1 installed at the same time.
I've tried the following:
InstalledFontCollection collection = new InstalledFontCollection();
foreach (var family in collection.Families)
{
Console.WriteLine(family.Name);
}
But, that only gives the font family names and does not show individually installed font files. I couldn't find a way to get the font type from the InstalledFontCollection or a list of the fonts in the FontFamily.

Categories