Spire.PDF - Page 4

PDF TableLayout in C#, VB.NET

2011-04-06 05:30:25 Written by Administrator

The sample demonstrates how to set PDF table layout.

Download TableLayout.pdf

PDF Barcode in C#, VB.NET

2011-04-05 07:27:31 Written by Administrator

The sample demonstrates how to draw barcode in PDF document.

Download Barcode.pdf

Convert Webpage to PDF in WPF

2012-08-08 01:06:49 Written by Koohji

HTML to PDF solution can be quite a few when people search on google. However, most solutions are not proper in use since what you view on webpage is not always completely same with what you get on your PDF. Some information such as dynamic image and anchor text in HTML are easily lost. This section will introduce a simple method to clearly convert HTML to PDF without any cut on WPF platform.

Spire.PDF for WPF, as a professional PDF component, allows you to clearly convert HTML to PDF only by three lines of key code on WPF platform. In the solution, you only need to load HTML file with four parameters:

  • URL, the url of the webpage which will be converted to PDF file.
  • Enable JavaScrpit, indicates whether enables the JavaScript of the webpage.
  • Enable hyperlink, indicates whether enables the hyperlink of the webpage.
  • Auto detect page break, indicates whether splits auto the web page in the result PDF file.

Then, save your html as PDF to file by calling the method PdfDocument.LoadFromHTM. Please preview the result screenshot of the whole project first as prove:

Webpage to PDF

Before demonstrating the code, please feel free to download Spire.PDF and install it on system. Now, you can view the full code below:

[C#]
using Spire.Pdf;

namespace WPFhtmltopdf
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        [STAThread]
        private void button1_Click(object sender, RoutedEventArgs e)
        {

            //Create a new pdf document.
            PdfDocument doc = new PdfDocument();
            //load the webpage 
            String url = "http://msdn.microsoft.com/";
            doc.LoadFromHTML(url, false, true, true);
            //Save html webpage as pdf file.
            doc.SaveToFile("sample.pdf");
            doc.Close();
        }
    }
}
[VB.NET]
Imports Spire.Pdf

Namespace WPFhtmltopdf
	''' 
	''' Interaction logic for MainWindow.xaml
	''' 
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub
		 _
		Private Sub button1_Click(sender As Object, e As RoutedEventArgs)

			'Create a new pdf document.
			Dim doc As New PdfDocument()
			'load the webpage 
			Dim url As  = "http://msdn.microsoft.com/"
			doc.LoadFromHTML(url, False, True, True)
			'Save html webpage as pdf file.
			doc.SaveToFile("sample.pdf")
			doc.Close()
		End Sub
	End Class
End Namespace

Obviously, using this WPF PDF component, the webpage is easily converted to PDF. Both text and images are clearly shown in PDF file. Besides, Spire.PDF for WPF is a 100% secure PDF component software. No Malware, No Spyware and No Virus.

A Sound action is used to embed and play a sound file in PDF document. In Spire.PDF, we can create a sound action by using the PdfSoundAction class. Attributes like sound, volume and repeat can be specified for the sound action.

Refer below code example:

Step 1: Create a new PDF document and add a page to it.

PdfDocument document = new PdfDocument();
PdfPageBase page = document.Pages.Add();

Step 2: Create a sound action and set its attributes.

PdfSoundAction soundAction = new PdfSoundAction(@"C:\Users\Administrator\Desktop\because of you.wav");
soundAction.Sound.Bits = 15;
soundAction.Sound.Channels = PdfSoundChannels.Stereo;
soundAction.Sound.Encoding = PdfSoundEncoding.Signed;
soundAction.Volume = 0.8f;
soundAction.Repeat = true;

Step 3: Set the sound action to be executed when the PDF document is opened.

document.AfterOpenAction = soundAction;

Step 4: Save and close the PDF document.

document.SaveToFile("Output.pdf");
document.Close();

Full code:

using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.General;

namespace PDF_Sound_Action
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a new PDF document
            PdfDocument document = new PdfDocument();
            //Add a page
            PdfPageBase page = document.Pages.Add();

            //Create a sound action
            PdfSoundAction soundAction = new PdfSoundAction(@"C:\Users\Administrator\Desktop\because of you.wav");
            soundAction.Sound.Bits = 15;
            soundAction.Sound.Channels = PdfSoundChannels.Stereo;
            soundAction.Sound.Encoding = PdfSoundEncoding.Signed;
            soundAction.Volume = 0.8f;
            soundAction.Repeat = true;

            // Set the sound action to be executed when the PDF document is opened
            document.AfterOpenAction = soundAction;

            //Save and close the PDF document
            document.SaveToFile("Output.pdf");
            document.Close();
        }
    }
}
Page 4 of 20
page 4