2

I'm quite new to c# and need some help with formating a document for printing.

I already managed to talk to the printer via this code:

private void Print(string printer)
    {
        PrintDocument PrintDoc = new PrintDocument();

        PrintDoc.PrinterSettings.PrinterName = printer;

        PrintDoc.PrintPage += new PrintPageEventHandler(PrintPage);
        PrintDoc.Print();
    }

    void PrintPage(object sender, PrintPageEventArgs e)
    {
        e.Graphics.DrawLine(new Pen(Color.Black), new Point(0, 0), new Point(100, 100));
        e.Graphics.DrawString("Hello World", new Font("Times New Roman", 12), new SolidBrush(Color.Black), new Point(45, 45));

    }

Which prints me my "hello world" string. Obviously the PrintPage method is code I found on the net. Sadly I couldn't find a way to

a) set the format the size of the paper I print on (it is 138mm x 99mm landscape format)

b) tell the printer where to print my texts exactly.

The paper is a preprinted form and I have to write my text in the specific fields.

So I'm looking for a way to give my printer a formated document like:

<field1>
    <x> 2cm </x>
    <y> 1cm </y>
    <text> textfield1 </text>
</field1>
<field2>
     ....

I couldn't find information on how to do that. So if anyone could tell me how to do this or has a link to a good tutorial, I'd be very thankfull

1 Answer 1

4

To set the size of the paper

printDocument.DefaultPageSettings.PaperSize = new PaperSize("Custom Name", width, height);
printDocument.DefaultPageSettings.Landscape = true;

width and height is in hundredths of an inch

See the tutorial in this SO question for printing text on pre-printed paper.

Also to avoid paper wastage during your experimentation, scan the preprinted paper as image and set it as background in PrintPageHanlder during preview.

void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
    if (printDocument.PrintController.IsPreview)
    {
        Image image = Image.FromFile("ScannedImagePath");
        e.Graphics.DrawImage(image,0,0)
    }
    // print other text here after drawing the background
}           
Sign up to request clarification or add additional context in comments.

2 Comments

I'd love to upvote your post, sadly I just registered and so don't have enough reputation to upvote your comment. I'm missing 4 points it seems ^^;
I finally have enough poits for upvoting. So better late than never I guess :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.