softwareengineering

I have a class responsible for generating a PDF. The class takes an existing PDF template and builds multiple pages. Each page has various elements that are inserted/manipulated.

I now need to handle historic templates, wherein the position differs among the templates.

What’s a good, but not overly complex way to handle this?

Instead of going into (further) conditional hell, I’m thinking of using a class per template and ultimately choosing the class based on a date, yet I hate the idea of having classes with names such as PdfDraw_01012022 and having to conditional select them.

I am stuck with VB.NET under .NET 4.8.

Is there a better approach to this, or a language feature wherein I can do something like PDF.Draw("01/01/2022") and automatically have the correct class handle the generation? Or am I simply overthinking this?

While immensely more complex, the crux of the current approach looks like this

Public Class PdfGen
    
    Private Function BuildPdf() As Document
        Dim newDoc As New Document
        newDoc.Pages.Add(BuildPage1)
        Return newDoc
    End Function
    
    Private Function BuildPage1() As Page
        Dim template = GetTemplate(MyCriteria.Date)
        Dim page1 As Page = template.Pages(0)

        Dim X = 10
        Dim Y = 10
        Dim WIDTH = 500
        Dim HEIGHT = 200

        page1.Elements.Add(New TextArea("My Data", X, Y, WIDTH, HEIGHT))
        Return page1
    End Function

End Class

7

LEAVE A COMMENT