.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()
doc.Sections.Add(sec)

Dim txt As Text = New Text("A curve")
sec.Paragraphs.Add(txt)

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)
sec.Paragraphs.Add(gph)

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)
gph.Shapes.Add(crv)

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 at 1:16 PM


comments powered by Disqus

Featured

  • Visual Studio Code Dev Team Cleans Up

    The Visual Studio Code development team focused on some housekeeping in the October update, closing more than 4,000 issues on GitHub, where the cross-platform, open-source editor lives.

  • ML.NET Model Builder Update Boosts Image Classification

    Microsoft announced an update to the Model Builder component of its ML.NET machine learning framework, boosting image classification and adding "try your model" functionality for predictions with sample input.

  • How to Do Naive Bayes with Numeric Data Using C#

    Dr. James McCaffrey of Microsoft Research uses a full code sample and screenshots to demonstrate how to create a naive Bayes classification system when the predictor values are numeric, using the C# language without any special code libraries.

  • Vortex

    Open Source 'Infrastructure-as-Code' SDK Adds .NET Core Support for Working with Azure

    Pulumi, known for its "Infrastructure-as-Code" cloud development tooling, has added support for .NET Core, letting .NET-centric developers use C#, F# and VB.NET to create, deploy, and manage Azure infrastructure.

  • .NET Framework Not Forgotten: Repair Tool Updated

    Even though Microsoft's development focus has shifted to the open-source, cross-platform .NET Core initiative -- with the aging, traditional, Windows-only .NET Framework relegated primarily to fixes and maintenance such as quality and reliability improvements -- the latter is still getting some other attention, as exemplified in a repair tool update.

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events