How to create a PDF document using flow layout model in UWP
Syncfusion Essential PDF is the UWP PDF library used to create, read, and edit PDF documents. Using this library, you can create a PDF document using flow layout model in UWP platform.
Steps to draw the flow layout text in a PDF document programmatically:
- Create a new UWP application project.
- Install the Syncfusion.Pdf.UWP NuGet package as reference to your UWP applications from NuGet.org.
- In the MainPage.xaml, add a new button as follows.
<Page x:Class="FlowLayoutSample.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:FlowLayoutSample" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid> <Button x:Name="btn" Content="Generate PDF" Click="OnButtonClick" HorizontalAlignment="Center" VerticalAlignment="Center"></Button> </Grid> </Page>
- Include the following namespaces in the MainPage.xaml.cs file.
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics;
- Include the following code snippet in the click event of the button in MainPage.xaml.cs to draw the flow layout text in a PDF document and save it in a stream.
//Create a new PDF document PdfDocument document = new PdfDocument(); //Add a new PDF page PdfPage page = document.Pages.Add(); //Get the page size SizeF pageSize = page.GetClientSize(); //Create a new PDF font PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 18, PdfFontStyle.Bold); //Create a new PdfTextElement instance PdfTextElement element = new PdfTextElement("Adventure Works Cycles", font, PdfBrushes.Blue); //Set alignment element.StringFormat = new PdfStringFormat(PdfTextAlignment.Center); //Draw the text element to PDF PdfLayoutResult result = element.Draw(page, new RectangleF(new PointF(0, 0), pageSize)); //Input text string text = @"Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is located in Bothell, Washington with 290 employees, several regional sales teams are located throughout their market base."; //Create new PDF font font = new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Regular); //create text element element = new PdfTextElement(text, font); //Draw the text element based on the existing layout result result = element.Draw(page, new RectangleF(new PointF(0, result.Bounds.Bottom + 10), pageSize)); //Get the image file as stream Stream imageStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("FlowLayoutSample.Assets.cycle.png"); //Create new PdfBitmap instance PdfBitmap image = new PdfBitmap(imageStream); //Draw the image to PDF result = image.Draw(page, new RectangleF(new PointF(135, result.Bounds.Bottom + 10), pageSize)); text = @"In 2000, Adventure Works Cycles bought a small manufacturing plant, Importadores Neptuno, located in Mexico. Importadores Neptuno manufactures several critical subcomponents for the Adventure Works Cycles product line. These subcomponents are shipped to the Bothell location for final product assembly. In 2001, Importadores Neptuno, became the sole manufacturer and distributor of the touring bicycle product group."; //Create new text elemnt instance element = new PdfTextElement(text, font); //Draw the text element result = element.Draw(page, new RectangleF(new PointF(0, result.Bounds.Bottom + 10), pageSize)); MemoryStream stream = new MemoryStream(); //Save the PDF document document.Save(stream); //Close the PDF document document.Close(true); //Save the PDF stream to physical file Save(stream, "output.pdf");
- Use the following helper method to save the stream as a physical file and open the file for viewing.
#region Helper Methods public async void Save(Stream stream, string filename) { stream.Position = 0; StorageFile stFile; if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))) { FileSavePicker savePicker = new FileSavePicker(); savePicker.DefaultFileExtension = ".pdf"; savePicker.SuggestedFileName = "Output"; savePicker.FileTypeChoices.Add("Adobe PDF Document", new List<string>() { ".pdf" }); stFile = await savePicker.PickSaveFileAsync(); } else { StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting); } if (stFile != null) { Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite); Stream st = fileStream.AsStreamForWrite(); st.Write((stream as MemoryStream).ToArray(), 0, (int)stream.Length); st.Flush(); st.Dispose(); fileStream.Dispose(); MessageDialog msgDialog = new MessageDialog("Do you want to view the Document?", "File created."); UICommand yesCmd = new UICommand("Yes"); msgDialog.Commands.Add(yesCmd); UICommand noCmd = new UICommand("No"); msgDialog.Commands.Add(noCmd); IUICommand cmd = await msgDialog.ShowAsync(); if (cmd == yesCmd) { // Launch the retrieved file bool success = await Windows.System.Launcher.LaunchFileAsync(stFile); } } } #endregion
By executing the program, you will get the PDF document as follows.
A complete working sample can be downloaded from FlowLayoutSample.zip
Take a moment to peruse the documentation, where you can find other options like drawing Unicode text, RTL text, Complex script language text, ordered list and unordered list to the PDF document.
Refer here to explore the rich set of Syncfusion Essential PDF features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.