Ticket T881025
Visible to All Users

How to insert a table at the beginning of a new page?

created 5 years ago

Hello.
How can I insert a table at the beginning of a new page.

I use the code from the example to insert the table.

C#
if (e.VariableName == "MyCustomInsertTableField") { System.Data.DataTable dataTable = T589084.Models.MyModel.GetDT1(); DevExpress.XtraRichEdit.RichEditDocumentServer docServer = new DevExpress.XtraRichEdit.RichEditDocumentServer(); string PageBreak = new string('\u000C', 1); int dataTableRows = dataTable.Rows.Count; int dataTableColumns = dataTable.Columns.Count; DevExpress.XtraRichEdit.API.Native.Table table = docServer.Document.Tables.Create(docServer.Document.Range.End, dataTableRows + 1, dataTableColumns); for (int i = 0; i < dataTableColumns; i++) { docServer.Document.InsertText(table[0, i].Range.Start, dataTable.Columns[i].ColumnName); } table.ForEachCell(delegate (DevExpress.XtraRichEdit.API.Native.TableCell cell, int rowIndex, int cellIndex) { if (rowIndex > 0) { docServer.Document.InsertText(cell.Range.Start, dataTable.Rows[rowIndex - 1][dataTable.Columns[cellIndex].ColumnName].ToString()); } }); e.Value = docServer.Document; e.Handled = true; } };

Tried to do so

C#
docServer.Document.AppendText(new string('\u000C', 1));
JavaScript
richEdit.Document.CaretPosition = richEdit.Document.Range.End;

Tell me how to implement this behavior?
Thanks!

Answers approved by DevExpress Support

created 5 years ago

Hi Vitaliy,

You need to insert a page break before a table in the CalculateDocumentVariable event handler. Attached is an example that demonstrates how to accomplish this.
Should you have further questions, let me know.

    Show previous comments (3)
    DevExpress Support Team 5 years ago

      Hello Vitaliy,

      If a document starts with a table and a new content is inserted into the beginning of the document, this content will be always inserted into this table.

      If you need to insert your content before the table, you need to use an opposite approach:

      1. Generate the required content in an empty document using the RichEditDocumentServer API.
      2. Append content of your original document to the end of this new document.
      3. Load the resulting document into MVC RichEditControl:
      C#
      public ActionResult CustomActionRichEditPartial(string action) { if(action == "insertTable") { using(System.IO.MemoryStream ms = new System.IO.MemoryStream(RichEditExtension.SaveCopy("MyRichEdit", DevExpress.XtraRichEdit.DocumentFormat.Doc))) { DevExpress.XtraRichEdit.RichEditDocumentServer docServer = new DevExpress.XtraRichEdit.RichEditDocumentServer(); docServer.Document.Paragraphs.Append(); //docServer.Document.AppendText(DevExpress.Office.Characters.PageBreak.ToString()); //string PageBreak = new string('\u000C', 1); int dataTableRows = 5; // dataTable.Rows.Count; int dataTableColumns = 5; // dataTable.Columns.Count; DevExpress.XtraRichEdit.API.Native.Table table = docServer.Document.Tables.Create(docServer.Document.Range.End, dataTableRows + 1, dataTableColumns); for(int i = 0; i < dataTableColumns; i++) { docServer.Document.InsertText(table[0, i].Range.Start, string.Format("Column{0}", i)); //dataTable.Columns[i].ColumnName } table.ForEachCell(delegate (DevExpress.XtraRichEdit.API.Native.TableCell cell, int rowIndex, int cellIndex) { if(rowIndex > 0) { docServer.Document.InsertText(cell.Range.Start, string.Format("Value{0}-{1}", rowIndex, cellIndex)); } }); docServer.Document.AppendText(DevExpress.Office.Characters.PageBreak.ToString()); docServer.Document.AppendDocumentContent(ms, DevExpress.XtraRichEdit.DocumentFormat.Doc); docServer.SaveDocument(Server.MapPath("~/Documents") + "/Test.doc", DevExpress.XtraRichEdit.DocumentFormat.Doc); DevExpress.Web.Office.DocumentManager.CloseDocument(Server.MapPath("~/Documents") + "/Test.doc"); } } return PartialView("_RichEditPartial"); }

      Please check the attached sample and let me know if this solution meets your requirements.

        Thanks you!
        It works as it should.

        DevExpress Support Team 5 years ago

          You are welcome!

          Do not hesitate to contact us if you have additional questions or need further assistance.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.