-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
80 lines (77 loc) · 3.14 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Drawing.Printing;
using DevExpress.XtraPrinting;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraPrintingLinks;
using DevExpress.XtraEditors;
namespace RichEdit_PrintingSystem
{
public partial class Form1 : XtraForm
{
public Form1()
{
InitializeComponent();
}
#region #printfromserver
private void btn_PrintFromServer_Click(object sender, EventArgs e)
{
RichEditDocumentServer srv = new RichEditDocumentServer();
srv.LoadDocument("Grimm.docx", DocumentFormat.OpenXml);
// Insert a field displaying the current date/time into the document header.
srv.BeginUpdate();
SubDocument _header = srv.Document.Sections[0].BeginUpdateHeader();
_header.Fields.Create(_header.Range.Start,"DATE \\@ \"dddd, MMMM dd, yyyy HH:mm:ss\" \\MERGEFORMAT");
_header.Paragraphs[0].Alignment = ParagraphAlignment.Right;
srv.Document.Sections[0].EndUpdateHeader(_header);
// Specify page margins, orientation, etc.
SetPrintOptions(srv);
srv.EndUpdate();
// Update fields
srv.Document.UpdateAllFields();
// Create a printable link to print a document.
PrintViaLink(srv);
}
#endregion #printfromserver
#region #setprintoptions
private static void SetPrintOptions(IRichEditDocumentServer richedit)
{
foreach (Section _section in richedit.Document.Sections) {
_section.Page.PaperKind = DevExpress.Drawing.Printing.DXPaperKind.A4;
_section.Page.Landscape = false;
_section.Margins.Left = 150f;
_section.Margins.Right = 150f;
_section.Margins.Top = 50f;
_section.Margins.Bottom = 50f;
}
}
#endregion #setprintoptions
#region #printvialink
private static void PrintViaLink(RichEditDocumentServer srv)
{
if (!srv.IsPrintingAvailable) return;
using (PrintingSystemBase ps = new PrintingSystemBase())
using (PrintableComponentLinkBase link = new PrintableComponentLinkBase(ps)) {
link.Component = srv;
// Disable warnings.
ps.ShowMarginsWarning = false;
ps.ShowPrintStatusDialog = false;
// Find a printer containing 'PDF' in its name.
string printerName = String.Empty;
for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++) {
string pName = PrinterSettings.InstalledPrinters[i];
if (pName.Contains("PDF")) {
printerName = pName;
break;
}
}
//Run document creaion
link.CreateDocument();
// Print to the specified printer.
PrintToolBase tool = new PrintToolBase(ps);
tool.Print(printerName);
}
}
#endregion #printvialink
}
}