private static void ExportContainerShape(RadDiagramContainerShape container, Rect enclosingBounds, RadFixedPage page) { var bounds = new Rect(container.Bounds.X - enclosingBounds.X, container.Bounds.Y - enclosingBounds.Y, container.Bounds.Width, container.Bounds.Height); var transformGroup = new TransformGroup(); transformGroup.Children.Add(new RotateTransform() { Angle = container.RotationAngle, CenterX = bounds.Width / 2, CenterY = bounds.Height / 2 }); transformGroup.Children.Add(new TranslateTransform() { X = bounds.X, Y = bounds.Y }); var position = new MatrixPosition(transformGroup.Value); FixedContentEditor containerEditor = CreateEditor(new EditorInfo(page, position, container, bounds, container.BorderBrush, container.RotationAngle), true); containerEditor.DrawRectangle(new Rect(new Point(), bounds.ToSize())); containerEditor.GraphicProperties.StrokeThickness = 0.5; var headerHeight = container.ContentBounds.Y - container.Bounds.Y - DiagramConstants.ContainerMargin; containerEditor.DrawRectangle(new Rect(new Point(0, headerHeight), new Size(bounds.Width, 0.5))); if (container.IsCollapsible) { var buttonTop = headerHeight / 2 - 2.5; var buttonLeft = bounds.Width - 20; if (container.IsCollapsed) { containerEditor.DrawLine(new Point(buttonLeft, buttonTop), new Point(buttonLeft + 4, buttonTop + 4)); containerEditor.DrawLine(new Point(buttonLeft + 4, buttonTop + 4), new Point(buttonLeft + 8, buttonTop)); if (container.CollapsedContent != null) { var contentHeight = container.ActualHeight - headerHeight; ExportContent(container, bounds, container.RotationAngle, page, (s) => { return(new Point(bounds.Center().X - s.Width / 2, bounds.Bottom - contentHeight / 2 - s.Height / 2)); }, container.CollapsedContent.ToString()); } } else { containerEditor.DrawLine(new Point(buttonLeft, buttonTop + 4), new Point(buttonLeft + 4, buttonTop)); containerEditor.DrawLine(new Point(buttonLeft + 4, buttonTop), new Point(buttonLeft + 8, buttonTop + 4)); } } if (container.Content != null) { ExportContent(container, bounds, container.RotationAngle, page, (s) => { return(new Point(bounds.Center().X - s.Width / 2, bounds.Top + headerHeight / 2 - s.Height / 2)); }); } }
private static void ExportTextShape(RadDiagramTextShape shape, Rect enclosingBounds, RadFixedPage page) { var bounds = new Rect(shape.Bounds.X - enclosingBounds.X, shape.Bounds.Y - enclosingBounds.Y, shape.Bounds.Width, shape.Bounds.Height); var transformGroup = new TransformGroup(); transformGroup.Children.Add(new RotateTransform() { Angle = shape.RotationAngle, CenterX = bounds.Width / 2, CenterY = bounds.Height / 2 }); transformGroup.Children.Add(new TranslateTransform() { X = bounds.X, Y = bounds.Y }); var position = new MatrixPosition(transformGroup.Value); FixedContentEditor containerEditor = CreateEditor(new EditorInfo(page, position, shape, bounds, shape.BorderBrush, shape.RotationAngle), true); containerEditor.DrawRectangle(new Rect(new Point(), bounds.ToSize())); if (shape.Content != null) { var center = bounds.Center(); ExportContent(shape, bounds, shape.RotationAngle, page, (s) => { return(new Point(bounds.Center().X - s.Width / 2, center.Y - s.Height / 2)); }); } }
private void AddPageWithImage() { RadFixedPage page = this.document.Pages.AddPage(); page.Size = PageSize; FixedContentEditor editor = new FixedContentEditor(page); editor.GraphicProperties.StrokeThickness = 0; editor.GraphicProperties.IsStroked = false; editor.GraphicProperties.FillColor = new RgbColor(200, 200, 200); editor.DrawRectangle(new Rect(0, 0, PageSize.Width, PageSize.Height)); Margins margins = new Margins(); editor.Position.Translate(margins.Left, margins.Top); Block block = new Block(); block.HorizontalAlignment = HorizontalAlignment.Center; block.TextProperties.FontSize = 22; block.InsertText("Image converted from PNG to JPG"); block.InsertLineBreak(); block.InsertText(string.Format("when ImageQuality set to {0}", this.imageQuality)); Size blockSize = block.Measure(RemainingPageSize); editor.DrawBlock(block, RemainingPageSize); editor.Position.Translate(margins.Left, blockSize.Height + margins.Top + 20); Block imageBlock = new Block(); imageBlock.HorizontalAlignment = HorizontalAlignment.Center; imageBlock.InsertImage(this.imageSource); editor.DrawBlock(imageBlock, RemainingPageSize); }
/// <summary> /// Creates a RadFixedDocument using an image from uploaded content /// </summary> /// <param name="value">the content to use</param> /// <returns>FixedDocument that can be exported asa PDF file</returns> private static RadFixedDocument GenerateImageDocument(MyPdfContent value) { // Load the image data into an ImageSource object Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource imageSource; using (var imgStream = new MemoryStream(Convert.FromBase64String(value.ImageBase64))) { imageSource = new Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource(imgStream); } // instantiate the document and add a page var document = new RadFixedDocument(); var page = document.Pages.AddPage(); page.Size = PageSize; // instantiate an editor, this is what writes all the content to the page var editor = new FixedContentEditor(page); editor.GraphicProperties.StrokeThickness = 0; editor.GraphicProperties.IsStroked = false; try { // use the uploaded value for background color var bgColor = (Color)ColorConverter.ConvertFromString(value.BackgroundColor); editor.GraphicProperties.FillColor = new RgbColor(bgColor.R, bgColor.G, bgColor.B); } catch { editor.GraphicProperties.FillColor = new RgbColor(255, 255, 255); } editor.DrawRectangle(new Rect(0, 0, PageSize.Width, PageSize.Height)); editor.Position.Translate(Margins.Left, Margins.Top); // Description text var block = new Block(); block.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center; block.TextProperties.FontSize = 22; // use the uploaded content for the title block.InsertText(value.Title); var blockSize = block.Measure(RemainingPageSize); editor.DrawBlock(block, RemainingPageSize); editor.Position.Translate(Margins.Left, blockSize.Height + Margins.Top + 20); // NOTE - This is where the ImageSource is used and drawn onto the document var imageBlock = new Block(); imageBlock.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center; imageBlock.InsertImage(imageSource); editor.DrawBlock(imageBlock, RemainingPageSize); return(document); }
private void AddPageWithImage(string description, ImageSource imageSource) { RadFixedPage page = this.document.Pages.AddPage(); page.Size = PageSize; FixedContentEditor editor = new FixedContentEditor(page); editor.GraphicProperties.StrokeThickness = 0; editor.GraphicProperties.IsStroked = false; editor.GraphicProperties.FillColor = new RgbColor(200, 200, 200); editor.DrawRectangle(new Rect(0, 0, PageSize.Width, PageSize.Height)); editor.Position.Translate(Margins.Left, Margins.Top); Block block = new Block(); block.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center; block.TextProperties.FontSize = 22; block.InsertText(description); Size blockSize = block.Measure(RemainingPageSize); editor.DrawBlock(block, RemainingPageSize); editor.Position.Translate(Margins.Left, blockSize.Height + Margins.Top + 20); Block imageBlock = new Block(); imageBlock.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center; imageBlock.InsertImage(imageSource); editor.DrawBlock(imageBlock, RemainingPageSize); }
private void CreateLinearGradient(RadFixedPage container) { #region radpdfprocessing-concepts-colors-and-color-spaces_1 FixedContentEditor containerEditor = new FixedContentEditor(container); LinearGradient linearGradient = new LinearGradient(new Point(0, 0), new Point(30, 30)); linearGradient.GradientStops.Add(new GradientStop(new RgbColor(0, 207, 0), 0)); linearGradient.GradientStops.Add(new GradientStop(new RgbColor(0, 102, 204), 0)); containerEditor.GraphicProperties.FillColor = linearGradient; containerEditor.DrawRectangle(new Rect(10, 10, 48, 29)); #endregion }
private void CreateTiling(RadFixedPage container) { #region radpdfprocessing-concepts-colors-and-color-spaces_2 FixedContentEditor containerEditor = new FixedContentEditor(container); Tiling tiling = new Tiling(new Rect(0, 0, 10, 10)); FixedContentEditor tilingEditor = new FixedContentEditor(tiling); tilingEditor.GraphicProperties.IsStroked = false; tilingEditor.GraphicProperties.FillColor = new RgbColor(128, 28, 43); tilingEditor.DrawRectangle(new Rect(2, 2, 5, 7)); containerEditor.GraphicProperties.FillColor = tiling; containerEditor.DrawCircle(new Point(30, 30), 20); #endregion }
public static RadFixedDocument CreatePagedDocument(FrameworkElement element, bool landscaped) { RadFixedDocument document = new RadFixedDocument(); double pageMagins = 20; var numberOfPages = landscaped ? Math.Ceiling(element.ActualHeight / element.ActualWidth * 1.4) : Math.Ceiling(element.ActualHeight / element.ActualWidth * .7); Size elementSizePerPage = new Size(element.ActualWidth, element.ActualHeight / numberOfPages); Size pageSize = new Size(elementSizePerPage.Width + 2 * pageMagins, elementSizePerPage.Height + 2 * pageMagins); for (int pageIndex = 0; pageIndex < numberOfPages; pageIndex++) { RadFixedPage page = document.Pages.AddPage(); page.Size = pageSize; FixedContentEditor editor = new FixedContentEditor(page, MatrixPosition.Default); editor.Position.Translate(pageMagins, pageMagins); // Draw dashed lines at the page clipping margins. using (editor.SaveGraphicProperties()) { editor.GraphicProperties.IsFilled = false; editor.GraphicProperties.IsStroked = true; editor.GraphicProperties.StrokeColor = new RgbColor(200, 200, 200); editor.GraphicProperties.StrokeDashArray = new double[] { 5, 5 }; editor.DrawRectangle(new Rect(0, 0, elementSizePerPage.Width, elementSizePerPage.Height)); } // Clip and translate the element, so that it is positioned correctly on different pages. using (editor.PushClipping(new Rect(pageMagins, pageMagins, elementSizePerPage.Width, elementSizePerPage.Height))) { editor.Position.Translate(0, -pageIndex * elementSizePerPage.Height); ExportHelper.ExportToPdf(element, editor); } } return(document); }
private static void DrawPageWithWidgets(RadFixedDocument document) { RadFixedPage page = document.Pages.AddPage(); FixedContentEditor editor = new FixedContentEditor(page); using (editor.SaveGraphicProperties()) { editor.GraphicProperties.IsFilled = true; editor.GraphicProperties.IsStroked = false; editor.GraphicProperties.StrokeThickness = 0; editor.GraphicProperties.FillColor = new RgbColor(209, 178, 234); editor.DrawRectangle(new Rect(50, 50, editor.Root.Size.Width - 100, editor.Root.Size.Height - 100)); } editor.Position.Translate(100, 100); Size widgetDimensions = new Size(200, 30); foreach (FormField field in document.AcroForm.FormFields) { switch (field.FieldType) { case FormFieldType.CheckBox: CheckBoxField check = (CheckBoxField)field; DrawNextWidgetWithDescription(editor, "CheckBox", (e) => e.DrawWidget(check, widgetDimensions)); break; case FormFieldType.ComboBox: ComboBoxField combo = (ComboBoxField)field; DrawNextWidgetWithDescription(editor, "ComboBox", (e) => e.DrawWidget(combo, widgetDimensions)); break; case FormFieldType.CombTextBox: CombTextBoxField comb = (CombTextBoxField)field; DrawNextWidgetWithDescription(editor, "Comb TextBox", (e) => e.DrawWidget(comb, widgetDimensions)); break; case FormFieldType.ListBox: ListBoxField list = (ListBoxField)field; DrawNextWidgetWithDescription(editor, "ListBox", (e) => e.DrawWidget(list, new Size(widgetDimensions.Width, widgetDimensions.Width))); break; case FormFieldType.PushButton: PushButtonField push = (PushButtonField)field; DrawNextWidgetWithDescription(editor, "Button", (e) => e.DrawWidget(push, widgetDimensions)); break; case FormFieldType.RadioButton: RadioButtonField radio = (RadioButtonField)field; foreach (RadioOption option in radio.Options) { DrawNextWidgetWithDescription(editor, option.Value, (e) => e.DrawWidget(radio, option, widgetDimensions)); } break; case FormFieldType.Signature: SignatureField signature = (SignatureField)field; DrawNextWidgetWithDescription(editor, "Signature", (e) => e.DrawWidget(signature, widgetDimensions)); break; case FormFieldType.TextBox: TextBoxField textBox = (TextBoxField)field; DrawNextWidgetWithDescription(editor, "TextBox", (e) => e.DrawWidget(textBox, widgetDimensions)); break; } } }
private void AddPageWithImage(string description, ImageSource imageSource) { RadFixedPage page = this.document.Pages.AddPage(); page.Size = PageSize; FixedContentEditor editor = new FixedContentEditor(page); editor.GraphicProperties.FillColor = new RgbColor(200, 200, 200); editor.DrawRectangle(new Rect(0, 0, PageSize.Width, PageSize.Height)); editor.Position.Translate(Margins.Left, Margins.Top); Block block = new Block(); block.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center; block.TextProperties.FontSize = 22; block.InsertText(description); Size blockSize = block.Measure(RemainingPageSize); editor.DrawBlock(block, RemainingPageSize); editor.Position.Translate(Margins.Left, blockSize.Height + Margins.Top + 20); Block imageBlock = new Block(); imageBlock.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center; imageBlock.InsertImage(imageSource); editor.DrawBlock(imageBlock, RemainingPageSize); }
private async void ExportButton_OnClicked(object sender, EventArgs e) { BusyIndicator.IsVisible = BusyIndicator.IsBusy = true; var success = await Task.Run(async() => { try { var imageBytes = await FileExtensions.LoadFileBytesAsync(App.EditedImageFilePath); using (var jpegStream = new MemoryStream(imageBytes)) { // Define page dimensions var pageSize = new Telerik.Documents.Primitives.Size(Telerik.Windows.Documents.Media.Unit.MmToDip(210), Telerik.Windows.Documents.Media.Unit.MmToDip(297)); var pageMargins = new Telerik.Documents.Primitives.Thickness(Telerik.Windows.Documents.Media.Unit.MmToDip(10)); var remainingPageSize = new Telerik.Documents.Primitives.Size(pageSize.Width - pageMargins.Left - pageMargins.Right, pageSize.Height - pageMargins.Top - pageMargins.Bottom); // Create in memory document // instantiate the document and add a page var document = new RadFixedDocument(); var page = document.Pages.AddPage(); page.Size = pageSize; // instantiate an editor, this is what writes all the content to the page var editor = new FixedContentEditor(page); editor.GraphicProperties.StrokeThickness = 0; editor.GraphicProperties.IsStroked = false; editor.GraphicProperties.FillColor = new RgbColor(255, 255, 255); editor.DrawRectangle(new Rect(0, 0, pageSize.Width, pageSize.Height)); editor.Position.Translate(pageMargins.Left, pageMargins.Top); var block = new Block(); block.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center; block.TextProperties.FontSize = 22; // use the uploaded content for the title block.InsertText("Generated in Xamarin.Forms!"); var blockSize = block.Measure(remainingPageSize); editor.DrawBlock(block, remainingPageSize); editor.Position.Translate(pageMargins.Left, blockSize.Height + pageMargins.Top + 20); // Create image that can be inserted into document using the jpeg's stream // !Note - Image is of type Telerik.Windows.Documents.Fixed.Model.Objects.Image var docImageSource = new Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource(jpegStream); // Draw the image into the document var imageBlock = new Block(); imageBlock.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center; imageBlock.InsertImage(docImageSource); editor.DrawBlock(imageBlock, remainingPageSize); // Export the document to Pdf var provider = new PdfFormatProvider(); if (File.Exists(App.PdfFilePath)) { File.Delete(App.PdfFilePath); } using (var fileStream = File.OpenWrite(App.PdfFilePath)) { provider.Export(document, fileStream); } return(true); } } catch (Exception ex) { Device.BeginInvokeOnMainThread(() => DisplayAlert("Error", ex.Message, "ok")); return(false); } }); BusyIndicator.IsVisible = BusyIndicator.IsBusy = false; if (success) { await Navigation.PushAsync(new DocumentViewerPage()); } }
private static Tiling CreateTiling(double offsetX, double offsetY, double width, SimpleColor color) { Tiling tiling = new Tiling(new Rect(0, 0, width, 2)); tiling.Position.Translate(offsetX, offsetY); var tilingEditor = new FixedContentEditor(tiling); tilingEditor.GraphicProperties.IsStroked = false; tilingEditor.GraphicProperties.FillColor = color; tilingEditor.DrawRectangle(new Rect(0, 0, width, 1)); LinearGradient gradient = new LinearGradient(new Point(0, 0), new Point(width, 0)); gradient.GradientStops.Add(new GradientStop(color, 0)); gradient.GradientStops.Add(new GradientStop(RgbColors.White, .5)); gradient.GradientStops.Add(new GradientStop(color, 1)); tilingEditor.GraphicProperties.FillColor = gradient; tilingEditor.DrawRectangle(new Rect(0, 1, width, 1)); return tiling; }
private void DrawChartFrame(double leftMargin, FixedContentEditor editor, out double offsetX, out double offsetY) { offsetX = leftMargin; offsetY = marginTop; editor.DrawRectangle(new Rect(offsetX, offsetY, chartWidth, chartHeight)); offsetY += 10; editor.Position.Translate(offsetX, offsetY); editor.TextProperties.FontSize = 18; editor.TextProperties.TrySetFont(new System.Windows.Media.FontFamily("Calibri"), FontStyles.Normal, FontWeights.Bold); DrawText(editor, "2013", chartWidth, HorizontalAlignment.Center); offsetY += 30; editor.Position.Translate(offsetX, offsetY); editor.TextProperties.TrySetFont(new System.Windows.Media.FontFamily("Calibri")); editor.TextProperties.FontSize = 10; editor.GraphicProperties.IsFilled = true; editor.GraphicProperties.IsStroked = false; }
private void DrawBarChartContent(FixedContentEditor editor) { editor.GraphicProperties.IsFilled = false; this.DrawCompanyLogo(editor); double leftMargin = this.GetLeftMargin(editor.Root.Size.Width); double offsetX; double offsetY; this.DrawChartFrame(leftMargin, editor, out offsetX, out offsetY); double offset = 20; double textWidth = 0; double rectMargin = 2; for (int i = 0; i < this.ExportedProductsCount; i++) { textWidth += rectSize + rectMargin + offset; textWidth += MeasureText(editor, this.products[i].Name).Width; } offsetX = leftMargin + ((chartWidth - textWidth) / 2); offsetY += 20; for (int i = 0; i < this.ExportedProductsCount; i++) { editor.Position.Translate(offsetX, offsetY); Tiling tiling = CreateTiling(offsetX, offsetY, rectSize, colors[i]); Block block = new Block(); block.GraphicProperties.FillColor = tiling; block.GraphicProperties.IsStroked = false; block.InsertRectangle(new Rect(0, 0, rectSize, rectSize)); block.GraphicProperties.FillColor = RgbColors.Black; block.InsertText(" " + this.products[i].Name); editor.DrawBlock(block); offsetX += block.DesiredSize.Width + offset; } offsetX = leftMargin; offsetY += 30; double markerHeight = (chartHeight - (offsetY - marginTop)) / markersCount; editor.Position.Translate(offsetX, offsetY); for (int i = markersCount - 1; i >= 0; i--) { DrawText(editor, string.Format("{0:C}", i * this.StepValue), markerAreaWidth, HorizontalAlignment.Right); if (i > 0) { offsetY += markerHeight; editor.Position.Translate(offsetX, offsetY); } } offsetX = leftMargin + markerAreaWidth + valuesMargin; double center = MeasureText(editor, "X").Height / 2; offsetY += center; double valueHeight = markerHeight / this.StepValue; double dataAreaWidth = chartWidth - markerAreaWidth - 2 * valuesMargin; double sectionWidth = dataAreaWidth / this.GetQuartersToExportCount(); double barWidth = (sectionWidth - 2 * valuesMargin - 2 * this.ExportedProductsCount * barMargin) / this.ExportedProductsCount; for (int j = 0; j < this.quartersToExport.Keys.Count; j++) { if (!this.quartersToExport[j]) { continue; } editor.Position.Translate(offsetX, offsetY + 5); editor.GraphicProperties.FillColor = RgbColors.Black; DrawText(editor, string.Format("Q{0}", j + 1), sectionWidth, HorizontalAlignment.Center); editor.Position.Translate(0, 0); offsetX += valuesMargin; for (int i = 0; i < this.ExportedProductsCount; i++) { Product product = this.products[i]; double h = product.Q[j] * valueHeight; offsetX += barMargin; Tiling tiling = CreateTiling(offsetX, offsetY - h, barWidth, colors[i]); editor.GraphicProperties.FillColor = tiling; editor.DrawRectangle(new Rect(offsetX, offsetY - h, barWidth, h)); offsetX += barWidth + barMargin; } offsetX += valuesMargin; } offsetX = leftMargin + markerAreaWidth + valuesMargin; DrawBarLine(editor, offsetX, offsetY, dataAreaWidth); }