-
Notifications
You must be signed in to change notification settings - Fork 2
Minimal example
Let's start with the classic example (see hello):
import std.stdio;
import harud;
import harud.c; // for getErrorDescription
void main() {
void errorCallback(uint error_number, uint detail_number) {
writefln("err %x, %s, (num %x)"
, error_number
, getErrorDescription(error_number),
detail_number);
}
Doc pdf = new Doc(&errorCallback);
Page page = pdf.addPage(); /// Add a new page to the document
Font helvetica = pdf.getFont("Helvetica");
page.setFontAndSize(helvetica, 60); /// Set the current font and size for the page
page.beginText(); /// Begin text mode
page.showText("Hello World"); /// Print text to the page
page.endText(); /// End text mode
pdf.saveToFile("./hello.pdf"); /// Write to disk
}
import harud;
import harud.c; // for getErrorDescription
It creates a new document object and set a user-defined error function ("errorCallback" here):
Doc pdf = new Doc(&errorCallback);
When somethings go wrong errorCallback
function it's called.
We can also set compression, encryption, page mode, and password if necessary.
// set compression mode
pdf.compressionMode = CompressionMode.all;
// set page mode to use outlines.
pdf.pageMode = PageMode.useOutline;
// set password
pdf.setPassword("owner", "user");
Call method addPage()
to add a page to the document. The Page
object returned is used in later operations on the page.
Page page = pdf.addPage();
To insert a new page before an existing page, call insertPage()
. For example, to insert "page0" before "page1":
Page page0 = pdf.insertPage(page);
If necessary set other attributes of the page object:
page.setSize(PageSize.B5, PageDirection.landscape);
Before inserting text, you must set the correct graphic mode
page.beginText(); /// Begin text mode
page.showText("Hello World"); /// Print text to the page
page.endText(); /// End text mode
saveToFile()
saves a document to a file.
pdf.saveToFile("./test.pdf");
In a more D-way we can use a try-catch
block to handle exceptions:
import std.stdio;
import harud;
void main() {
Doc pdf = new Doc();
Page page0 = pdf.addPage(); /// Add a new page to the document
try {
page0.width = 1; // <- ERROR: page width cannot be 1!
} catch(HarudException e) {
writeln("exp:", e.msg, " num:", e.errCode);
}
}
When somethings go wrong an HarudException
was thown and it will print:
exp:Page invalid size num:4180