Monday, 05 March 2012 07:29
Export PDF Document to images
The sample demonstrates how to export PDF pages as images by PdfDocumentViewer Component.
using System; using System.Drawing; using System.IO; using System.Windows.Forms; using Spire.PdfViewer.Forms; namespace Spire.PdfView.Demos.Export { class Program { private static PdfDocumentViewer viewer = null; [STAThread] static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); viewer = new PdfDocumentViewer(); viewer.LoadFromFile("PDFViewer.pdf"); //form and child components Form mainForm = new Form(); mainForm.Text = "Spire.PdfView Demo - Export"; mainForm.Size = new System.Drawing.Size(800, 600); mainForm.StartPosition = FormStartPosition.CenterScreen; TableLayoutPanel table = new TableLayoutPanel(); table.ColumnCount = 3; table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50)); table.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 20)); table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50)); table.RowCount = 2; table.RowStyles.Add(new RowStyle(SizeType.Percent, 100)); table.RowStyles.Add(new RowStyle(SizeType.Absolute, 30)); table.Controls.Add(viewer, 0, 0); table.SetColumnSpan(viewer, 3); viewer.Dock = DockStyle.Fill; //Export current page to one image Button button = new Button(); button.Text = "Export to one image"; button.Size = new Size(180, 24); button.TextAlign = ContentAlignment.MiddleCenter; table.Controls.Add(button, 0, 1); button.Dock = DockStyle.Right; button.Click += ExportToOneImage; //Export current pdf document to multiple images button = new Button(); button.Text = "Export to multiple images"; button.Size = new Size(180, 24); button.TextAlign = ContentAlignment.MiddleCenter; table.Controls.Add(button, 2, 1); button.Dock = DockStyle.Left; button.Click += ExportToMultipleImages; mainForm.Controls.Add(table); table.Dock = DockStyle.Fill; Application.Run(mainForm); } private static void ExportToOneImage(object sender, EventArgs e) { if (viewer.PageCount > 0) { SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "PNG Format(*.png)|*.png"; if (dialog.ShowDialog() == DialogResult.OK) { int currentPage = viewer.CurrentPageNumber; Bitmap image = viewer.SaveAsImage(currentPage - 1); image.Save(dialog.FileName); } } } private static void ExportToMultipleImages(object sender, EventArgs e) { if (viewer.PageCount > 0) { FolderBrowserDialog dialog = new FolderBrowserDialog(); if (dialog.ShowDialog() == DialogResult.OK) { int currentPage = viewer.CurrentPageNumber; Bitmap[] images = viewer.SaveAsImage(0, currentPage - 1); for (int i = 0; i < images.Length; i++) { String fileName = Path.Combine(dialog.SelectedPath, String.Format("PDFViewer-{0}.png", i)); images[i].Save(fileName); } } } } } }
Imports System.Drawing Imports System.IO Imports System.Windows.Forms Imports Spire.PdfViewer.Forms Namespace Spire.PdfView.Demos.Export Friend NotInheritable Class Program Private Shared viewer As PdfDocumentViewer = Nothing _ Shared Sub Main() Application.EnableVisualStyles() Application.SetCompatibleTextRenderingDefault(False) viewer = New PdfDocumentViewer() viewer.LoadFromFile("PDFViewer.pdf") 'form and child components Dim mainForm As New Form() mainForm.Text = "Spire.PdfView Demo - Export" mainForm.Size = New System.Drawing.Size(800, 600) mainForm.StartPosition = FormStartPosition.CenterScreen Dim table As New TableLayoutPanel() table.ColumnCount = 3 table.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 50)) table.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 20)) table.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 50)) table.RowCount = 2 table.RowStyles.Add(New RowStyle(SizeType.Percent, 100)) table.RowStyles.Add(New RowStyle(SizeType.Absolute, 30)) table.Controls.Add(viewer, 0, 0) table.SetColumnSpan(viewer, 3) viewer.Dock = DockStyle.Fill 'Export current page to one image Dim button As New Button() button.Text = "Export to one image" button.Size = New Size(180, 24) button.TextAlign = ContentAlignment.MiddleCenter table.Controls.Add(button, 0, 1) button.Dock = DockStyle.Right AddHandler button.Click, AddressOf ExportToOneImage 'Export current pdf document to multiple images button = New Button() button.Text = "Export to multiple images" button.Size = New Size(180, 24) button.TextAlign = ContentAlignment.MiddleCenter table.Controls.Add(button, 2, 1) button.Dock = DockStyle.Left AddHandler button.Click, AddressOf ExportToMultipleImages mainForm.Controls.Add(table) table.Dock = DockStyle.Fill Application.Run(mainForm) End Sub Private Shared Sub ExportToOneImage(ByVal sender As Object, ByVal e As EventArgs) If viewer.PageCount > 0 Then Dim dialog As New SaveFileDialog() dialog.Filter = "PNG Format(*.png)|*.png" If dialog.ShowDialog() = DialogResult.OK Then Dim currentPage As Integer = viewer.CurrentPageNumber Dim image As Bitmap = viewer.SaveAsImage(currentPage - 1) image.Save(dialog.FileName) End If End If End Sub Private Shared Sub ExportToMultipleImages(ByVal sender As Object, ByVal e As EventArgs) If viewer.PageCount > 0 Then Dim dialog As New FolderBrowserDialog() If dialog.ShowDialog() = DialogResult.OK Then Dim currentPage As Integer = viewer.CurrentPageNumber Dim images As Bitmap() = viewer.SaveAsImage(0, currentPage - 1) For i As Integer = 0 To images.Length - 1 Dim fileName As [String] = Path.Combine(dialog.SelectedPath, [String].Format("PDFViewer-{0}.png", i)) images(i).Save(fileName) Next End If End If End Sub End Class End Namespace
Published in
PDF Document Viewer
Monday, 25 July 2011 09:12
XLS to PDF in C#, VB.NET
The sample demonstrates how to convert Excel workbook to PDF file via Spire.XLS.
static void Main() { Spire.Xls.Workbook workbook = new Spire.Xls.Workbook(); workbook.LoadFromFile(@"DataTableSample.xls"); Spire.Xls.Converter.PdfConverter pdfConverter = new Spire.Xls.Converter.PdfConverter(workbook); Spire.Pdf.PdfDocument pdfDocument = new Spire.Pdf.PdfDocument(); Spire.Xls.Converter.PdfConverterSettings settings = new Spire.Xls.Converter.PdfConverterSettings(); settings.EmbedFonts = true; settings.TemplateDocument = pdfDocument; pdfDocument = pdfConverter.Convert(settings); pdfDocument.SaveToFile("XLS-to-PDF.pdf"); pdfDocument.Close(); }
Shared Sub Main() Dim workbook As New Spire.Xls.Workbook() workbook.LoadFromFile("DataTableSample.xls") Dim pdfConverter As New Spire.Xls.Converter.PdfConverter(workbook) Dim pdfDocument As New Spire.Pdf.PdfDocument() Dim settings As New Spire.Xls.Converter.PdfConverterSettings() settings.EmbedFonts = True settings.TemplateDocument = pdfDocument pdfDocument = pdfConverter.Convert(settings) pdfDocument.SaveToFile("XLS-to-PDF.pdf") pdfDocument.Close() End Sub
Published in
Misc
Wednesday, 06 April 2011 09:53
PDF Split in C#, VB.NET
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
Published in
Document Operation
Wednesday, 06 April 2011 09:50
PDF Merge in C#, VB.NET
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
Published in
Document Operation
Wednesday, 06 April 2011 09:47
PDF Extract in C#, VB.NET
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
Published in
Document Operation
Wednesday, 06 April 2011 09:44
PDF Booklet in C#, VB.NET
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
Published in
Document Operation
Wednesday, 06 April 2011 09:41
PDF ViewPreference in C#, VB.NET
using System; using System.Drawing; using Spire.Pdf; using Spire.Pdf.Graphics; namespace ViewerPreference { class Program { static void Main(string[] args) { //Create a pdf document. PdfDocument doc = new PdfDocument(); // Create one page PdfPageBase page = doc.Pages.Add(); //Draw the page DrawPage(page); //set view reference doc.ViewerPreferences.CenterWindow = true; doc.ViewerPreferences.DisplayTitle = false; doc.ViewerPreferences.FitWindow = false; doc.ViewerPreferences.HideMenubar = true; doc.ViewerPreferences.HideToolbar = true; doc.ViewerPreferences.PageLayout = PdfPageLayout.SinglePage; //Save pdf file. doc.SaveToFile("ViewerPreference.pdf"); doc.Close(); //Launching the Pdf file. System.Diagnostics.Process.Start("ViewerPreference.pdf"); } private static void DrawPage(PdfPageBase page) { float pageWidth = page.Canvas.ClientSize.Width; float y = 0; //page header PdfPen pen1 = new PdfPen(Color.LightGray, 1f); PdfBrush brush1 = new PdfSolidBrush(Color.LightGray); PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Italic)); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Right); String text = "Demo of Spire.Pdf"; page.Canvas.DrawString(text, font1, brush1, pageWidth, y, format1); SizeF size = font1.MeasureString(text, format1); y = y + size.Height + 1; page.Canvas.DrawLine(pen1, 0, y, pageWidth, y); //title y = y + 5; PdfBrush brush2 = new PdfSolidBrush(Color.Black); PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format2 = new PdfStringFormat(PdfTextAlignment.Center); format2.CharacterSpacing = 1f; text = "Summary of Science"; page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2); size = font2.MeasureString(text, format2); y = y + size.Height + 6; //icon PdfImage image = PdfImage.FromFile(@"Wikipedia_Science.png"); page.Canvas.DrawImage(image, new PointF(pageWidth - image.PhysicalDimension.Width, y)); float imageLeftSpace = pageWidth - image.PhysicalDimension.Width - 2; float imageBottom = image.PhysicalDimension.Height + y; //refenrence content PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 9f)); PdfStringFormat format3 = new PdfStringFormat(); format3.ParagraphIndent = font3.Size * 2; format3.MeasureTrailingSpaces = true; format3.LineSpacing = font3.Size * 1.5f; String text1 = "(All text and picture from "; String text2 = "Wikipedia"; String text3 = ", the free encyclopedia)"; page.Canvas.DrawString(text1, font3, brush2, 0, y, format3); size = font3.MeasureString(text1, format3); float x1 = size.Width; format3.ParagraphIndent = 0; PdfTrueTypeFont font4 = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Underline)); PdfBrush brush3 = PdfBrushes.Blue; page.Canvas.DrawString(text2, font4, brush3, x1, y, format3); size = font4.MeasureString(text2, format3); x1 = x1 + size.Width; page.Canvas.DrawString(text3, font3, brush2, x1, y, format3); y = y + size.Height; //content PdfStringFormat format4 = new PdfStringFormat(); text = System.IO.File.ReadAllText(@"Summary_of_Science.txt"); PdfTrueTypeFont font5 = new PdfTrueTypeFont(new Font("Arial", 10f)); format4.LineSpacing = font5.Size * 1.5f; PdfStringLayouter textLayouter = new PdfStringLayouter(); float imageLeftBlockHeight = imageBottom - y; PdfStringLayoutResult result = textLayouter.Layout(text, font5, format4, new SizeF(imageLeftSpace, imageLeftBlockHeight)); if (result.ActualSize.Height < imageBottom - y) { imageLeftBlockHeight = imageLeftBlockHeight + result.LineHeight; result = textLayouter.Layout(text, font5, format4, new SizeF(imageLeftSpace, imageLeftBlockHeight)); } foreach (LineInfo line in result.Lines) { page.Canvas.DrawString(line.Text, font5, brush2, 0, y, format4); y = y + result.LineHeight; } PdfTextWidget textWidget = new PdfTextWidget(result.Remainder, font5, brush2); PdfTextLayout textLayout = new PdfTextLayout(); textLayout.Break = PdfLayoutBreakType.FitPage; textLayout.Layout = PdfLayoutType.Paginate; RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize); textWidget.StringFormat = format4; textWidget.Draw(page, bounds, textLayout); } } }
Imports System.Drawing Imports Spire.Pdf Imports Spire.Pdf.Graphics Namespace ViewerPreference Friend Class Program Shared Sub Main(ByVal args() As String) 'Create a pdf document. Dim doc As New PdfDocument() ' Create one page Dim page As PdfPageBase = doc.Pages.Add() 'Draw the page DrawPage(page) 'set view reference doc.ViewerPreferences.CenterWindow = True doc.ViewerPreferences.DisplayTitle = False doc.ViewerPreferences.FitWindow = False doc.ViewerPreferences.HideMenubar = True doc.ViewerPreferences.HideToolbar = True doc.ViewerPreferences.PageLayout = PdfPageLayout.SinglePage 'Save pdf file. doc.SaveToFile("ViewerPreference.pdf") doc.Close() 'Launching the Pdf file. Process.Start("ViewerPreference.pdf") End Sub Private Shared Sub DrawPage(ByVal page As PdfPageBase) Dim pageWidth As Single = page.Canvas.ClientSize.Width Dim y As Single = 0 'page header Dim pen1 As New PdfPen(Color.LightGray, 1.0F) Dim brush1 As PdfBrush = New PdfSolidBrush(Color.LightGray) Dim font1 As New PdfTrueTypeFont(New Font("Arial", 8.0F, FontStyle.Italic)) Dim format1 As New PdfStringFormat(PdfTextAlignment.Right) Dim text As String = "Demo of Spire.Pdf" page.Canvas.DrawString(text, font1, brush1, pageWidth, y, format1) Dim size As SizeF = font1.MeasureString(text, format1) y = y + size.Height + 1 page.Canvas.DrawLine(pen1, 0, y, pageWidth, y) 'title y = y + 5 Dim brush2 As PdfBrush = New PdfSolidBrush(Color.Black) Dim font2 As New PdfTrueTypeFont(New Font("Arial", 16.0F, FontStyle.Bold)) Dim format2 As New PdfStringFormat(PdfTextAlignment.Center) format2.CharacterSpacing = 1.0F text = "Summary of Science" page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2) size = font2.MeasureString(text, format2) y = y + size.Height + 6 'icon Dim image As PdfImage = PdfImage.FromFile("Wikipedia_Science.png") page.Canvas.DrawImage(image, New PointF(pageWidth - image.PhysicalDimension.Width, y)) Dim imageLeftSpace As Single = pageWidth - image.PhysicalDimension.Width - 2 Dim imageBottom As Single = image.PhysicalDimension.Height + y 'refenrence content Dim font3 As New PdfTrueTypeFont(New Font("Arial", 9.0F)) Dim format3 As New PdfStringFormat() format3.ParagraphIndent = font3.Size * 2 format3.MeasureTrailingSpaces = True format3.LineSpacing = font3.Size * 1.5F Dim text1 As String = "(All text and picture from " Dim text2 As String = "Wikipedia" Dim text3 As String = ", the free encyclopedia)" page.Canvas.DrawString(text1, font3, brush2, 0, y, format3) size = font3.MeasureString(text1, format3) Dim x1 As Single = size.Width format3.ParagraphIndent = 0 Dim font4 As New PdfTrueTypeFont(New Font("Arial", 9.0F, FontStyle.Underline)) Dim brush3 As PdfBrush = PdfBrushes.Blue page.Canvas.DrawString(text2, font4, brush3, x1, y, format3) size = font4.MeasureString(text2, format3) x1 = x1 + size.Width page.Canvas.DrawString(text3, font3, brush2, x1, y, format3) y = y + size.Height 'content Dim format4 As New PdfStringFormat() text = System.IO.File.ReadAllText("Summary_of_Science.txt") Dim font5 As New PdfTrueTypeFont(New Font("Arial", 10.0F)) format4.LineSpacing = font5.Size * 1.5F Dim textLayouter As New PdfStringLayouter() Dim imageLeftBlockHeight As Single = imageBottom - y Dim result As PdfStringLayoutResult _ = textLayouter.Layout(text, font5, format4, New SizeF(imageLeftSpace, imageLeftBlockHeight)) If result.ActualSize.Height < imageBottom - y Then imageLeftBlockHeight = imageLeftBlockHeight + result.LineHeight result = textLayouter.Layout(text, font5, format4, New SizeF(imageLeftSpace, imageLeftBlockHeight)) End If For Each line As LineInfo In result.Lines page.Canvas.DrawString(line.Text, font5, brush2, 0, y, format4) y = y + result.LineHeight Next line Dim textWidget As New PdfTextWidget(result.Remainder, font5, brush2) Dim textLayout As New PdfTextLayout() textLayout.Break = PdfLayoutBreakType.FitPage textLayout.Layout = PdfLayoutType.Paginate Dim bounds As New RectangleF(New PointF(0, y), page.Canvas.ClientSize) textWidget.StringFormat = format4 textWidget.Draw(page, bounds, textLayout) End Sub End Class End Namespace
Published in
Document Settings
Wednesday, 06 April 2011 09:37
PDF Properties in C#, VB.NET
The sample demonstrates how to set PDF document properties and PDF file information.
using System; using System.Drawing; using Spire.Pdf; using Spire.Pdf.Graphics; namespace Properties { class Program { static void Main(string[] args) { //Create a pdf document. PdfDocument doc = new PdfDocument(); // Create one page PdfPageBase page = doc.Pages.Add(); //Draw the page DrawPage(page); //set document info doc.DocumentInformation.Author = "Harry Hu"; doc.DocumentInformation.Creator = "Harry Hu"; doc.DocumentInformation.Keywords = "pdf, demo, document information"; doc.DocumentInformation.Producer = "Spire.Pdf"; doc.DocumentInformation.Subject = "Demo of Spire.Pdf"; doc.DocumentInformation.Title = "Document Information"; //file info doc.FileInfo.CrossReferenceType = PdfCrossReferenceType.CrossReferenceStream; doc.FileInfo.IncrementalUpdate = false; doc.FileInfo.Version = PdfVersion.Version1_5; //Save pdf file. doc.SaveToFile("Properties.pdf"); doc.Close(); //Launching the Pdf file. System.Diagnostics.Process.Start("Properties.pdf"); } private static void DrawPage(PdfPageBase page) { float pageWidth = page.Canvas.ClientSize.Width; float y = 0; //page header PdfPen pen1 = new PdfPen(Color.LightGray, 1f); PdfBrush brush1 = new PdfSolidBrush(Color.LightGray); PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Italic)); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Right); String text = "Demo of Spire.Pdf"; page.Canvas.DrawString(text, font1, brush1, pageWidth, y, format1); SizeF size = font1.MeasureString(text, format1); y = y + size.Height + 1; page.Canvas.DrawLine(pen1, 0, y, pageWidth, y); //title y = y + 5; PdfBrush brush2 = new PdfSolidBrush(Color.Black); PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format2 = new PdfStringFormat(PdfTextAlignment.Center); format2.CharacterSpacing = 1f; text = "Summary of Science"; page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2); size = font2.MeasureString(text, format2); y = y + size.Height + 6; //icon PdfImage image = PdfImage.FromFile(@"Wikipedia_Science.png"); page.Canvas.DrawImage(image, new PointF(pageWidth - image.PhysicalDimension.Width, y)); float imageLeftSpace = pageWidth - image.PhysicalDimension.Width - 2; float imageBottom = image.PhysicalDimension.Height + y; //refenrence content PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 9f)); PdfStringFormat format3 = new PdfStringFormat(); format3.ParagraphIndent = font3.Size * 2; format3.MeasureTrailingSpaces = true; format3.LineSpacing = font3.Size * 1.5f; String text1 = "(All text and picture from "; String text2 = "Wikipedia"; String text3 = ", the free encyclopedia)"; page.Canvas.DrawString(text1, font3, brush2, 0, y, format3); size = font3.MeasureString(text1, format3); float x1 = size.Width; format3.ParagraphIndent = 0; PdfTrueTypeFont font4 = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Underline)); PdfBrush brush3 = PdfBrushes.Blue; page.Canvas.DrawString(text2, font4, brush3, x1, y, format3); size = font4.MeasureString(text2, format3); x1 = x1 + size.Width; page.Canvas.DrawString(text3, font3, brush2, x1, y, format3); y = y + size.Height; //content PdfStringFormat format4 = new PdfStringFormat(); text = System.IO.File.ReadAllText(@"Summary_of_Science.txt"); PdfTrueTypeFont font5 = new PdfTrueTypeFont(new Font("Arial", 10f)); format4.LineSpacing = font5.Size * 1.5f; PdfStringLayouter textLayouter = new PdfStringLayouter(); float imageLeftBlockHeight = imageBottom - y; PdfStringLayoutResult result = textLayouter.Layout(text, font5, format4, new SizeF(imageLeftSpace, imageLeftBlockHeight)); if (result.ActualSize.Height < imageBottom - y) { imageLeftBlockHeight = imageLeftBlockHeight + result.LineHeight; result = textLayouter.Layout(text, font5, format4, new SizeF(imageLeftSpace, imageLeftBlockHeight)); } foreach (LineInfo line in result.Lines) { page.Canvas.DrawString(line.Text, font5, brush2, 0, y, format4); y = y + result.LineHeight; } PdfTextWidget textWidget = new PdfTextWidget(result.Remainder, font5, brush2); PdfTextLayout textLayout = new PdfTextLayout(); textLayout.Break = PdfLayoutBreakType.FitPage; textLayout.Layout = PdfLayoutType.Paginate; RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize); textWidget.StringFormat = format4; textWidget.Draw(page, bounds, textLayout); } } }
Imports System Imports System.Drawing Imports Spire.Pdf Imports Spire.Pdf.Graphics Namespace Properties Friend Class Program Shared Sub Main(ByVal args() As String) 'Create a pdf document. Dim doc As New PdfDocument() ' Create one page Dim page As PdfPageBase = doc.Pages.Add() 'Draw the page DrawPage(page) 'set document info doc.DocumentInformation.Author = "Harry Hu" doc.DocumentInformation.Creator = "Harry Hu" doc.DocumentInformation.Keywords = "pdf, demo, document information" doc.DocumentInformation.Producer = "Spire.Pdf" doc.DocumentInformation.Subject = "Demo of Spire.Pdf" doc.DocumentInformation.Title = "Document Information" 'file info doc.FileInfo.CrossReferenceType = PdfCrossReferenceType.CrossReferenceStream doc.FileInfo.IncrementalUpdate = False doc.FileInfo.Version = PdfVersion.Version1_5 'Save pdf file. doc.SaveToFile("Properties.pdf") doc.Close() 'Launching the Pdf file. Process.Start("Properties.pdf") End Sub Private Shared Sub DrawPage(ByVal page As PdfPageBase) Dim pageWidth As Single = page.Canvas.ClientSize.Width Dim y As Single = 0 'page header Dim pen1 As New PdfPen(Color.LightGray, 1.0F) Dim brush1 As PdfBrush = New PdfSolidBrush(Color.LightGray) Dim font1 As New PdfTrueTypeFont(New Font("Arial", 8.0F, FontStyle.Italic)) Dim format1 As New PdfStringFormat(PdfTextAlignment.Right) Dim text As String = "Demo of Spire.Pdf" page.Canvas.DrawString(text, font1, brush1, pageWidth, y, format1) Dim size As SizeF = font1.MeasureString(text, format1) y = y + size.Height + 1 page.Canvas.DrawLine(pen1, 0, y, pageWidth, y) 'title y = y + 5 Dim brush2 As PdfBrush = New PdfSolidBrush(Color.Black) Dim font2 As New PdfTrueTypeFont(New Font("Arial", 16.0F, FontStyle.Bold)) Dim format2 As New PdfStringFormat(PdfTextAlignment.Center) format2.CharacterSpacing = 1.0F text = "Summary of Science" page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2) size = font2.MeasureString(text, format2) y = y + size.Height + 6 'icon Dim image As PdfImage = PdfImage.FromFile("Wikipedia_Science.png") page.Canvas.DrawImage(image, New PointF(pageWidth - image.PhysicalDimension.Width, y)) Dim imageLeftSpace As Single = pageWidth - image.PhysicalDimension.Width - 2 Dim imageBottom As Single = image.PhysicalDimension.Height + y 'refenrence content Dim font3 As New PdfTrueTypeFont(New Font("Arial", 9.0F)) Dim format3 As New PdfStringFormat() format3.ParagraphIndent = font3.Size * 2 format3.MeasureTrailingSpaces = True format3.LineSpacing = font3.Size * 1.5F Dim text1 As String = "(All text and picture from " Dim text2 As String = "Wikipedia" Dim text3 As String = ", the free encyclopedia)" page.Canvas.DrawString(text1, font3, brush2, 0, y, format3) size = font3.MeasureString(text1, format3) Dim x1 As Single = size.Width format3.ParagraphIndent = 0 Dim font4 As New PdfTrueTypeFont(New Font("Arial", 9.0F, FontStyle.Underline)) Dim brush3 As PdfBrush = PdfBrushes.Blue page.Canvas.DrawString(text2, font4, brush3, x1, y, format3) size = font4.MeasureString(text2, format3) x1 = x1 + size.Width page.Canvas.DrawString(text3, font3, brush2, x1, y, format3) y = y + size.Height 'content Dim format4 As New PdfStringFormat() text = System.IO.File.ReadAllText("Summary_of_Science.txt") Dim font5 As New PdfTrueTypeFont(New Font("Arial", 10.0F)) format4.LineSpacing = font5.Size * 1.5F Dim textLayouter As New PdfStringLayouter() Dim imageLeftBlockHeight As Single = imageBottom - y Dim result As PdfStringLayoutResult _ = textLayouter.Layout(text, font5, format4, New SizeF(imageLeftSpace, imageLeftBlockHeight)) If result.ActualSize.Height < imageBottom - y Then imageLeftBlockHeight = imageLeftBlockHeight + result.LineHeight result = textLayouter.Layout(text, font5, format4, New SizeF(imageLeftSpace, imageLeftBlockHeight)) End If For Each line As LineInfo In result.Lines page.Canvas.DrawString(line.Text, font5, brush2, 0, y, format4) y = y + result.LineHeight Next line Dim textWidget As New PdfTextWidget(result.Remainder, font5, brush2) Dim textLayout As New PdfTextLayout() textLayout.Break = PdfLayoutBreakType.FitPage textLayout.Layout = PdfLayoutType.Paginate Dim bounds As New RectangleF(New PointF(0, y), page.Canvas.ClientSize) textWidget.StringFormat = format4 textWidget.Draw(page, bounds, textLayout) End Sub End Class End Namespace
Published in
Document Settings
Wednesday, 06 April 2011 09:33
PDF FromHTML in C#, VB.NET
using System; using Spire.Pdf; namespace FromHTML { class Program { [STAThread] static void Main(string[] args) { //Create a pdf document. PdfDocument doc = new PdfDocument(); String url = "http://www.e-iceblue.com/"; doc.LoadFromHTML(url, false, true, true); //Save pdf file. doc.SaveToFile("FromHTML.pdf"); doc.Close(); //Launching the Pdf file. System.Diagnostics.Process.Start("FromHTML.pdf"); } } }
Imports Spire.Pdf Namespace FromHTML Friend Class Program _ Shared Sub Main(ByVal args() As String) 'Create a pdf document. Dim doc As New PdfDocument() Dim url As String = "http://www.e-iceblue.com/" doc.LoadFromHTML(url, False, True, True) 'Save pdf file. doc.SaveToFile("FromHTML.pdf") doc.Close() 'Launching the Pdf file. Process.Start("FromHTML.pdf") End Sub End Class End Namespace
Published in
Conversion
Wednesday, 06 April 2011 09:30
PDF DigitalCertificate in C#, VB.NET
The sample demonstrates how to sign PDF document with digital certificate.
using System; using System.Drawing; using Spire.Pdf; using Spire.Pdf.Graphics; using Spire.Pdf.Security; namespace DigitalSignature { class Program { static void Main(string[] args) { //Create a pdf document. PdfNewDocument doc = new PdfNewDocument(); // Create one page PdfPageBase page = doc.Pages.Add(); //Draw the page DrawPage(page); String pfxPath = @"Demo.pfx"; PdfCertificate cert = new PdfCertificate(pfxPath, "e-iceblue"); PdfSignature signature = new PdfSignature(doc, page, cert, "demo"); signature.ContactInfo = "Harry Hu"; signature.Certificated = true; signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill; //Save pdf file. doc.Save("DigitalSignature.pdf"); doc.Close(); //Launching the Pdf file. System.Diagnostics.Process.Start("DigitalSignature.pdf"); } private static void DrawPage(PdfPageBase page) { float pageWidth = page.Canvas.ClientSize.Width; float y = 0; //page header PdfPen pen1 = new PdfPen(Color.LightGray, 1f); PdfBrush brush1 = new PdfSolidBrush(Color.LightGray); PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Italic)); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Right); String text = "Demo of Spire.Pdf"; page.Canvas.DrawString(text, font1, brush1, pageWidth, y, format1); SizeF size = font1.MeasureString(text, format1); y = y + size.Height + 1; page.Canvas.DrawLine(pen1, 0, y, pageWidth, y); //title y = y + 5; PdfBrush brush2 = new PdfSolidBrush(Color.Black); PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format2 = new PdfStringFormat(PdfTextAlignment.Center); format2.CharacterSpacing = 1f; text = "Summary of Science"; page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2); size = font2.MeasureString(text, format2); y = y + size.Height + 6; //icon PdfImage image = PdfImage.FromFile(@"Wikipedia_Science.png"); page.Canvas.DrawImage(image, new PointF(pageWidth - image.PhysicalDimension.Width, y)); float imageLeftSpace = pageWidth - image.PhysicalDimension.Width - 2; float imageBottom = image.PhysicalDimension.Height + y; //refenrence content PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 9f)); PdfStringFormat format3 = new PdfStringFormat(); format3.ParagraphIndent = font3.Size * 2; format3.MeasureTrailingSpaces = true; format3.LineSpacing = font3.Size * 1.5f; String text1 = "(All text and picture from "; String text2 = "Wikipedia"; String text3 = ", the free encyclopedia)"; page.Canvas.DrawString(text1, font3, brush2, 0, y, format3); size = font3.MeasureString(text1, format3); float x1 = size.Width; format3.ParagraphIndent = 0; PdfTrueTypeFont font4 = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Underline)); PdfBrush brush3 = PdfBrushes.Blue; page.Canvas.DrawString(text2, font4, brush3, x1, y, format3); size = font4.MeasureString(text2, format3); x1 = x1 + size.Width; page.Canvas.DrawString(text3, font3, brush2, x1, y, format3); y = y + size.Height; //content PdfStringFormat format4 = new PdfStringFormat(); text = System.IO.File.ReadAllText(@"Summary_of_Science.txt"); PdfTrueTypeFont font5 = new PdfTrueTypeFont(new Font("Arial", 10f)); format4.LineSpacing = font5.Size * 1.5f; PdfStringLayouter textLayouter = new PdfStringLayouter(); float imageLeftBlockHeight = imageBottom - y; PdfStringLayoutResult result = textLayouter.Layout(text, font5, format4, new SizeF(imageLeftSpace, imageLeftBlockHeight)); if (result.ActualSize.Height < imageBottom - y) { imageLeftBlockHeight = imageLeftBlockHeight + result.LineHeight; result = textLayouter.Layout(text, font5, format4, new SizeF(imageLeftSpace, imageLeftBlockHeight)); } foreach (LineInfo line in result.Lines) { page.Canvas.DrawString(line.Text, font5, brush2, 0, y, format4); y = y + result.LineHeight; } PdfTextWidget textWidget = new PdfTextWidget(result.Remainder, font5, brush2); PdfTextLayout textLayout = new PdfTextLayout(); textLayout.Break = PdfLayoutBreakType.FitPage; textLayout.Layout = PdfLayoutType.Paginate; RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize); textWidget.StringFormat = format4; textWidget.Draw(page, bounds, textLayout); } } }
Imports System.Drawing Imports Spire.Pdf Imports Spire.Pdf.Graphics Imports Spire.Pdf.Security Namespace DigitalSignature Friend Class Program Shared Sub Main(ByVal args() As String) 'Create a pdf document. Dim doc As New PdfNewDocument() ' Create one page Dim page As PdfPageBase = doc.Pages.Add() 'Draw the page DrawPage(page) Dim pfxPath As String = "Demo.pfx" Dim cert As New PdfCertificate(pfxPath, "e-iceblue") Dim signature As New PdfSignature(doc, page, cert, "demo") signature.ContactInfo = "Harry Hu" signature.Certificated = True signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill 'Save pdf file. doc.Save("DigitalSignature.pdf") doc.Close() 'Launching the Pdf file. Process.Start("DigitalSignature.pdf") End Sub Private Shared Sub DrawPage(ByVal page As PdfPageBase) Dim pageWidth As Single = page.Canvas.ClientSize.Width Dim y As Single = 0 'page header Dim pen1 As New PdfPen(Color.LightGray, 1.0F) Dim brush1 As PdfBrush = New PdfSolidBrush(Color.LightGray) Dim font1 As New PdfTrueTypeFont(New Font("Arial", 8.0F, FontStyle.Italic)) Dim format1 As New PdfStringFormat(PdfTextAlignment.Right) Dim text As String = "Demo of Spire.Pdf" page.Canvas.DrawString(text, font1, brush1, pageWidth, y, format1) Dim size As SizeF = font1.MeasureString(text, format1) y = y + size.Height + 1 page.Canvas.DrawLine(pen1, 0, y, pageWidth, y) 'title y = y + 5 Dim brush2 As PdfBrush = New PdfSolidBrush(Color.Black) Dim font2 As New PdfTrueTypeFont(New Font("Arial", 16.0F, FontStyle.Bold)) Dim format2 As New PdfStringFormat(PdfTextAlignment.Center) format2.CharacterSpacing = 1.0F text = "Summary of Science" page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2) size = font2.MeasureString(text, format2) y = y + size.Height + 6 'icon Dim image As PdfImage = PdfImage.FromFile("Wikipedia_Science.png") page.Canvas.DrawImage(image, New PointF(pageWidth - image.PhysicalDimension.Width, y)) Dim imageLeftSpace As Single = pageWidth - image.PhysicalDimension.Width - 2 Dim imageBottom As Single = image.PhysicalDimension.Height + y 'refenrence content Dim font3 As New PdfTrueTypeFont(New Font("Arial", 9.0F)) Dim format3 As New PdfStringFormat() format3.ParagraphIndent = font3.Size * 2 format3.MeasureTrailingSpaces = True format3.LineSpacing = font3.Size * 1.5F Dim text1 As String = "(All text and picture from " Dim text2 As String = "Wikipedia" Dim text3 As String = ", the free encyclopedia)" page.Canvas.DrawString(text1, font3, brush2, 0, y, format3) size = font3.MeasureString(text1, format3) Dim x1 As Single = size.Width format3.ParagraphIndent = 0 Dim font4 As New PdfTrueTypeFont(New Font("Arial", 9.0F, FontStyle.Underline)) Dim brush3 As PdfBrush = PdfBrushes.Blue page.Canvas.DrawString(text2, font4, brush3, x1, y, format3) size = font4.MeasureString(text2, format3) x1 = x1 + size.Width page.Canvas.DrawString(text3, font3, brush2, x1, y, format3) y = y + size.Height 'content Dim format4 As New PdfStringFormat() text = System.IO.File.ReadAllText("Summary_of_Science.txt") Dim font5 As New PdfTrueTypeFont(New Font("Arial", 10.0F)) format4.LineSpacing = font5.Size * 1.5F Dim textLayouter As New PdfStringLayouter() Dim imageLeftBlockHeight As Single = imageBottom - y Dim result As PdfStringLayoutResult _ = textLayouter.Layout(text, font5, format4, New SizeF(imageLeftSpace, imageLeftBlockHeight)) If result.ActualSize.Height < imageBottom - y Then imageLeftBlockHeight = imageLeftBlockHeight + result.LineHeight result = textLayouter.Layout(text, font5, format4, New SizeF(imageLeftSpace, imageLeftBlockHeight)) End If For Each line As LineInfo In result.Lines page.Canvas.DrawString(line.Text, font5, brush2, 0, y, format4) y = y + result.LineHeight Next line Dim textWidget As New PdfTextWidget(result.Remainder, font5, brush2) Dim textLayout As New PdfTextLayout() textLayout.Break = PdfLayoutBreakType.FitPage textLayout.Layout = PdfLayoutType.Paginate Dim bounds As New RectangleF(New PointF(0, y), page.Canvas.ClientSize) textWidget.StringFormat = format4 textWidget.Draw(page, bounds, textLayout) End Sub End Class End Namespace
Published in
Security