/// <summary> /// Prepares this instance for rendering. /// </summary> public void PrepareDocument() { PdfFlattenVisitor visitor = new PdfFlattenVisitor(); visitor.Visit(_document); _previousListNumbers = new Dictionary <ListType, int>(3); _previousListNumbers[ListType.NumberList1] = 0; _previousListNumbers[ListType.NumberList2] = 0; _previousListNumbers[ListType.NumberList3] = 0; _formattedDocument = new FormattedDocument(_document, this); //REM: Size should not be necessary in this case. #if true XGraphics gfx = XGraphics.CreateMeasureContext(new XSize(2000, 2000), XGraphicsUnit.Point, XPageDirection.Downwards); #else #if GDI XGraphics gfx = XGraphics.FromGraphics(Graphics.FromHwnd(IntPtr.Zero), new XSize(2000, 2000)); #endif #if WPF XGraphics gfx = XGraphics.FromDrawingContext(null, new XSize(2000, 2000), XGraphicsUnit.Point); #endif #endif // _previousListNumber = int.MinValue; //gfx.MUH = _unicode; //gfx.MFEH = _fontEmbedding; _previousListInfo = null; _formattedDocument.Format(gfx); }
/// <summary> /// Prepares new bitmap image for drawing. /// </summary> public void BeginImage() { #if GDI int factor = 2; int width = (int)(WidthInPoint * factor); int height = (int)(HeightInPoint * factor); this.image = new Bitmap(width, height); this.image.SetResolution(72 * factor, 72 * factor); this.gdiGfx = Graphics.FromImage(this.image); this.gdiGfx.SmoothingMode = SmoothingMode.HighQuality; // Draw a bounding box Rectangle rect = new Rectangle(0, 0, width - 1, height - 1); this.gdiGfx.FillRectangle(Brushes.WhiteSmoke, rect); //this.gdiGfx.DrawRectangle(new Pen(Brushes.LightGray, factor), rect); this.gdiGfx.ScaleTransform(factor, factor); this.imgGfx = XGraphics.FromGraphics(this.gdiGfx, new XSize(WidthInPoint, HeightInPoint), XGraphicsUnit.Point); #endif #if WPF int factor = 4; int width = (int)(WidthInPoint * factor); int height = (int)(HeightInPoint * factor); this.image = new RenderTargetBitmap(width, height, 72 * factor, 72 * factor, PixelFormats.Default); this.dv = new DrawingVisual(); this.dc = this.dv.RenderOpen(); // Draw a bounding box //Rect rect = new Rect(0, 0, width - 1, height - 1); Rect rect = new Rect(0, 0, WidthInPoint * 4 / 3 - 1, HeightInPoint * 4 / 3 - 1); //this.dc.DrawRectangle(Brushes.WhiteSmoke, new Pen(Brushes.LightGray, 1), rect); this.dc.DrawRectangle(Brushes.WhiteSmoke, null, rect); //double d = 10; //Pen pen = new Pen(Brushes.Red, 5); //this.dc.DrawLine(pen, rect.TopLeft, rect.TopLeft + new Vector(d, d)); //this.dc.DrawLine(pen, rect.TopRight, rect.TopRight + new Vector(-d, d)); //this.dc.DrawLine(pen, rect.BottomLeft, rect.BottomLeft + new Vector(d, -d)); //this.dc.DrawLine(pen, rect.BottomRight, rect.BottomRight + new Vector(-d, -d)); //this.dc.PushTransform(new ScaleTransform(factor, factor)); this.imgGfx = XGraphics.FromDrawingContext(this.dc, new XSize(WidthInPoint, HeightInPoint), XGraphicsUnit.Point); #endif }
protected override void OnRender(DrawingContext drawingContext) { base.OnRender(drawingContext); //drawingContext.DrawLine(new Pen(Brushes.Green, 10), new Point(10, 10), new Point(100, 150)); drawingContext.PushTransform(new ScaleTransform(0.75, 0.75)); XGraphics gfx = XGraphics.FromDrawingContext(drawingContext, new XSize(100, 100), XGraphicsUnit.Millimeter); if (RenderEvent != null) { try { RenderEvent(gfx); } catch { RenderEvent = null; } } else { Draw(gfx); } }
/// <summary> /// Called when the Ddl property has changed. /// </summary> void DdlUpdated() { if (_ddl != null) { _document = DdlReader.DocumentFromString(_ddl); _renderer = new DocumentRenderer(_document); //this.renderer.PrivateFonts = this.privateFonts; _renderer.PrepareDocument(); //IDocumentPaginatorSource source = this.documentViewer.Document; //IDocumentPaginatorSource source = this.documentViewer.Document; int pageCount = _renderer.FormattedDocument.PageCount; if (pageCount == 0) { return; } // HACK: hardcoded A4 size //double pageWidth = XUnit.FromMillimeter(210).Presentation; //double pageHeight = XUnit.FromMillimeter(297).Presentation; //Size a4 = new Size(pageWidth, pageHeight); XUnit pageWidth, pageHeight; Size size96 = GetSizeOfPage(1, out pageWidth, out pageHeight); FixedDocument fixedDocument = new FixedDocument(); fixedDocument.DocumentPaginator.PageSize = size96; for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++) { try { size96 = GetSizeOfPage(1, out pageWidth, out pageHeight); DrawingVisual dv = new DrawingVisual(); DrawingContext dc = dv.RenderOpen(); //XGraphics gfx = XGraphics.FromDrawingContext(dc, new XSize(XUnit.FromMillimeter(210).Point, XUnit.FromMillimeter(297).Point), XGraphicsUnit.Point); XGraphics gfx = XGraphics.FromDrawingContext(dc, new XSize(pageWidth.Point, pageHeight.Presentation), XGraphicsUnit.Point); _renderer.RenderPage(gfx, pageNumber, PageRenderOptions.All); dc.Close(); // Create page content PageContent pageContent = new PageContent(); pageContent.Width = size96.Width; pageContent.Height = size96.Height; FixedPage fixedPage = new FixedPage(); fixedPage.Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0xFE, 0xFE, 0xFE)); UIElement visual = new DrawingVisualPresenter(dv); FixedPage.SetLeft(visual, 0); FixedPage.SetTop(visual, 0); fixedPage.Width = size96.Width; fixedPage.Height = size96.Height; fixedPage.Children.Add(visual); fixedPage.Measure(size96); fixedPage.Arrange(new Rect(new Point(), size96)); fixedPage.UpdateLayout(); ((IAddChild)pageContent).AddChild(fixedPage); fixedDocument.Pages.Add(pageContent); } catch (Exception) { // eat exception } viewer.Document = fixedDocument; } } else { viewer.Document = null; } }
internal void OnRender() { if (RenderEvent != null) { //IDocumentPaginatorSource source = this.documentViewer.Document; DrawingVisual dv = new DrawingVisual(); DrawingContext dc = dv.RenderOpen(); XGraphics gfx = XGraphics.FromDrawingContext(dc, new XSize(XUnit.FromMillimeter(210).Point, XUnit.FromMillimeter(297).Point), XGraphicsUnit.Point); try { RenderEvent(gfx); } catch { } dc.Close(); //DrawingGroup dg = dv.Drawing; // Create page content PageContent pageContent = new PageContent(); FixedPage fixedPage = new FixedPage(); fixedPage.Background = Brushes.GhostWhite; //UIElement visual = dv; // CreateSecondVisual(false); UIElement visual = new DrawingVisualPresenter(dv); FixedPage.SetLeft(visual, 0); FixedPage.SetTop(visual, 0); double pageWidth = XUnit.FromMillimeter(210).Presentation; double pageHeight = XUnit.FromMillimeter(297).Presentation; fixedPage.Width = pageWidth; fixedPage.Height = pageHeight; fixedPage.Children.Add((UIElement)visual); Size size = new Size(pageWidth, pageHeight); fixedPage.Measure(size); fixedPage.Arrange(new Rect(new Point(), size)); fixedPage.UpdateLayout(); ((IAddChild)pageContent).AddChild(fixedPage); FixedDocument fixedDocument = new FixedDocument(); fixedDocument.DocumentPaginator.PageSize = size; fixedDocument.Pages.Add(pageContent); this.documentViewer.Document = fixedDocument; string savedButton = System.Windows.Markup.XamlWriter.Save(fixedDocument); } else { this.documentViewer.Document = null; } //base.OnRender(drawingContext); ////drawingContext.DrawLine(new Pen(Brushes.Green, 10), new Point(10, 10), new Point(100, 150)); //drawingContext.PushTransform(new ScaleTransform(0.75, 0.75)); //XGraphics gfx = XGraphics.FromDrawingContext(drawingContext, new XSize(100, 100), XGraphicsUnit.Millimeter); //if (RenderEvent != null) //{ // try // { // RenderEvent(gfx); // } // catch // { // RenderEvent = null; // } //} //else // Draw(gfx); }