- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何使用 C# 打印 .rtf 文件? (WinForms/WPF 应用程序)
最佳答案
来自 https://support.microsoft.com/en-us/kb/812425 :
In Visual C# .NET or Visual C# 2005, create a new Class Library project that is named RichTextBoxPrintCtrl. By default, Class1.cs is created. Change the name of Class1.cs to RichTextBoxPrintCtrl.cs. In Solution Explorer, right-click References, and then click Add Reference. In the Add Reference dialog box, double-click System.Drawing.dll and System.Windows.Forms.dll, and then click OK.
Replace the existing code in RichTextBoxPrintCtrl.cs with the following code:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Drawing.Printing;
namespace RichTextBoxPrintCtrl
{
public class RichTextBoxPrintCtrl:RichTextBox
{
//Convert the unit used by the .NET framework (1/100 inch)
//and the unit used by Win32 API calls (twips 1/1440 inch)
private const double anInch = 14.4;
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[StructLayout(LayoutKind.Sequential)]
private struct CHARRANGE
{
public int cpMin; //First character of range (0 for start of doc)
public int cpMax; //Last character of range (-1 for end of doc)
}
[StructLayout(LayoutKind.Sequential)]
private struct FORMATRANGE
{
public IntPtr hdc; //Actual DC to draw on
public IntPtr hdcTarget; //Target DC for determining text formatting
public RECT rc; //Region of the DC to draw to (in twips)
public RECT rcPage; //Region of the whole DC (page size) (in twips)
public CHARRANGE chrg; //Range of text to draw (see earlier declaration)
}
private const int WM_USER = 0x0400;
private const int EM_FORMATRANGE = WM_USER + 57;
[DllImport("USER32.dll")]
private static extern IntPtr SendMessage (IntPtr hWnd , int msg , IntPtr wp, IntPtr lp);
// Render the contents of the RichTextBox for printing
// Return the last character printed + 1 (printing start from this point for next page)
public int Print( int charFrom, int charTo,PrintPageEventArgs e)
{
//Calculate the area to render and print
RECT rectToPrint;
rectToPrint.Top = (int)(e.MarginBounds.Top * anInch);
rectToPrint.Bottom = (int)(e.MarginBounds.Bottom * anInch);
rectToPrint.Left = (int)(e.MarginBounds.Left * anInch);
rectToPrint.Right = (int)(e.MarginBounds.Right * anInch);
//Calculate the size of the page
RECT rectPage;
rectPage.Top = (int)(e.PageBounds.Top * anInch);
rectPage.Bottom = (int)(e.PageBounds.Bottom * anInch);
rectPage.Left = (int)(e.PageBounds.Left * anInch);
rectPage.Right = (int)(e.PageBounds.Right * anInch);
IntPtr hdc = e.Graphics.GetHdc();
FORMATRANGE fmtRange;
fmtRange.chrg.cpMax = charTo; //Indicate character from to character to
fmtRange.chrg.cpMin = charFrom;
fmtRange.hdc = hdc; //Use the same DC for measuring and rendering
fmtRange.hdcTarget = hdc; //Point at printer hDC
fmtRange.rc = rectToPrint; //Indicate the area on page to print
fmtRange.rcPage = rectPage; //Indicate size of page
IntPtr res = IntPtr.Zero;
IntPtr wparam = IntPtr.Zero;
wparam = new IntPtr(1);
//Get the pointer to the FORMATRANGE structure in memory
IntPtr lparam= IntPtr.Zero;
lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
Marshal.StructureToPtr(fmtRange, lparam, false);
//Send the rendered data for printing
res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam);
//Free the block of memory allocated
Marshal.FreeCoTaskMem(lparam);
//Release the device context handle obtained by a previous call
e.Graphics.ReleaseHdc(hdc);
//Return last + 1 character printer
return res.ToInt32();
}
}
}
On the Debug menu, click Start to run the application. Form1 is displayed. Type some text in RichTextBoxPrintCtrl. Click Page Setup to set the page settings. Click Print Preview to view the print preview of the page. Click Print to print the content of RichTextBoxPrintCtrl.
关于c# - 如何打印 .rtf 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3838623/
有谁知道将背景颜色应用于表格单元格的 rtf 标签? 我知道表结构: {\rtf1\ansi\deff0 \trowd \clshdng10000\cellx1000 \clshdng10000
MS Exchange/Outlook 消息使用 MAPI 存储数据。一个常见的 MAPI 属性包含消息正文的富文本版本(0x1009、PR_RTF_COMPRESSED、PidTagRtfCompr
我需要以编程方式生成一个 RTF 文档,其中包含一个垂直合并列的表格,例如 ______________________________ | merged | foo | hello |
我正在尝试增加 柜台 使用以下代码在 BIP 报告的 for-each 循环中将变量增加 1。 然而,柜台 无论有多少循环,变量都不会增加。有人可以指导我如何做到这一点。 我也知道 position(
我需要在 RTF 文件中创建一个表。但是我不熟悉 RΤF。以下是这些 RTF 文件应该替换的文本文件示例: GENERAL JOURNAL
我正在尝试手动生成 RTF 文档,最终会以编程方式完成。我计划改进 pyRTF,使其可以生成“目录”,但我认为它不能。 我无法使用 RTF 控制字(\tc、\tcf 和\tcl.)生成目录。 http
如何在 rtf 中的单元格内制作表格?这是我所知道的最简单的表格: {\rtf\ansi\deff0 \trowd \intbl \cellx1000 Item 1\cell \cellx2000 I
我正在研究 RTF 解析器,但在处理 unicode 时遇到了一些困难。 RTF 规范规定“大于 32767 的 Unicode 值必须表示为负数”( http://www.biblioscape.c
我想更改 RTF 文档的内容,而不是将其另存为 RTF 文档: $defaultRtfFile>> "C:\Users\user\Desktop\Outlokk-Signature\Test.rtf"
我需要一个简单的 HTML2RTF 转换器我尝试运行以下代码...但我收到此代码示例的错误 代码: import java.io.ByteArrayInputStream; import java.i
我想编写一个 python 脚本,将文件编码从 cp949 转换为 utf8。该文件最初以 cp949 编码。我的脚本如下: cpstr = open('terms.rtf').read() utf
有哪些方法可以使用 Java 将 RTF 转换为包含 Windows 或 Unix 文档中表格的 PDF? 我们在这里尝试过的选项是: ITEXT - 但 rtf 文档中的表格在转换为 PDF 后无法
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
似乎不可能为 RTF 项目符号列表找到一个非常简单的工作示例,如下所示: 您好 世界 嵌套(但请以正方形作为项目符号) 也嵌套(也是方形项目符号) 再次定期 我尝试使用 Word 或 TextEdit
我有一个数据框 df 用于 RTF 格式的报告,如下所示: df <- ATRSLBL POPUL CENTRE BAGE BAGEC1 SEX Red PPS 3720
我正在处理由其他人在未知平台上制作的 RTF 文件,并且所有内容都被正确解释,除了一些字符,无论我在 openoffice 中打开它们的任何字符集。这是经过解释的纯文本: "Même taille q
有人知道RTF文件的linter吗? 我正在为文字处理器编写一个简单的 RTF 导出工具。我已经到了我生成的文件现在加载到 OpenOffice 并且似乎可以工作的阶段——除了我刚刚发现如果我从 Op
有人可以帮我将特殊字符转换为可以在 RTF 文件中正确表示的字符吗? 我正在获取 iPad 上存储在字符串中的文本,并使用 NSASCIIStringEncoding 将其输出为 RTF 文件。到目前
我对 RichTextBox.Rtf 属性有点困惑。我正在输入一个混合字体大小的字符串:Test 字体大小为 20,string 字体大小为 10。但是,当我查看 Rtf 时 字符串,我得到以下内容:
我正试图让一个欧元符号显示在 RTF 文档中,但我不太走运。 RTF 文档是一个模板,其中填充了来自 .Net 的数据(通过读取文本流并替换其中的标记)。这些标记之一与欧元符号有关。我的代码按预期工作
我是一名优秀的程序员,十分优秀!