.NET Tips and Tricks

Blog archive

The Beauty of Consistency with Aspose.PDF

One of the things that I like about object models is consistency. If I learn one object, I hope that I can leverage that knowledge when working with other objects. I also like it if the object model that I'm working with is compatible with the .NET object model. Aspose.PDF, which I reviewed in the September issue of Visual Studio Magazine is like that.

For instance, with Aspose.PDF if I want to create a document with some text, I first create a Pdf object, create a Section and add the Section to the Pdf's Sections collection. I then create a Text object and add the Text to the Section's Paragraphs collection (I can add a Text object to the Paragraphs collection because Text inherits from Paragraph). Right at the start you can see the consistency between how a Section is added to the Pdf object and the Text is added to the Section.

The code looks like this (and you can see the repetition):

Dim doc As Pdf = New Pdf()

Dim sec As New Section()

Dim txt As Text = New Text("A curve")

This is exactly the way that I expect objects-with-collections to work, so I'm a happy camper.

While I like collections, I also appreciate shortcuts. Aspose, for instance, allows me to create a Section and add it to the Pdf object by using an inconsistent version of the Add method that accepts no parameters:

Dim sec As Section = doc.Sections.Add()

I'm comfortable with using this shortcut because I believe that a developer reading this code would understand what the code was doing.

There's a second shortcut that I can use to create a Section and add it to the Pdf object. I can pass a Pdf object to the Section as I create it:

Dim sec As New Section(doc)

I'm less comfortable with this shortcut because I don't think it's obvious what's going on here. In fact, a developer with knowledge of the Decorator pattern might assume that's what's happening here (and it's not, by the way).

But these are all shortcuts and I'm free to ignore them to stick with the standard objects-with-collections coding.

Now what happens when I create a document with a graphic? The pattern is the same but I create a Graph object and add it to the Section's Paragraphs collection (a Graph inherits from Paragraph, just like a Text object does). So that code looks like this:

Dim gph As Graph = New Graph(sec1, 100, 400)

I can build up a Graph by creating Shapes and adding them to the Graph's Shapes collection in a way very similar to the way I added Texts and Graphs to Sections. This example creates a Curve (using an array of positions) and adds it to the Graph object:

Dim pos () As Single = New Single() {0, 0, 200, 80, 300, 40, 350, 90}
Dim crv As Curve = New Curve(graph1, posArr)

However, I have to admit, the number of parameters required to create a curve makes creating a graphic out of shapes more than a little intimidating. Ignoring that though, I appreciate the care that went into the Aspose.PDF object model.

Posted by Peter Vogel on 10/15/2010

comments powered by Disqus


Subscribe on YouTube