Reputation: 815
I have a function FillPdf()
which I am using to fill an editable pdf. For now I have defined its parameter type but I have three different forms each having different model so I want is my this FillPdf()
receives the respective model and use its data.How to achieve this.
FillPdf.cs
public static bool FillPdf(M2HDetail m2HUserDetails)
{
try
{
if (PdfFileSettingViewModel.M2HAuthorizationFormLocation == null)
return false;
var pdfReader = new PdfReader(PdfFileSettingViewModel.M2HAuthorizationFormLocation);
var sb = new StringBuilder();
foreach (DictionaryEntry de in pdfReader.AcroFields.Fields)
{
sb.Append(de.Key.ToString() + Environment.NewLine);
}
var newFile = Path.Combine(PdfFileSettingViewModel.TempFolderLocation, string.Format("{0}_{1}.pdf", "M2HForm",SessionItems.UserId));
System.IO.File.Copy(PdfFileSettingViewModel.M2HAuthorizationFormLocation, newFile);
var pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
var pdfFormFields = pdfStamper.AcroFields;
var totalFields = pdfFormFields.Fields.Count;
var myParams = new List<string>();
#region # SetPdfFields #
pdfFormFields.SetField("Text1", m2HUserDetails.LastName);
pdfFormFields.SetField("Text2", m2HUserDetails.FirstName);
pdfStamper.FormFlattening = false;
#endregion
pdfStamper.Close();
return true;
}
catch (Exception ex)
{
var files = System.IO.Directory.GetFiles(PdfFileSettingViewModel.TempFolderLocation);
foreach (var file in files)
{
System.IO.File.Delete(file);
}
return false;
}
}
Caller function
public ActionResult AuthorizationForm(M2HDetail model)
{
var isFillPdfSuccess = PdfFiller.FillPdf(model);
return View();
}
Now I want that I can use the same pdf method for my other form like this:
public ActionResult AuthorizationFormHippa(HippaAuthForm model)
{
var isFillPdfSuccess = PdfFiller.FillPdf(model);
return View();
}
Also I want that my pdf method could have parameter as model like this:
public static bool FillPdf(ModelForm model)
{
}
Please suggest how to achieve this.As I can have more than 3 forms in future and each time I have to write the same method just with different parameter type.
Upvotes: 0
Views: 1135
Reputation: 8183
Personally I would create a generic interface
public interface IPdfContent
{
// Add your common properties here
}
Then get all of your models to implement this interface
public class HippaAuthForm : IPdfContent
{
}
Now for your PdfFill
class you can do this:
public static bool FillPdf(IPdfContent m2HUserDetails)
{
...
}
Because FillPdf
is now expecting the Interface
you can now pass it anyone who implements that Interface
. This now allows you to create new models without having to do any work.
Just implement the interface!
This also will allow you to Mock out your models and you can unit test a bit easier since you will now have DI in place.
Upvotes: 2
Reputation: 5137
I think your title has a clue on the solution to your problem.
All you gotta do is,
PdfDetailsModel
. This model class should have information that you must have in FillPdf()
method. PdfDetailsModel
. This specific class will contain additional information required to serve the request. Let us call it PdfDetailsHippaAuthModel
.PdfDetailsHippaAuthModel
to FillPdf
method that accepts base class object.FillPdf()
depends on the base class, it is happy with any derived instance.Upvotes: -1