简体   繁体   English

如何从大字符串值创建多页Tiff文件

[英]How can I create a multi page Tiff file from a large string value

I've the following code, which takes a string and creates a Tiff file. 我有以下代码,它接受一个字符串并创建一个Tiff文件。

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. 当字符串的宽度和高度不超过A4页面的大小时,这可以正常工作。 However the problem I have now, is I need to be able to print this Tiff to a printer. 但是我现在遇到的问题是,我需要能够将这个Tiff打印到打印机上。

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. 因此,我需要一种将任何文本包装到A4页面宽度的方法,如果高度超过A4页面的高度,则需要将文本转移到下一页。

Can anyone offer any examples on how I can achieve this ? 任何人都可以提供我如何实现这一目标的任何例子吗?

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. 您已经在测量字符串并知道A4的大小,因此我认为您的问题是您不知道如何创建包含多个页面的TIFF。

If so, look at this answer to a similar question: 如果是这样,请查看类似问题的答案:

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

The upshot is to use Bitmap.SaveAdd() to add images to an existing TIFF 结果是使用Bitmap.SaveAdd()将图像添加到现有TIFF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM