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.

3 Creating PDF Documents<br />

3.1 General <strong>PDFlib</strong> Programming Aspects<br />

Cookbook Code samples regarding general programming issues can be found in the general category of<br />

the <strong>PDFlib</strong> Cookbook.<br />

3.1.1 Exception Handling<br />

Errors of a certain kind are called exceptions in many languages for good reasons – they<br />

are mere exceptions, and are not expected to occur very often during the lifetime of a<br />

program. The general strategy is to use conventional error reporting mechanisms (i.e.<br />

special error return codes such as -1) for function calls which may often fail, and use a<br />

special exception mechanism for those rare occasions which don’t warrant cluttering<br />

the code with conditionals. This is exactly the path that <strong>PDFlib</strong> goes: Some operations<br />

can be expected to go wrong rather frequently, for example:<br />

> Trying to open an output file for which one doesn’t have permission<br />

> Trying to open an input PDF with a wrong file name<br />

> Trying to open a corrupt image file<br />

<strong>PDFlib</strong> signals such errors by returning a special value (usually – 1) as documented in<br />

the <strong>PDFlib</strong> API Reference. This error code must be checked by the application developer<br />

for all functions which are documented to return -1 on error.<br />

Other events may be considered harmful, but will occur rather infrequently, e.g.<br />

> running out of virtual memory<br />

> scope violations (e.g., closing a document before opening it)<br />

> supplying wrong parameters to <strong>PDFlib</strong> API functions (e.g., trying to draw a circle with<br />

negative radius), or supplying wrong options.<br />

When <strong>PDFlib</strong> detects such a situation, an exception will be thrown instead of passing a<br />

special error return value to the caller. It is important to understand that the generated<br />

PDF document cannot be finished when an exception occurred. The only methods<br />

which can safely be called after an exception are get_apiname( ), get_errnum( ), and get_<br />

errmsg( ). Calling any other <strong>PDFlib</strong> method after an exception may lead to unexpected<br />

results. The exception will contain the following information:<br />

> A unique error number;<br />

> The name of the <strong>PDFlib</strong> API function which caused the exception;<br />

> A descriptive text containing details of the problem.<br />

Querying the reason of a failed function call. As noted above, the generated PDF output<br />

document must always be abandoned when an exception occurs. Some clients,<br />

however, may prefer to continue the document by adjusting the program flow or supplying<br />

different data. For example, when a particular font cannot be loaded most clients<br />

will give up the document, while others may prefer to work with a different font. In this<br />

case it may be desirable to retrieve an error message which describes the problem in<br />

more detail. In this situation the functions get_errnum( ), get_errmsg( ), and get_<br />

apiname( ) can be called immediately after a failed function call, i.e., a function call<br />

which returned a -1 error value.<br />

3.1 General <strong>PDFlib</strong> Programming Aspects 59

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

Saved successfully!

Ooh no, something went wrong!