0

我需要通过代码动态创建一个文档,然后打印并将其保存到 .doc 文件中。到目前为止,我已经设法使用图形类来打印文档,但我不知道如何让它以 .doc 或任何文本格式保存文件。是否有可能做到这一点?如果是的话怎么做?

4

2 回答 2

0

我不确定这是您要查找的内容,但如果您想将使用 Graphics 生成的内容保存在磁盘上,您可以使用 Windows 元文件 (wmf)。如果g是你的实例Graphics,是这样的:

        IntPtr hdc = g.GetHdc();
        Rectangle rect = new Rectangle(0, 0, 200, 200);
        Metafile curMetafile = new Metafile(@"c:\tmp\newFile.wmf", hdc);
        Graphics mfG = Graphics.FromImage(curMetafile);
        mfG.DrawString("foo", new Font("Arial", 10), Brushes.Black, new PointF(10, 10));
        g.ReleaseHdc(hdc);
        mfG.Dispose();
于 2013-02-13T08:04:44.707 回答
0

假设您并不是真的要将图形保存为文本,而只是想创建一个 Word 文档,那么您需要查看Microsoft.Office.Interop.Word.

即来自DotNetPearls

using System;
using Microsoft.Office.Interop.Word;

class Program
{
    static void Main()
    {
    // Open a doc file.
    Application application = new Application();
    Document document = application.Documents.Open("C:\\word.doc");

    // Loop through all words in the document.
    int count = document.Words.Count;
    for (int i = 1; i <= count; i++)
    {
        // Write the word.
        string text = document.Words[i].Text;
        Console.WriteLine("Word {0} = {1}", i, text);
    }
    // Close word.
    application.Quit();
    }
}
于 2013-02-13T08:06:52.143 回答