public static void Main() { string dataDir = Path.GetFullPath("../../../Data/"); Document doc = new Document(dataDir + "TestFile.docx"); // This sample introduces the RenderedDocument class and other related classes which provide an API wrapper for // the LayoutEnumerator. This allows you to access the layout entities of a document using a DOM style API. // Create a new RenderedDocument class from a Document object. RenderedDocument layoutDoc = new RenderedDocument(doc); // The following examples demonstrate how to use the wrapper API. // This snippet returns the third line of the first page and prints the line of text to the console. RenderedLine line = layoutDoc.Pages[0].Columns[0].Lines[2]; Console.WriteLine("Line: " + line.Text); // With a rendered line the original paragraph in the document object model can be returned. Paragraph para = line.Paragraph; Console.WriteLine("Paragraph text: " + para.Range.Text); // Retrieve all the text that appears of the first page in plain text format (including headers and footers). string pageText = layoutDoc.Pages[0].Text; Console.WriteLine(); // Loop through each page in the document and print how many lines appear on each page. foreach (RenderedPage page in layoutDoc.Pages) { LayoutCollection <LayoutEntity> lines = page.GetChildEntities(LayoutEntityType.Line, true); Console.WriteLine("Page {0} has {1} lines.", page.PageIndex, lines.Count); } // This method provides a reverse lookup of layout entities for any given node (with the exception of runs and nodes in the // header and footer). Console.WriteLine(); Console.WriteLine("The lines of the second paragraph:"); foreach (RenderedLine paragraphLine in layoutDoc.GetLayoutEntitiesOfNode(doc.FirstSection.Body.Paragraphs[1])) { Console.WriteLine(string.Format("\"{0}\"", paragraphLine.Text.Trim())); Console.WriteLine(paragraphLine.Rectangle.ToString()); Console.WriteLine(); } }
/// <summary> /// Internal render method /// </summary> /// <param name="pageNumber"></param> /// <param name="rotationAngle"></param> /// <returns></returns> private async Task BackgroundPageRenderAsync() { using (PdfPage page = RenderedDocument.GetPage(renderedPageIndex)) { var renderOptions = new PdfPageRenderOptions(); renderOptions.BackgroundColor = Windows.UI.Colors.Transparent; double actualWidth = PreviewArea.ActualWidth - PreviewBorder.Margin.Left - PreviewBorder.Margin.Right - 4; ScaledRectangle displayedPageDimension; if (RenderedRotationAngle % 180 == 0) { // Raw page orientation matches displayed page orientation displayedPageDimension = new ScaledRectangle(page.Size.Height, page.Size.Width); } else { // Raw page orientation does not match the displayed page orientation displayedPageDimension = new ScaledRectangle(page.Size.Width, page.Size.Height); } double stretchedHeight = displayedPageDimension.GetScaledHeight(actualWidth); if (stretchedHeight > MaxHeight) { renderOptions.DestinationHeight = (uint)(MaxHeight); renderOptions.DestinationWidth = (uint)displayedPageDimension.GetScaledWidth(MaxHeight); } else { renderOptions.DestinationHeight = (uint)stretchedHeight; renderOptions.DestinationWidth = (uint)actualWidth; } // update decent border around the previewed page PreviewBorder.Height = renderOptions.DestinationHeight; PreviewBorder.Width = renderOptions.DestinationWidth; var stream = new InMemoryRandomAccessStream(); await page.RenderToStreamAsync(stream, renderOptions); BitmapImage src = new BitmapImage(); await src.SetSourceAsync(stream); Preview.Source = src; RotateTransform rotationTransform = new RotateTransform() { Angle = RenderedRotationAngle, CenterX = (PreviewBorder.Width - 4) / 2, CenterY = (PreviewBorder.Height - 4) / 2 }; ScaleTransform scaleTransform; if (RenderedRotationAngle % 180 == 0) { scaleTransform = new ScaleTransform() { ScaleX = 1, ScaleY = 1, }; } else { scaleTransform = new ScaleTransform() { CenterX = (PreviewBorder.Width - 4) / 2, CenterY = (PreviewBorder.Height - 4) / 2, ScaleX = displayedPageDimension.AspectRatio, ScaleY = 1 / displayedPageDimension.AspectRatio, }; } TransformGroup renderTransform = new TransformGroup(); renderTransform.Children.Add(rotationTransform); renderTransform.Children.Add(scaleTransform); Preview.RenderTransform = renderTransform; SetBorderStyle(); } }