17.05.2014 Views

PDFlib 8 Windows COM/.NET Tutorial

PDFlib 8 Windows COM/.NET Tutorial

PDFlib 8 Windows COM/.NET Tutorial

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using <strong>PDFlib</strong> with VB.<strong>NET</strong>. In order to use <strong>PDFlib</strong>.<strong>NET</strong> in your VB.<strong>NET</strong> project you must<br />

create a reference to the <strong>PDFlib</strong>.<strong>NET</strong> assembly as follows in Visual Basic .<strong>NET</strong>: Project, Add<br />

Reference..., Browse..., and select pdflib_dotnet.dll from the installation directory.<br />

With the command line compiler you can reference <strong>PDFlib</strong>.<strong>NET</strong> as in the following<br />

example:<br />

vbc.exe /r:..\..\bin\pdflib_dotnet.dll hello.vb<br />

Visual Basic .<strong>NET</strong> supports two different kinds of exception handling:<br />

> modern structured exception handling (which is also used in other modern languages,<br />

such as C#)<br />

> traditional unstructured exception handling (which has been the only way of exception<br />

handling in Visual Basic 6.0)<br />

Client code can handle .<strong>NET</strong> exceptions thrown by <strong>PDFlib</strong> with both kinds of exception<br />

handling, but the syntax varies. Catching Exceptions with structured exception handling<br />

is the recommended way. It works with a try...catch clause:<br />

Try<br />

...some <strong>PDFlib</strong> instructions...<br />

Catch e As <strong>PDFlib</strong>Exception<br />

Console.WriteLine("<strong>PDFlib</strong> exception occurred in hello sample:")<br />

Console.WriteLine("[{0}] {1}: {2}",<br />

e.get_errnum(), e.get_apiname(), e.get_errmsg())<br />

Finally<br />

If Not p Is Nothing Then<br />

p.Dispose()<br />

End If<br />

End Try<br />

Catching exceptions with the traditional unstructured exception handling is achieved<br />

with an On Error GoTo clause:<br />

Imports Microsoft.VisualBasic<br />

Public Shared Sub Main()<br />

On Error GoTo ErrExit<br />

...some <strong>PDFlib</strong> instructions...<br />

Exit Sub<br />

ErrExit:<br />

Console.WriteLine("<strong>PDFlib</strong> exception caught: {0}", Err.Description)<br />

End Sub<br />

Using <strong>PDFlib</strong> with C++ and CLI. .<strong>NET</strong> applications written in C++ (based on the<br />

Common Language Infrastructure CLI) can directly access the <strong>PDFlib</strong>.<strong>NET</strong> DLL without using<br />

the <strong>PDFlib</strong> C++ binding. The source code must reference <strong>PDFlib</strong> as follows:<br />

using namespace <strong>PDFlib</strong>_dotnet;<br />

Unicode and legacy encoding conversion. For the convenience of <strong>PDFlib</strong> users we<br />

show a useful C# string conversion method. Please refer to the .<strong>NET</strong> documentation for<br />

more details. The following constructor creates a Unicode string from a byte array (at<br />

the specified offset and length), using the encoding supplied in the Encoding parameter:<br />

public String(sbyte*, int, int, Encoding)<br />

38 Chapter 2: <strong>PDFlib</strong> Language Bindings (Edition for <strong>COM</strong>, .<strong>NET</strong>, and REALbasic)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!