DevExpress ASPxHtmlEditor controls format and export Word (fixed loss of Chinese font export ),
Insert an ASPxHtmlEditor control named aspxhtmleditor1. My Dev version is 14.1
Format Text
Insert the following code in the background:
1 const string css = "style = 'text-align: justify;" // align the two ends
2 + "text-justify: inter-ideograph ;"
3 + "text-indent: 2em;" // indent the first line with 2 Characters
4 + "line-height: 1.25;" // 1.25 times the line spacing
5 + "margin-top: 0; margin-bottom: 0;" // The first and last 0 rows of the segment.
6 + "font-size: 12pt;" // font: Small four
7 + "font-family: Times New Roman,; '"; // Chinese font:, Spanish font: Times New Roman
8
9
10 ASPxHtmlEditor1.Html = "<p" + css + ">"; // The section is marked with the p tag.
11 for (int I = 0; I <30; I ++)
12 ASPxHtmlEditor1.Html + = "test text 123 abCD"; // This is the content
13 ASPxHtmlEditor1.Html + = "</p>
The above code is used to format the text, which can basically meet the format requirements for generating general reports.
Word export and page settings
For the ASPxHtmlEditor control, you cannot directly set the page (margin, paper size, and so on) for exporting Word or other format files. You need to use RichEditDocumentServer to perform the conversion.
Add the following reference:
1 using DevExpress. XtraRichEdit;
2 using DevExpress. XtraRichEdit. API. Native;
Insert the following two functions
1 private void SetPrintOptions (IRichEditDocumentServer richedit) // set the format
2 {
3 foreach (Section _ section in richedit. Document. Sections)
4 {
5 _ section. Page. PaperKind = System. Drawing. Printing. PaperKind. A4; // A4 paper
6 _ section. Page. Landscape = false; // vertical layout
7 _ section. Margins. Left = 295f; // The Left margin is 2.5
8 _ section. Margins. Right = 295f;
9 _ section. Margins. Top = 295f;
10 _ section. Margins. Bottom = 295f;
11
12}
13}
14
15 protected void PushToBrowser (string fileName) // export the file
16 {
17
18 FileStream fs = new FileStream (fileName, FileMode. Open );
19 byte [] bytes = new byte [(int) fs. Length];
20 fs. Read (bytes, 0, bytes. Length );
21 fs. Close ();
22 if (File. Exists (fileName ))
23 File. Delete (fileName );
24
25 Response. ContentType = "application/octet-stream ";
26 Response. AddHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. UrlEncode (fileName, System. Text. Encoding. UTF8 ));
27 Response. BinaryWrite (bytes );
28 Response. Flush ();
29 Response. End ();
30}
Add the following code to the export event (such as the Button_Click event ):
1 string outputFileName = "newtext.doc"; // name of the exported file
2
3 FileStream fs = new FileStream ("test. rtf", FileMode. Create );
4 ASPxHtmlEditor1.Export (DevExpress. Web. ASPxHtmlEditor. HtmlEditorExportFormat. Rtf, fs); // note that the Rtf format is output first.
5 fs. Close ();
6 fs. Close ();
7
8 RichEditDocumentServer srv = new RichEditDocumentServer ();
9 srv. LoadDocument ("test. rtf", DocumentFormat. Rtf );
10 srv. BeginUpdate ();
11 SetPrintOptions (srv );
12 srv. EndUpdate ();
13 srv. SaveDocument (outputFileName, DocumentFormat. Doc); // export the Doc format. If the export OpenXml (. Docx) format is used, the Chinese font is lost. 14
15 if (File. Exists ("test. rtf "))
16 File. Delete ("test. rtf ");
17
18 PushToBrowser (outputFileName );
The output text is as follows:
I tried several groups of data and made a three-time fitting. Let's use it together first. (X indicates the expected centimeter number. For example, 2.5 cm y indicates the parameter used in the program, for example, 295)
Reprinted please indicate the source http://www.cnblogs.com/LFDX/p/4688938.html