.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


comments powered by Disqus

Featured

  • Full Stack Hands-On Development with .NET

    In the fast-paced realm of modern software development, proficiency across a full stack of technologies is not just beneficial, it's essential. Microsoft has an entire stack of open source development components in its .NET platform (formerly known as .NET Core) that can be used to build an end-to-end set of applications.

  • .NET-Centric Uno Platform Debuts 'Single Project' for 9 Targets

    "We've reduced the complexity of project files and eliminated the need for explicit NuGet package references, separate project libraries, or 'shared' projects."

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

Subscribe on YouTube