I have an application in C# that would print invoices and payslips. The client have sent me a template which would be used for the day to day operations. I don't know how to print to it, though I already know how to print a programmatically made text file which contains the information from an access database.
How do I print the information on this kind of template? (This is only something I [found on Google][1] and is a good candidate for a simple invoice printing) The document template I have also have a LOGO..
Do it by mail merge in Word. Using this technique you create Word document. Inside document you create placeholders for text. And from code you fill placeholders with whatever you want.
For example:
In word document type ctrl + F9
Right click on field and Edit field
Choose MergeField
On field name type FirstName
Add code:
.
var document = new Document("document.docx");
var sqlCommand = "SELECT TOP 1 userName FirstName FROM Users";
var table = GetTable(sqlCommand, String.Empty);
document.MailMerge.Execute(table);
I've been using the PrintDocument and PrintPreview objects. That use the the Graphics class. When print is called you get an "PrintEventArgs e" object. Then you can use e.Graphics to have access to things like e.Graphics.DrawString, .DrawImage etc.
I built a whole print objects class that overrides print. So I have a detail box that has different fonts, or a logo, a header, leagal jargon etc. Each one of these has it's own class. I put them all in a big list and I call printThis(List); and it will take each print function and the coordinates and make me a form.
Inherited object
class formHdr : printObject
{
private string headerText;
public formHdr(string hText)
: base()
{
headerText = hText;
}
public override void printThis(System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString(headerText, FRHEADER, Brushes.Black, BaseX, BaseY);
}
}
Base class
abstract class printObject
{
protected Font FTHEADER;
protected Font NRML;
protected Font DETAIL;
protected Font FRHEADER;
protected Font DETHEADER;
protected Font LEGAL;
protected Font LEGAL2;
public int baseX, baseY;
public int boxSX, boxSY;
public printObject()
{
baseX = 0;
baseY = 0;
boxSX = 0;
boxSY = 0;
FTHEADER = new Font("Arial", 12, FontStyle.Bold);
NRML = new Font("Calibri", 10);
DETAIL = new Font("Consolas", 8);
FRHEADER = new Font("Arial", 16, FontStyle.Bold);
DETHEADER = new Font("Calibri", 10, FontStyle.Bold);
LEGAL = new Font("Arial", 8);
LEGAL2 = new Font("Arial", 10);
}
public virtual void printThis(PrintPageEventArgs e) { }
Object creation
mainHead = new formHdr("Bill of Lading/Weigh slip Original");
mainHead.BaseX = 225;
mainHead.BaseY = 20;
bol.Add(mainHead);
Maybe this can get you started? I'm still tweaking it and will be interested in other responses.
Related
I solved the problem, I created a function where I just pass the value of the spacing I need and the TextField
public static void ChangeSpaceBetweenCharacters(this UITextField textField, float space)
{
textField.WeakDefaultTextAttributes.SetValueForKey(NSObject.FromObject((nfloat)space), UIStringAttributeKey.KerningAdjustment);
}
Best regards,
Rafael Santos
I think the problem may be caused by the font method you use, I can't make it work with:
UIFont("ArialMT", 13)
Instead, you can use :
Font = UIFont.FromName("ArialMT", 13),
Or
Font = UIFont.SystemFontOfSize(10),
I test below sample code and it works for me:
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
NSMutableAttributedString mtText = new NSMutableAttributedString("testTextfield", new UIStringAttributes
{
Font = UIFont.SystemFontOfSize(10),
KerningAdjustment = 8.0f
});
UITextField textField = new UITextField();
textField.Frame = new CoreGraphics.CGRect(10,20,350,50);
textField.AttributedText = mtText;
Add(textField);
}
I have alot of methods who use same line of code:
title.Font = new Font("Arial", 12, FontStyle.Bold);
I want to optimize it and just call another function instead create a new font foreach method so I try something like this:
void titleFont()
{
var font = new Font("Arial", 12, FontStyle.Bold);
return ;
}
and then call as:
title.Font = titleFont();
But I get
The name 'titleFont' does not exist in the current context
What am I doing wrong? Regards
The method is likely not accessible due to scope. By making it public, it is available to all callers. Also, your method needs to return your font or the font variable doesn't get set to anything. The code below replaces void with Font so that the method itself will return the value of the internal font variable upon returning.
public Font titleFont()
{
var font = new Font("Arial", 12, FontStyle.Bold);
return font;
}
your return type is void, change that to font
public Font titleFont()
{
Font fnt = new Font("Arial", 12, FontStyle.Bold);
return fnt ;
}
and yes the error is because you are not using the reference properly suppose you have the method in a class f suppose and is non static method then you have to Create object of f class and call that method.
I have created a TextBox on the code
RichTextBox tb3 = new RichTextBox();
Then I wrote an other code (in the same void) to change it's font:
tb3.Font.Size(FontSize.Text);
But the following error appeares : "non-invocable member 'Font Size' cannot be used like a method.
Note: "FontSize" is an ID for a textbox.
How can I do it without creating a new Font?
Please help!
tb3.Font.Size isn't a method, it is
public float Size { get; }
and you can't change it so.
if you want to change font see it -> link1
and it link2
you should create new font, for example:
tb3.Font = new Font(tb3.Font.FontFamily, 23, FontStyle.Regular);
Easy example, lets say I'm creating a Label like that:
Label label = new Label();
label.Text = "Hello" + "20.50";
label.Width = 250;
label.Height = 100;
panel1.Controls.Add(label);
How could I say that the "20.50" should appear in the lowest right edge of the label?
For clarity I made a little example in word:
How could I achieve this? Any help appreciated!
There's no built-in support for this with a Label control. You'll need to inherit from Label to create a custom control, and then write the painting code yourself.
Of course, you'll also need some way to differentiate between the two strings. The + sign, when applied to two strings, is concatenation. The two strings are joined together by the compiler, so all you get is this: Hello20.50. You will either need to use two separate properties, each with their own strings, or insert some sort of delimiter in between the two strings that you can use to split them apart later. Since you're already creating a custom control class, I'd go with the separate properties—much cleaner code, and harder to get wrong.
public class CornerLabel : Label
{
public string Text2 { get; set; }
public CornerLabel()
{
// This label doesn't support autosizing because the default autosize logic
// only knows about the primary caption, not the secondary one.
//
// You will either have to set its size manually, or override the
// GetPreferredSize function and write your own logic. That would not be
// hard to do: use TextRenderer.MeasureText to determine the space
// required for both of your strings.
this.AutoSize = false;
}
protected override void OnPaint(PaintEventArgs e)
{
// Call the base class to paint the regular caption in the top-left.
base.OnPaint(e);
// Paint the secondary caption in the bottom-right.
TextRenderer.DrawText(e.Graphics,
this.Text2,
this.Font,
this.ClientRectangle,
this.ForeColor,
TextFormatFlags.Bottom | TextFormatFlags.Right);
}
}
Add this class to a new file, build your project, and then drop this control onto your form. Make sure to set both the Text and Text2 properties, and then resize the control in the designer and watch what happens!
Here is what you need, a custom label:
public class CustomLabel : Label
{
public CustomLabel()
{
TopLeftText = BottomRightText = "";
AutoSize = false;
}
public string TopLeftText {get;set;}
public string BottomRightText {get;set;}
protected override void OnPaint(PaintEventArgs e)
{
using (StringFormat sf = new StringFormat() { LineAlignment = StringAlignment.Near})
{
using(SolidBrush brush = new SolidBrush(ForeColor)){
e.Graphics.DrawString(TopLeftText, Font, brush, ClientRectangle, sf);
sf.LineAlignment = StringAlignment.Far;
sf.Alignment = StringAlignment.Far;
e.Graphics.DrawString(BottomRightText, Font, brush, ClientRectangle, sf);
}
}
}
}
//use it:
//first, set its size to what you want.
customLabel1.TopLeftText = house.Name;
customLabel2.BottomRightText = house.Number;
A form with a label and a button 'Options'. By clicking the button a new form opens with 2 radio buttons 'Font1' and 'Font2', and two buttons 'Apply' and 'Cancel'. Upon selecting one of the radio buttons and clicking 'Apply' will make the label on the first form change the font face. The problem is how to change the font as in from say Tahoma to Arial or to any other font face of the label.
Options form code for apply button, which if was clicked will return dialogresult.ok == true and change the font of the label on the first form:
private void btnApply_Click(object sender, EventArgs e)
{
if (radioFont1.Checked)
{
mainForm.lblName.Font.Name = "Arial"; 'wrong attempt
}
this.DialogResult = DialogResult.OK;
}
Declaration of the label on first form so that it is visible to second form:
public static Label lblName = new Label();
...
private void mainForm_Load(object sender, EventArgs e)
{
lblName = lblBarName;
}
Font.Name, Font.XYZProperty, etc are readonly as Font is an immutable object, so you need to specify a new Font object to replace it:
mainForm.lblName.Font = new Font("Arial", mainForm.lblName.Font.Size);
Check the constructor of the Font class for further options.
You can't change a Font once it's created - so you need to create a new one:
mainForm.lblName.Font = new Font("Arial", mainForm.lblName.Font.Size);
You need to create a new Font
mainForm.lblName.Font = new Font("Arial", mainForm.lblName.Font.Size);
I noticed there was not an actual full code answer, so as i come across this, i have created a function, that does change the font, which can be easily modified. I have tested this in
- XP SP3 and Win 10 Pro 64
private void SetFont(Form f, string name, int size, FontStyle style)
{
Font replacementFont = new Font(name, size, style);
f.Font = replacementFont;
}
Hint: replace Form to either Label, RichTextBox, TextBox, or any other relative control that uses fonts to change the font on them. By using the above function thus making it completely dynamic.
/// To call the function do this.
/// e.g in the form load event etc.
public Form1()
{
InitializeComponent();
SetFont(this, "Arial", 8, FontStyle.Bold);
// This sets the whole form and
// everything below it.
// Shaun Cassidy.
}
You can also, if you want a full libary so you dont have to code all the back end bits, you can download my dll from Github.
Github DLL
/// and then import the namespace
using Droitech.TextFont;
/// Then call it using:
TextFontClass fClass = new TextFontClass();
fClass.SetFont(this, "Arial", 8, FontStyle.Bold);
Simple.
this.lblMessage.Font = new Font("arial", this.lblName.Font.Size);