using System;
using Spire.Pdf;
namespace Booklet
{
class Program
{
static void Main(string[] args)
{
//Create a pdf document.
PdfDocument doc = new PdfDocument();
String srcPdf = @"Sample2.pdf";
float width = PdfPageSize.A4.Width * 2;
float height = PdfPageSize.A4.Height;
doc.CreateBooklet(srcPdf, width, height, true);
//Save pdf file.
doc.SaveToFile("Booklet.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("Booklet.pdf");
}
}
}
Imports Spire.Pdf
Namespace Booklet
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Create a pdf document.
Dim doc As New PdfDocument()
Dim srcPdf As String = "Sample2.pdf"
Dim width As Single = PdfPageSize.A4.Width * 2
Dim height As Single = PdfPageSize.A4.Height
doc.CreateBooklet(srcPdf, width, height, True)
'Save pdf file.
doc.SaveToFile("Booklet.pdf")
doc.Close()
'Launching the Pdf file.
Process.Start("Booklet.pdf")
End Sub
End Class
End Namespace
The sample demonstrates how to extract images and text from PDF document.
(NO screenshot)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using Spire.Pdf;
namespace Extraction
{
class Program
{
static void Main(string[] args)
{
//Create a pdf document.
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"Sample2.pdf");
StringBuilder buffer = new StringBuilder();
IList<Image> images = new List<Image>();
foreach (PdfPageBase page in doc.Pages)
{
buffer.Append(page.ExtractText());
foreach (Image image in page.ExtractImages())
{
images.Add(image);
}
}
doc.Close();
//save text
String fileName = "TextInPdf.txt";
File.WriteAllText(fileName, buffer.ToString());
//save image
int index = 0;
foreach (Image image in images)
{
String imageFileName
= String.Format("Image-{0}.png", index++);
image.Save(imageFileName, ImageFormat.Png);
}
//Launching the Text file.
System.Diagnostics.Process.Start(fileName);
}
}
}
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text
Imports Spire.Pdf
Namespace Extraction
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Create a pdf document.
Dim doc As New PdfDocument()
doc.LoadFromFile("Sample2.pdf")
Dim buffer As New StringBuilder()
Dim images As IList(Of Image) = New List(Of Image)()
For Each page As PdfPageBase In doc.Pages
buffer.Append(page.ExtractText())
For Each image As Image In page.ExtractImages()
images.Add(image)
Next image
Next page
doc.Close()
'save text
Dim fileName As String = "TextInPdf.txt"
File.WriteAllText(fileName, buffer.ToString())
'save image
Dim index As Integer = 0
For Each image As Image In images
Dim imageFileName As String = String.Format("Image-{0}.png", index)
index += 1
image.Save(imageFileName, ImageFormat.Png)
Next image
'Launching the Text file.
Process.Start(fileName)
End Sub
End Class
End Namespace
The sample demonstrates how to merge multiple PDF documents to one PDF document.
(NO screenshot)
using System;
using Spire.Pdf;
namespace MergeDocuments
{
class Program
{
static void Main(string[] args)
{
//pdf document list
String[] files = new String[]
{
@"Sample3.pdf",
@"Sample2.pdf",
@"Sample1.pdf"
};
//open pdf documents
PdfDocument[] docs = new PdfDocument[files.Length];
for (int i = 0; i < files.Length; i++)
{
docs[i] = new PdfDocument(files[i]);
}
//append document
docs[0].AppendPage(docs[1]);
//import page
for (int i = 0; i < docs[2].Pages.Count; i = i + 2)
{
docs[0].InsertPage(docs[2], i);
}
//Save pdf file.
docs[0].SaveToFile("MergeDocuments.pdf");
//close
foreach (PdfDocument doc in docs)
{
doc.Close();
}
//Launching the Pdf file.
System.Diagnostics.Process.Start("MergeDocuments.pdf");
}
}
}
Imports Spire.Pdf
Namespace MergeDocuments
Friend Class Program
Shared Sub Main(ByVal args() As String)
'pdf document list
Dim files() As String = {"Sample3.pdf", "Sample2.pdf", "Sample1.pdf"}
'open pdf documents
Dim docs(files.Length - 1) As PdfDocument
For i As Integer = 0 To files.Length - 1
docs(i) = New PdfDocument(files(i))
Next i
'append document
docs(0).AppendPage(docs(1))
'import page
For i As Integer = 0 To docs(2).Pages.Count - 1 Step 2
docs(0).InsertPage(docs(2), i)
Next i
'Save pdf file.
docs(0).SaveToFile("MergeDocuments.pdf")
'close
For Each doc As PdfDocument In docs
doc.Close()
Next doc
'Launching the Pdf file.
Process.Start("MergeDocuments.pdf")
End Sub
End Class
End Namespace
The sample demonstrates how to split one PDF document to multiple PDF documents.
(NO screenshot)
using System;
using Spire.Pdf;
namespace SplitDocument
{
class Program
{
static void Main(string[] args)
{
//open pdf document
PdfDocument doc = new PdfDocument(@"Sample3.pdf");
String pattern = "SplitDocument-{0}.pdf";
doc.Split(pattern);
String lastPageFileName
= String.Format(pattern, doc.Pages.Count - 1);
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start(lastPageFileName);
}
}
}
Imports Spire.Pdf
Namespace SplitDocument
Friend Class Program
Shared Sub Main(ByVal args() As String)
'open pdf document
Dim doc As New PdfDocument("Sample3.pdf")
Dim pattern As String = "SplitDocument-{0}.pdf"
doc.Split(pattern)
Dim lastPageFileName As String = String.Format(pattern, doc.Pages.Count - 1)
doc.Close()
'Launching the Pdf file.
Process.Start(lastPageFileName)
End Sub
End Class
End Namespace
using System;
using System.Windows;
namespace MergePdfTest
{
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 MergePdfTest
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
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Spire.Pdf;
namespace MergePdfTest
{
public partial class MainPage : UserControl
{
//create two blank pdf files
PdfDocument document1 = new PdfDocument();
PdfDocument document2 = new PdfDocument();
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//merge the two files
document1.AppendPage(document2);
//save the result using a SaveFileDialog
SaveFileDialog saveFileDiaglog = new SaveFileDialog();
bool? resultSave = saveFileDiaglog.ShowDialog();
if (resultSave.HasValue && resultSave.Value)
{
document1.SaveToStream(saveFileDiaglog.OpenFile());
}
}
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
}
private void button2_Click(object sender, RoutedEventArgs e)
{
//new a OpenFileDialog to open the pdf files will be merged
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Pdf Documents (*.pdf)|*.pdf";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = true;
bool? userClickedOK = openFileDialog1.ShowDialog();
//Process input if the user clicked OK.
if (userClickedOK == true)
{
//get the selected files
System.IO.Stream fileStream1 = openFileDialog1.Files.ElementAt(0).OpenRead();
System.IO.Stream filestream2 = openFileDialog1.Files.ElementAt(1).OpenRead();
document1.LoadFromStream(fileStream1);
document2.LoadFromStream(filestream2);
}
}
}
}
Imports System.Windows
Imports System.Windows.Controls
Imports Spire.Pdf
Namespace MergePdfTest
Partial Public Class MainPage
Inherits UserControl
'create two blank pdf files
Private document1 As New PdfDocument()
Private document2 As New PdfDocument()
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
'merge the two files
document1.AppendPage(document2)
'save the result using a SaveFileDialog
Dim saveFileDiaglog As New SaveFileDialog()
Dim resultSave? As Boolean = saveFileDiaglog.ShowDialog()
If resultSave.HasValue AndAlso resultSave.Value Then
document1.SaveToStream(saveFileDiaglog.OpenFile())
End If
End Sub
Private Sub LayoutRoot_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
End Sub
Private Sub button2_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
'new a OpenFileDialog to open the pdf files will be merged
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "Pdf Documents (*.pdf)|*.pdf"
openFileDialog1.FilterIndex = 1
openFileDialog1.Multiselect = True
Dim userClickedOK? As Boolean = openFileDialog1.ShowDialog()
'Process input if the user clicked OK.
If userClickedOK = True Then
'get the selected files
Dim fileStream1 As System.IO.Stream = openFileDialog1.Files.ElementAt(0).OpenRead()
Dim filestream2 As System.IO.Stream = openFileDialog1.Files.ElementAt(1).OpenRead()
document1.LoadFromStream(fileStream1)
document2.LoadFromStream(filestream2)
End If
End Sub
End Class
End Namespace