|
| 1 | +--- |
| 2 | +title: Converting Comments of a RadFlowDocument to Annotations in RadFixedDocument |
| 3 | +description: Learn how to extract comments from Word documents and implement annotations in PDF using Telerik Document Processing libraries. |
| 4 | +type: how-to |
| 5 | +page_title: Extracting Word Document Comments and Converting to PDF Annotations |
| 6 | +meta_title: Extracting Word Document Comments and Converting to PDF Annotations |
| 7 | +slug: extracting-comments-flowdocuments-pdf-annotations |
| 8 | +tags: docx, word, comment, pdf, processing, flow, document, fixed, annotations |
| 9 | +res_type: kb |
| 10 | +ticketid: 1701044 |
| 11 | +--- |
| 12 | + |
| 13 | +<style> |
| 14 | +img[alt$="><"] { |
| 15 | + border: 1px solid lightgrey; |
| 16 | +} |
| 17 | +</style> |
| 18 | + |
| 19 | +## Environment |
| 20 | + |
| 21 | +| Version | Product | Author | |
| 22 | +| ---- | ---- | ---- | |
| 23 | +| 2025.3.806| RadWordsProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 24 | + |
| 25 | +## Description |
| 26 | + |
| 27 | +This article aims to demonstrate a sample approach of extracting comments from Word (DOCX) documents and add them as annotations to the converted PDF document. Currently, comments in Word documents are not preserved during the conversion to PDF. |
| 28 | + |
| 29 | +This knowledge base article also answers the following questions: |
| 30 | +- How to extract comments from Word documents using RadWordsProcessing? |
| 31 | +- How to convert the DOCX file to PDF format? |
| 32 | +- How to convert Word document comments to PDF annotations? |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +## Solution |
| 37 | + |
| 38 | +To achieve the desired behavior, follow these steps: |
| 39 | + |
| 40 | +1. Import the existing DOCX file to [RadFlowDocument]({%slug radwordsprocessing-model-radflowdocument%}). |
| 41 | + |
| 42 | +1. Convert the RadFlowDocument to PDF format ([RadFixedDocument]({%slug radpdfprocessing-model-radfixeddocument%})). |
| 43 | + |
| 44 | +1. Extract the [Comments]({%slug radwordsprocessing-model-comment%}) from the RadFlowDocument and add programmatically [Annotations]({%slug radpdfprocessing-model-annotations-overview%}) to the RadFixedDocument. |
| 45 | + |
| 46 | +1. Export the RadFixedDocument to PDF file. |
| 47 | + |
| 48 | +```csharp |
| 49 | + static void Main(string[] args) |
| 50 | + { |
| 51 | + RadFlowDocument flowDocument; |
| 52 | + using (Stream input = File.OpenRead("Test.docx")) |
| 53 | + { |
| 54 | + //or DocFormatProvider for .doc files |
| 55 | + var docxProvider = new DocxFormatProvider(); |
| 56 | + flowDocument = docxProvider.Import(input, TimeSpan.FromSeconds(10)); |
| 57 | + } |
| 58 | + |
| 59 | + // Convert RadFlowDocument to RadFixedDocument for PDF export |
| 60 | + var flowToPdfProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider(); |
| 61 | + RadFixedDocument fixedDocument; |
| 62 | + |
| 63 | + using (var memoryStream = new MemoryStream()) |
| 64 | + { |
| 65 | + flowToPdfProvider.Export(flowDocument, memoryStream, TimeSpan.FromSeconds(10)); |
| 66 | + memoryStream.Position = 0; |
| 67 | + |
| 68 | + var fixedPdfProvider = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider(); |
| 69 | + fixedDocument = fixedPdfProvider.Import(memoryStream, TimeSpan.FromSeconds(10)); |
| 70 | + } |
| 71 | + |
| 72 | + // Add annotations for each comment |
| 73 | + AddCommentsAsAnnotations(fixedDocument, flowDocument); |
| 74 | + |
| 75 | + string outputFilePath = "output.pdf"; |
| 76 | + // Export the final PDF with annotations |
| 77 | + using (Stream output = File.Create(outputFilePath)) |
| 78 | + { |
| 79 | + var pdfProvider = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider(); |
| 80 | + pdfProvider.Export(fixedDocument, output, TimeSpan.FromSeconds(10)); |
| 81 | + } |
| 82 | + |
| 83 | + Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); |
| 84 | + } |
| 85 | + |
| 86 | + private static void AddCommentsAsAnnotations(RadFixedDocument fixedDocument, RadFlowDocument flowDocument) |
| 87 | + { |
| 88 | + // This is a simplified approach - in a real scenario, you'd need to map comment positions |
| 89 | + // from the flow document to the fixed document coordinates |
| 90 | +
|
| 91 | + int commentIndex = 0; |
| 92 | + foreach (Comment comment in flowDocument.Comments) |
| 93 | + { |
| 94 | + |
| 95 | + var start = comment.CommentRangeStart; |
| 96 | + var end = comment.CommentRangeEnd; |
| 97 | + var paragraph = start.Paragraph; |
| 98 | + System.Text.StringBuilder commentTextBuilder = new System.Text.StringBuilder(); |
| 99 | + foreach (Paragraph commentBlock in comment.Blocks) |
| 100 | + { |
| 101 | + foreach (Run inline in commentBlock.Inlines) |
| 102 | + { |
| 103 | + commentTextBuilder.AppendLine(inline.Text); |
| 104 | + } |
| 105 | + } |
| 106 | + string commentText = commentTextBuilder.ToString(); |
| 107 | + // Get all inlines between start and end |
| 108 | + var inlines = paragraph.Inlines |
| 109 | + .SkipWhile(inline => inline != start) |
| 110 | + .Skip(1) |
| 111 | + .TakeWhile(inline => inline != end); |
| 112 | + |
| 113 | + var associatedText = string.Join("", inlines |
| 114 | + .OfType<Run>() |
| 115 | + .Select(run => run.Text)); |
| 116 | + |
| 117 | + Debug.WriteLine($"Comment: {commentText}"); |
| 118 | + Debug.WriteLine($"Associated Text: {associatedText}"); |
| 119 | + |
| 120 | + if (fixedDocument.Pages.Count > 0) |
| 121 | + { |
| 122 | + TextSearch search = new TextSearch(fixedDocument); |
| 123 | + SearchResult result = search.Find(associatedText, TextSearchOptions.Default); |
| 124 | + if (result.Result != null) |
| 125 | + { |
| 126 | + System.Windows.Rect rect = result.GetWordBoundingRect(); |
| 127 | + RadFixedPage page = result.GetResultPage(); |
| 128 | + TextAnnotation annotation = page.Annotations.AddText(rect); |
| 129 | + annotation.Contents = commentTextBuilder.ToString(); |
| 130 | + annotation.Opacity = 0.5; |
| 131 | + annotation.Color = new RgbColor(255, 0, 0); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | +``` |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +## See Also |
| 141 | + |
| 142 | +- [RadFlowDocument]({%slug radwordsprocessing-model-radflowdocument%}) |
| 143 | +- [RadFixedDocument]({%slug radpdfprocessing-model-radfixeddocument%}) |
| 144 | + |
0 commit comments