- Demo
- App.xaml
- App.xaml.cs
- App.xaml.vb
- MainPage.xaml
- MainPage.xaml.cs
- MainPage.xaml.vb
The sample demonstrates how to set PDF page orientation for Silverlight via Spire.PDF.
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="pageOrientation_PDF.App"> <Application.Resources> </Application.Resources> </Application>
using System; using System.Windows; namespace pageOrientation_PDF { public partial class App : Application { public App() { this.Startup += this.Application_Startup; this.Exit += this.Application_Exit; this.UnhandledException += this.Application_UnhandledException; InitializeComponent(); } private void Application_Startup(object sender, StartupEventArgs e) { this.RootVisual = new MainPage(); } private void Application_Exit(object sender, EventArgs e) { } private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { // If the app is running outside of the debugger then report the exception using // the browser's exception mechanism. On IE this will display it a yellow alert // icon in the status bar and Firefox will display a script error. if (!System.Diagnostics.Debugger.IsAttached) { // NOTE: This will allow the application to continue running after an exception has been thrown // but not handled. // For production applications this error handling should be replaced with something that will // report the error to the website and stop the application. e.Handled = true; Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); }); } } private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e) { try { string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace; errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n"); System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");"); } catch (Exception) { } } } }
Imports System.Windows Namespace pageOrientation_PDF Partial Public Class App Inherits Application Public Sub New() AddHandler Me.Startup, AddressOf Application_Startup AddHandler Me.Exit, AddressOf Application_Exit AddHandler Me.UnhandledException, AddressOf Application_UnhandledException InitializeComponent() End Sub Private Sub Application_Startup(ByVal sender As Object, ByVal e As StartupEventArgs) Me.RootVisual = New MainPage() End Sub Private Sub Application_Exit(ByVal sender As Object, ByVal e As EventArgs) End Sub Private Sub Application_UnhandledException(ByVal sender As Object, ByVal e As ApplicationUnhandledExceptionEventArgs) ' If the app is running outside of the debugger then report the exception using ' the browser's exception mechanism. On IE this will display it a yellow alert ' icon in the status bar and Firefox will display a script error. If Not Debugger.IsAttached Then ' NOTE: This will allow the application to continue running after an exception has been thrown ' but not handled. ' For production applications this error handling should be replaced with something that will ' report the error to the website and stop the application. e.Handled = True Deployment.Current.Dispatcher.BeginInvoke(Sub() ReportErrorToDOM(e)) End If End Sub Private Sub ReportErrorToDOM(ByVal e As ApplicationUnhandledExceptionEventArgs) Try Dim errorMsg As String = e.ExceptionObject.Message + e.ExceptionObject.StackTrace errorMsg = errorMsg.Replace(""""c, "'"c).Replace(vbCrLf, vbLf) System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(""Unhandled Error in Silverlight Application " & errorMsg & """);") Catch e1 As Exception End Try End Sub End Class End Namespace
<UserControl x:Class="pageOrientation_PDF.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded"> <Button Content="RUN" Height="23" HorizontalAlignment="Left" Margin="297,265,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> </Grid> </UserControl>
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.IO; using System.Drawing; using Spire.Pdf; using Spire.Pdf.Graphics; namespace pageOrientation_PDF { public partial class MainPage : UserControl { private SaveFileDialog saveFileDialog = null; public MainPage() { InitializeComponent(); this.saveFileDialog = new SaveFileDialog(); this.saveFileDialog.Filter = "PDF Document(*.pdf)|*.pdf"; } private void button1_Click(object sender, RoutedEventArgs e) { //create a pdfDocument PdfDocument document = new PdfDocument(); //add one portrait page PdfSection section1 = document.Sections.Add(); section1.PageSettings.Orientation = PdfPageOrientation.Portrait; section1.PageSettings.Size = PdfPageSize.A4; PdfPageBase page = section1.Pages.Add(); //draw the portrait page DrawPage(page); //add one landscape page PdfSection section2 = document.Sections.Add(); section2.PageSettings.Orientation = PdfPageOrientation.Landscape; section2.PageSettings.Size = PdfPageSize.A4; PdfPageBase page21 = section2.Pages.Add(); //draw the landscape page DrawPage(page21); //save the pdfDocument bool? result = this.saveFileDialog.ShowDialog(); if (result.HasValue && result.Value) { using (Stream stream = this.saveFileDialog.OpenFile()) { document.SaveToStream(stream); } } } private void DrawPage(PdfPageBase page) { float pageWidth = page.Canvas.ClientSize.Width; float y = 0; //title y = y + 5; PdfBrush brush1 = new PdfSolidBrush(Color.FromArgb(255, 0, 0, 0)); PdfFont font1 = new PdfFont(PdfFontFamily.Helvetica, 30f); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); format1.CharacterSpacing = 1f; String text = "Spire.Office"; page.Canvas.DrawString(text, font1, brush1, pageWidth / 2, y, format1); SizeF size = font1.MeasureString(text, format1); y = y + size.Height + 6; //draw the content PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 15f); PdfStringFormat format = new PdfStringFormat(); PdfBrush brush = PdfBrushes.DeepSkyBlue; format.LineSpacing = font.Size * 1.5f; float x1 = 2*size.Width; y = y + size.Height; //content text = "Spire.Office for .NET is a compilation of every .NET component offered by e-iceblue. It includes Spire.Doc, Spire.XLS, Spire.PDFViewer, Spire.PDF and Spire.DataExport. Spire.Office contains the most up-to-date versions of the components above. Using Spire.Office for .NET developers can create a wide range of applications." + "Consistently offer high-quality, pure and stable .NET components for handling office format files. E-iceblue product lines include components for Word, Excel, PDF and component specially used for data exporting. For E-iceblue, customer service and products quality are the same most important and we guarantee 100% honesty to" + "customers. E-iceblue Welcome any feedback from customers and promise timely and effectively response. E-iceblue .NET component products save your time and save your money from now on."; PdfStringLayouter textLayouter = new PdfStringLayouter(); PdfStringLayoutResult result = textLayouter.Layout(text, font, format, new SizeF(pageWidth, 0)); foreach (LineInfo line in result.Lines) { page.Canvas.DrawString(line.Text, font, brush, 0, y, format); y = y + result.LineHeight; } } private void LayoutRoot_Loaded(object sender, RoutedEventArgs e) { } } }
Imports System.Windows Imports System.Windows.Controls Imports System.Windows.Media Imports System.IO Imports System.Drawing Imports Spire.Pdf Imports Spire.Pdf.Graphics Namespace pageOrientation_PDF Partial Public Class MainPage Inherits UserControl Private saveFileDialog As SaveFileDialog = Nothing Public Sub New() InitializeComponent() Me.saveFileDialog = New SaveFileDialog() Me.saveFileDialog.Filter = "PDF Document(*.pdf)|*.pdf" End Sub Private Sub button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) 'create a pdfDocument Dim document As New PdfDocument() 'add one portrait page Dim section1 As PdfSection = document.Sections.Add() section1.PageSettings.Orientation = PdfPageOrientation.Portrait section1.PageSettings.Size = PdfPageSize.A4 Dim page As PdfPageBase = section1.Pages.Add() 'draw the portrait page DrawPage(page) 'add one landscape page Dim section2 As PdfSection = document.Sections.Add() section2.PageSettings.Orientation = PdfPageOrientation.Landscape section2.PageSettings.Size = PdfPageSize.A4 Dim page21 As PdfPageBase = section2.Pages.Add() 'draw the landscape page DrawPage(page21) 'save the pdfDocument Dim result? As Boolean = Me.saveFileDialog.ShowDialog() If result.HasValue AndAlso result.Value Then Using stream As Stream = Me.saveFileDialog.OpenFile() document.SaveToStream(stream) End Using End If End Sub Private Sub DrawPage(ByVal page As PdfPageBase) Dim pageWidth As Single = page.Canvas.ClientSize.Width Dim y As Single = 0 'title y = y + 5 Dim brush1 As PdfBrush = New PdfSolidBrush(Color.FromArgb(255, 0, 0, 0)) Dim font1 As New PdfFont(PdfFontFamily.Helvetica, 30f) Dim format1 As New PdfStringFormat(PdfTextAlignment.Center) format1.CharacterSpacing = 1f Dim text As String = "Spire.Office" page.Canvas.DrawString(text, font1, brush1, pageWidth / 2, y, format1) Dim size As SizeF = font1.MeasureString(text, format1) y = y + size.Height + 6 'draw the content Dim font As New PdfFont(PdfFontFamily.Helvetica, 15f) Dim format As New PdfStringFormat() Dim brush As PdfBrush = PdfBrushes.DeepSkyBlue format.LineSpacing = font.Size * 1.5f Dim x1 As Single = 2*size.Width y = y + size.Height 'content text = "Spire.Office for .NET is a compilation of every .NET component offered by e-iceblue. It includes Spire.Doc, Spire.XLS, Spire.PDFViewer, Spire.PDF and Spire.DataExport. Spire.Office contains the most up-to-date versions of the components above. Using Spire.Office for .NET developers can create a wide range of applications." & "Consistently offer high-quality, pure and stable .NET components for handling office format files. E-iceblue product lines include components for Word, Excel, PDF and component specially used for data exporting. For E-iceblue, customer service and products quality are the same most important and we guarantee 100% honesty to" & "customers. E-iceblue Welcome any feedback from customers and promise timely and effectively response. E-iceblue .NET component products save your time and save your money from now on." Dim textLayouter As New PdfStringLayouter() Dim result As PdfStringLayoutResult = textLayouter.Layout(text, font, format, New SizeF(pageWidth, 0)) For Each line As LineInfo In result.Lines page.Canvas.DrawString(line.Text, font, brush, 0, y, format) y = y + result.LineHeight Next line End Sub Private Sub LayoutRoot_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) End Sub End Class End Namespace