如何使用C#打印.rtf文件? (的WinForms/WPF應用程序)如何打印.rtf文件?
回答
從https://support.microsoft.com/en-us/kb/812425:
在Visual C#.NET或Visual C#2005,創建一個名爲RichTextBoxPrintCtrl的類庫項目。默認情況下,創建Class1.cs。 將Class1.cs的名稱更改爲RichTextBoxPrintCtrl.cs。 在解決方案資源管理器中,右鍵單擊引用,然後單擊添加引用。 在添加引用對話框中,雙擊System.Drawing.dll和System.Windows.Forms.dll,然後單擊確定。
用下面的代碼替換現有的代碼在RichTextBoxPrintCtrl.cs:
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();
}
}
}
在調試菜單上,單擊開始運行該應用程序。顯示Form1。 在RichTextBoxPrintCtrl中鍵入一些文本。 單擊頁面設置以設置頁面設置。 單擊打印預覽可查看頁面的打印預覽。 單擊「打印」以打印RichTextBoxPrintCtrl的內容。
直接鏈接:http://support.microsoft.com/default.aspx?scid=kb;en-us;812425 – amalgamate 2014-02-07 18:14:35
Printing Word(RTF) Documents in C#
或者,你可以在一個RichTextBox控件打開文件,並從那裏send to printing。
- 1. MFC - 打印rtf文件
- 2. 如何從richtextbox打印rtf
- 3. 服務器打印RTF文件
- 4. 打開.rtf文件
- 5. 如何打印pdf.js文件?
- 6. 如何打印JSON文件?
- 7. 如何打印文件C#
- 8. 從PRINT按鈕打印.rtf或.txt文件?
- 9. 在用Python製作的RTF文件中打印新行
- 10. 如何在Windows Store應用程序中打印RichEditBox內容(RTF文件)
- 11. 如何從.RTF/.doc文件
- 12. 如何打開.rtf文件作爲文本流
- 13. 如何在Qt中打印文本文件到打印機?
- 14. 發送rtf到打印機vb.net
- 15. 在Java中渲染和打印RTF
- 16. 使用格式化打印RTF
- 17. 如何知道打印機的打印方法何時完成打印文件?
- 18. WPF中如何打印文本文件
- 19. 如何將RTF格式的字符串打印到JLabel?
- 20. 打開RTF文件並查看源
- 21. 打印文件
- 22. 打印文件
- 23. 如何將文件打印到特定的打印機
- 24. 如何在PDF文件上打印打印機型號
- 25. 如何訪問打印稿文件中的實例可打印?
- 26. 如何從打印了評論的文件打印值?
- 27. 如何從無線打印機打印PDF文件?
- 28. 我如何打印圖像文件在打印機
- 29. 如何判斷打印機是否打印到文件?
- 30. 如何打印PDF文件在Java中與打印對話框
請您詳細說明一下嗎? – 2010-10-01 11:13:50