/// <summary>
        /// Creates a same RadFixedDocument using details text 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 GenerateSampleDocument(MyPdfContent value)
        {
            double defaultLeftIndent = 50;
            double defaultLineHeight = 16;

            var document = new RadFixedDocument();
            var page     = document.Pages.AddPage();

            page.Size = PageSize;

            var editor = new FixedContentEditor(page);

            editor.Position.Translate(defaultLeftIndent, 50);

            double currentTopOffset = 110;

            editor.Position.Translate(defaultLeftIndent, currentTopOffset);
            double maxWidth = page.Size.Width - defaultLeftIndent * 2;

            DocumentHelpers.DrawDescription(editor, maxWidth);

            currentTopOffset += defaultLineHeight * 4;
            editor.Position.Translate(defaultLeftIndent, currentTopOffset);

            using (editor.SaveProperties())
            {
                DocumentHelpers.DrawFunnelFigure(editor);
            }

            // use the uploaded text
            DocumentHelpers.DrawText(editor, maxWidth, value);

            return(document);
        }
        /// <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);
        }
        public HttpResponseMessage Post([FromBody] MyPdfContent value)
        {
            RadFixedDocument document;

            // determine if we're using images or not and generate the document accordingly
            if (string.IsNullOrEmpty(value.ImageBase64))
            {
                // if there's no image, generate a sample with graphics
                document = GenerateSampleDocument(value);
            }
            else
            {
                // if there is image data, insert the image
                document = GenerateImageDocument(value);
            }

            // Now we export the RadDocument as a PDF file and save it to the server
            var provider = new PdfFormatProvider();

            provider.Export(document);
            byte[] bytes = provider.Export(document);

            var response = new HttpResponseMessage();

            response.Content = new ByteArrayContent(bytes);
            response.Content.Headers.ContentDisposition          = new ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = value.RequestedFileName;
            response.Content.Headers.ContentType   = new MediaTypeHeaderValue("application/pdf");
            response.Content.Headers.ContentLength = bytes.Length;

            return(response);
        }
示例#4
0
        private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var pdfContent = new MyPdfContent
            {
                Title             = "Hello World!",
                Body              = "This is the body of the PDF document, and where I'd put some general text",
                BackgroundColor   = "#FFFFFF",
                RequestedFileName = "HelloWorld.pdf"
            };

            OutputLabel.Text = "Generating...";

            // Serialize the content
            var bodyContent = new StringContent(JsonConvert.SerializeObject(pdfContent), Encoding.UTF8, "application/json");

            // post it to the API method that will generate the PDF
            using (var response = await _client.PostAsync(ServiceConstants.PdfGeneratorApi, bodyContent))
            {
                if (!response.IsSuccessStatusCode)
                {
                    OutputLabel.Text = $"Error: {response.StatusCode}";
                }
                else
                {
                    var bytes = await response.Content.ReadAsByteArrayAsync();

                    using (var pdfStream = new MemoryStream(bytes))
                    {
                        pdfViewer.DocumentSource = new PdfDocumentSource(pdfStream);

                        OutputLabel.Text = $"Complete: Size: {pdfStream.Length}";
                    }
                }
            }
        }
示例#5
0
        private async void UploadTextButton_OnClicked(object sender, RoutedEventArgs e)
        {
            this.BusyIndicator.IsActive = true;
            this.BusyIndicator.Content  = "creating content...";

            var contentForPdf = new MyPdfContent
            {
                Title             = "Hello World!",
                Body              = "This property can be used to set the string content that would be in the body of the PDF document.",
                BackgroundColor   = "#FFFFFF",
                RequestedFileName = "HelloWorld.pdf"
            };

            this.BusyIndicator.Content = "uploading content and generating pdf...";

            var pdfBytes = await WebApiService.Instance.GenerateDocumentAsync(contentForPdf);

            if (pdfBytes != null)
            {
                this.BusyIndicator.Content = "saving file...";

                await this.SaveFileAsync(pdfBytes, contentForPdf.RequestedFileName);
            }
            else
            {
                this.OutputLabel.Foreground = new SolidColorBrush(Colors.OrangeRed);
                this.OutputLabel.Text       = "Error Uploading Content. Try again.";
            }

            this.BusyIndicator.Content  = "";
            this.BusyIndicator.IsActive = false;
        }
示例#6
0
        public async Task <byte[]> GenerateDocumentAsync(MyPdfContent content)
        {
            // serialize into json  todo - use BSON and skip json conversion
            var json = JsonConvert.SerializeObject(content);

            // StringContent for POST
            var bodyContent = new StringContent(json, Encoding.UTF8, "application/json");

            // POST the StringContent to the controller that generates a PDF and return the file byte[]
            using (var response = await _client.PostAsync(ServiceConstants.PdfGeneratorApi, bodyContent))
            {
                if (response.IsSuccessStatusCode)
                {
                    // Return the byte[] of the PDF file.
                    var bytes = await response.Content.ReadAsByteArrayAsync();

                    return(bytes);
                }
                else
                {
                    await new MessageDialog($"Error: {response.StatusCode}", "Upload error").ShowAsync();
                    return(null);
                }
            }
        }
示例#7
0
        private async void UploadPictureButton_OnClick(object sender, RoutedEventArgs e)
        {
            this.BusyIndicator.IsActive = true;

            this.BusyIndicator.Content = "generating and encoding bitmap...";

            // Render the UI as a png and convert to base64
            var base64String = await RenderGridAsync();

            this.BusyIndicator.Content = "creating content...";

            // NOTE: the png is serialized as a base64 string, you can choose whichever method is best for you
            var contentForPdf = new MyPdfContent
            {
                ImageBase64       = base64String,
                Title             = "Example: UWP XAML -> PDF",
                Body              = "This document is an example of rendering a XAML UI in a PDF document using Telerik Document Processing Libraries.",
                BackgroundColor   = "#FFFFFF",
                RequestedFileName = "XamlContentTest.pdf"
            };

            this.BusyIndicator.Content = "uploading content and generating pdf...";

            var pdfBytes = await WebApiService.Instance.GenerateDocumentAsync(contentForPdf);

            if (pdfBytes != null)
            {
                this.BusyIndicator.Content = "saving file...";
                await this.SaveFileAsync(pdfBytes, contentForPdf.RequestedFileName);
            }
            else
            {
                this.OutputLabel.Foreground = new SolidColorBrush(Colors.OrangeRed);
                this.OutputLabel.Text       = "Error Uploading Content. Try again.";
            }

            this.BusyIndicator.Content  = "";
            this.BusyIndicator.IsActive = false;
        }
        public static void DrawText(FixedContentEditor editor, double maxWidth, MyPdfContent content)
        {
            double currentTopOffset = 470;

            currentTopOffset += defaultLineHeight * 2;
            editor.Position.Translate(defaultLeftIndent, currentTopOffset);
            editor.TextProperties.FontSize = 11;

            Block block = new Block();

            block.TextProperties.FontSize = 11;
            block.TextProperties.TrySetFont(new FontFamily("Arial"));
            block.InsertText("A wizard's job is to vex ");
            using (block.GraphicProperties.Save())
            {
                block.GraphicProperties.FillColor = new RgbColor(255, 146, 208, 80);
                block.InsertText("chumps");
            }

            block.InsertText(" quickly in fog.");
            editor.DrawBlock(block, new Size(maxWidth, double.PositiveInfinity));

            currentTopOffset += defaultLineHeight;
            editor.Position.Translate(defaultLeftIndent, currentTopOffset);

            block = new Block();
            block.TextProperties.FontSize = 11;
            block.TextProperties.TrySetFont(new FontFamily("Trebuchet MS"));
            block.InsertText("A wizard's job is to vex chumps ");
            using (block.TextProperties.Save())
            {
                block.TextProperties.UnderlinePattern = UnderlinePattern.Single;
                block.TextProperties.UnderlineColor   = editor.GraphicProperties.FillColor;
                block.InsertText("quickly");
            }

            block.InsertText(" in fog.");
            editor.DrawBlock(block, new Size(maxWidth, double.PositiveInfinity));

            currentTopOffset += defaultLineHeight;
            editor.Position.Translate(defaultLeftIndent, currentTopOffset);
            block = new Block();
            block.TextProperties.FontSize = 11;
            block.TextProperties.TrySetFont(new FontFamily("Algerian"));
            block.InsertText("A ");
            using (block.TextProperties.Save())
            {
                block.TextProperties.UnderlinePattern = UnderlinePattern.Single;
                block.TextProperties.UnderlineColor   = editor.GraphicProperties.FillColor;
                block.InsertText("wizard's");
            }

            block.InsertText(" job is to vex chumps quickly in fog.");
            editor.DrawBlock(block, new Size(maxWidth, double.PositiveInfinity));

            currentTopOffset += defaultLineHeight;
            editor.Position.Translate(defaultLeftIndent, currentTopOffset);

            editor.TextProperties.TrySetFont(new FontFamily("Lucida Calligraphy"));
            editor.DrawText("A wizard's job is to vex chumps quickly in fog.", new Size(maxWidth, double.PositiveInfinity));

            currentTopOffset += defaultLineHeight + 2;
            editor.Position.Translate(defaultLeftIndent, currentTopOffset);
            block = new Block();
            block.TextProperties.FontSize = 11;
            block.TextProperties.TrySetFont(new FontFamily("Consolas"));
            block.InsertText("A wizard's job is to vex chumps ");
            using (block.TextProperties.Save())
            {
                block.TextProperties.TrySetFont(new FontFamily("Consolas"), FontStyles.Normal, FontWeights.Bold);
                block.InsertText("quickly");
            }

            block.InsertText(" in fog.");
            editor.DrawBlock(block, new Size(maxWidth, double.PositiveInfinity));

            currentTopOffset += defaultLineHeight;
            editor.Position.Translate(defaultLeftIndent, currentTopOffset);
            editor.DrawText("El pingüino Wenceslao hizo kilómetros bajo exhaustiva lluvia y frío; añoraba a su querido cachorro.", new Size(maxWidth, double.PositiveInfinity));

            currentTopOffset += defaultLineHeight;
            editor.Position.Translate(defaultLeftIndent, currentTopOffset);

            // TO VERIFY CONTENT WAS UPLOADED THIS IS THE PASSED TEXT
            editor.DrawText(content.Body, new Size(maxWidth, double.PositiveInfinity));
        }