Ticket T1019089
Visible to All Users

How to insert an image on a specific page in MVC RichEdit using a RichEditDocumentServer component

created 4 years ago (modified 4 years ago)

Hello,

our software allows doctors to file Medical Reports for their patients, and a customer requested a feature which involves adding a image (sort of a watermark, but not exactly a watermark) starting from one page. There's a feature on the software that allows the doctor to redo a medical report, but due to legal issues, the old report text must be shown on the medical report. The old report then must have a image saying "This text has no legal value". But the customer wants this image to appear on the beginning of the old text and then on every subsequent page after that. Is that possible? I did not find any "Page Collection" on DevExpress reference.

Thanks in advance

Show previous comments (2)
M M
MV SISTEMAS DE MEDICINA DIAGNOSTICA LTDA 4 years ago

    Also, I wish to know if it's possible to add a new text programmatically forcing it to a new page. Let's say we have a text consisting of 2 small paragraphs, then I want to insert a new paragraph on a new page, regardless of how much space is left on the current page, is it possible?

    Lanette (DevExpress Support) 4 years ago

      Hello,

      Thank you for your response.
      We may need additional time to research this scenario. Please bear with us.

      M M
      MV SISTEMAS DE MEDICINA DIAGNOSTICA LTDA 4 years ago

        Ok, thank you for your attention.

        We'll be waiting.

        Answers approved by DevExpress Support

        created 4 years ago

        Hello,

        Since MVC RichEdit's client-side document is not page based, you can insert images into the document on the server side. To accomplish the task, you can use the non-visual Word Processing Document API library.

        Please review the following Code Example to learn how to load MVC RichEdit's document to the Word Processing Document API library via a custom callback: RichEdit - How to insert RTF text to a document.

        This library allows you to access document layout elements (e.g., pages) using methods of the DocumentLayout class (use the RichEditDocumentServer.DocumentLayout property to retrieve it). Use the DocumentLayout.GetPageCount method to get a number of pages in the document. Then, call the DocumentLayout.GetPage(Int32) method to retrieve a layout page element for the specified page index. You can use the LayoutPage.MainContentRange property to determine a range that belongs to the current page.

        Here is a sample code snippet that demonstrates how to determine a document range that belongs to the last page of the document:

        C#
        MemoryStream memoryStream = new MemoryStream(); RichEditExtension.SaveCopy("RichEdit", memoryStream, DocumentFormat.Rtf); memoryStream.Position = 0; using (RichEditDocumentServer server = new RichEditDocumentServer()){ server.LoadDocument(memoryStream, DocumentFormat.Rtf); int pageCount = server.DocumentLayout.GetPageCount(); LayoutPage page = server.DocumentLayout.GetPage(pageCount - 1); DocumentRange lastPageRange = server.Document.CreateRange(page.MainContentRange.Start, page.MainContentRange.Length); ...

        Review the following Code Example to learn more about functions that the Layout API has: Document Layout API - Practical usage.

        Then, use the Document.Shapes.InsertPicture method to insert a picture into the specified document position. This method returns a Shape object. Set the Shape.TextWrapping property to TextWrappingType.BehindText to display the image behind text (similar to a watermark).

        C#
        var shape = server.Document.Shapes.InsertPicture(lastPageRange.Start, DocumentImageSource.FromStream(imageStream)); shape.TextWrapping = TextWrappingType.BehindText;

        You can also position the inserted shape on the page using the API described in this article: Position Shapes.

        As for inserting text starting with the next page, insert a page break after the last paragraph of the current page and call the Document.InsertText(DocumentPosition, String) method to insert text. Please refer to this ticket where a similar task is described for clarification: How to insert a table at the beginning of a new page?.

        After the document is modified, you can save it to a memory stream using the RichEditDocumentServer.SaveDocument method and load it to MVC RichEdit as described in this example: RichEdit - How to insert RTF text to a document.

          Show previous comments (1)
          DevExpress Support Team 4 years ago

            You are welcome.

            M M
            MV SISTEMAS DE MEDICINA DIAGNOSTICA LTDA 4 years ago

              Hi,

              it worked like a charm.

              I've added some stuff to my helper class:

              On it's Constructor I added the Document Layout

              C#
              public TextoHelper() { _richEdit = new DevExpress.XtraRichEdit.RichEditControl(); _doc = _richEdit.Document; _docLayout = _richEdit.DocumentLayout; }

              Then, I added a SetMilestone method, which will be responsible to set from which page it's going to set the watermark.

              C#
              public void SetMilestone() { var pageCount = _docLayout.GetPageCount(); _milestonePageIndex = pageCount - 1; }

              Then, when I've added all the content, I call a SetBackgroundImageFromMilestone, method, which is written as this:

              C#
              public void SetBackgroundImageFromMilestone(byte[] byteArray) { var pageCount = _docLayout.GetPageCount(); var lastPageIndex = pageCount - 1; if (lastPageIndex <= _milestonePageIndex) return; for (var i = _milestonePageIndex; i < pageCount; i++) { var currentPage = _docLayout.GetPage(i); var currentPageRange = _doc.CreateRange(currentPage.MainContentRange.Start, currentPage.MainContentRange.Length); _doc.CaretPosition = currentPageRange.Start; AddBackgroundImage(byteArray); } }

              And I also added a AddNovaPagina method, so I can write on a new page if needed:

              C#
              public void AddNovaPagina() { _doc.AppendText(DevExpress.Office.Characters.PageBreak.ToString()); }

              So, when I assemble the rtf parts, it goes like this:

              C#
              // snippet byte[] imgBytes = File.ReadAllBytes(imgPath); helper.AddTexto(newRtf); helper.AddNovaPagina(); helper.AddParagrafo(); helper.SetMilestone(); helper.AddParagrafo(); helper.AddTexto(oldRtf); helper.SetBackgroundImageFromMilestone(imgBytes);

              Thank you for your time.

              DevExpress Support Team 4 years ago

                Hi,

                I'm happy to hear that my assistance was helpful to you. Thank you for informing me. I greatly appreciate it!

                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.