Converts a Text, RTF file into array of PDF bytes.
Namespace:
SautinSoftAssembly: PdfMetamorphosis (in PdfMetamorphosis.dll) Version: 5.3.4.18
Syntax
| C# | Visual Basic |
Parameters
- inputFileName
- String
Local path to input Text or RTF file
Return Value
PDF document as byte array, or null in case of converting failed
Examples
using System; using System.IO; using System.Collections; namespace Sample { class Test { static void Main(string[] args) { SautinSoft.PdfMetamorphosis p = new SautinSoft.PdfMetamorphosis(); //this property is necessary only for registered version //p.Serial = "XXXXXXXXXXX"; //specify some options p.PageStyle.PageOrientation.Landscape(); //specify header in HTML format p.Header.Html("<b>Sample header in HTML format</b>"); //specify footer in RTF format p.Footer.Rtf("{\b Bold footer}"); //specify page numbers p.PageStyle.PageNumFormat = "Page {page} of {numpages}"; if (p != null) { string rtfPath = @"..\..\..\..\..\test.rtf"; string pdfPath = @"..\..\..\..\..\test.pdf"; string rtfString = ""; //1. Get RTF content from file ReadFromFile(rtfPath,ref rtfString); //2. Converting RTF to PDF byte[] pdf = p.RtfToPdfConvertByte(rtfString); if (pdf != null) { //3. Save to PDF file FileStream fs = new FileStream(pdfPath, FileMode.Create, FileAccess.Write); fs.Write(pdf, 0, pdf.Length); fs.Close(); System.Diagnostics.Process.Start(pdfPath); } else { System.Console.WriteLine("An error occured during converting HTML to PDF!"); } } } public static int ReadFromFile(string fileName,ref string fileStr) { try { FileInfo fi = new FileInfo(fileName); StreamReader strmRead = fi.OpenText(); fileStr = strmRead.ReadToEnd(); strmRead.Close(); return 0; } catch { //error open file System.Console.WriteLine("Error in open file"); return 1; } } } }
Imports System Imports System.IO Imports System.Text Module sample Sub Main() Dim p As New SautinSoft.PdfMetamorphosis() 'this property is necessary only for registered version 'p.Serial = "XXXXXXXXXXX"; 'specify some options p.PageStyle.PageOrientation.Landscape() 'specify header in HTML format p.Header.Html("<b>Sample header in HTML format</b>") 'specify footer in RTF format p.Footer.Rtf("{\b Bold footer}") 'specify page numbers p.PageStyle.PageNumFormat = "Page {page} of {numpages}" If Not p Is Nothing Then Dim rtfPath As String = "..\..\..\..\..\test.rtf" Dim pdfPath As String = "..\..\..\..\..\test.pdf" Dim rtfString As String = "" '1. Get RTF content from file ReadFromFile(rtfPath,rtfString) '2. Converting RTF to PDF Dim pdf() As Byte = p.RtfToPdfConvertByte(rtfString) If Not pdf Is Nothing Then '3. Save to PDF file Dim fs As New FileStream(pdfPath, FileMode.Create, FileAccess.Write) fs.Write(pdf, 0, pdf.Length) fs.Close() System.Diagnostics.Process.Start(pdfPath) Else System.Console.WriteLine("An error occured during converting HTML to PDF!") End If End If End Sub Public Function ReadFromFile(ByVal fileName As String, ByRef fileStr As String) As Integer Try Dim fi As New FileInfo(fileName) Dim strmRead As StreamReader = fi.OpenText() fileStr = strmRead.ReadToEnd() strmRead.Close() Return 0 Catch 'error open file System.Console.WriteLine("Error in open file") Return 1 End Try End Function End Module