Question

I've the following code, which takes a string and creates a Tiff file.

string sFileData = "Hello World";
string sFileName = "Bitmap.bmp";

Font oFont = new Font("Arial", 11, FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
var sz = GraphicsHelper.MeasureString(sFileData, oFont);

var oBitmap = new Bitmap((int)sz.Width, (int)sz.Height);

using (Graphics oGraphics = Graphics.FromImage(oBitmap)) {
    oGraphics.Clear(Color.White);
    oGraphics.DrawString(sFileData, oFont, new SolidBrush(System.Drawing.Color.Black), 0, 0);
    oGraphics.Flush();

}

oBitmap.Save(sFileName, System.Drawing.Imaging.ImageFormat.Tiff);

public static class GraphicsHelper {
    public static SizeF MeasureString(string s, Font font) {
        SizeF result;
        using (var image = new Bitmap(1, 1)) {
            using (var g = Graphics.FromImage(image)) {
                result = g.MeasureString(s, font);
            }
        }
     return result;
    }
}

This works fine when the width and height of the string doesn't exceed the size of an A4 page. However the problem I have now, is I need to be able to print this Tiff to a printer.

Therefore I need a way of wrapping any text to the width of a A4 page, and if the height exceeds the height of a A4 page, then the text needs to be carried over to the next page.

Can anyone offer any examples on how I can achieve this ?

Was it helpful?

Solution

You are already measuring the string and know the size of A4, so I assume your problem is that you don't know how to create a TIFF with multiple pages in it.

If so, look at this answer to a similar question:

https://stackoverflow.com/a/7675996/3937

The upshot is to use Bitmap.SaveAdd() to add images to an existing TIFF

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top