5
\$\begingroup\$

To create a pdfDocument I am using following code which works as expected but do not want to use multiple if else.

Any patterns or any design strategies? It may be a over do for the example , but will be useful.

string fileExtension = Path.GetExtension(fileName).ToLower();
string outPutPath = Path.GetDirectoryName(fileName) + "\\" + Path.GetFileNameWithoutExtension(fileName) + ".pdf";
Aspose.Pdf.Generator.Pdf pdfConverter = new Aspose.Pdf.Generator.Pdf();
Section pdfSection = pdfConverter.Sections.Add();
//Table pdfTable = new Table() { DefaultCellBorder=new Aspose.Pdf.Generator.BorderInfo((int)BorderSide.All,0.1F)};
//pdfSection.Paragraphs.Add(pdfTable);
Image sourceImage = new Image();
sourceImage.ImageInfo.File = fileName;
if (fileExtension == ".jpg" || fileExtension == ".jpeg")
{
 sourceImage.ImageInfo.ImageFileType = ImageFileType.Jpeg;
}
else if (fileExtension == ".bmp")
{
 sourceImage.ImageInfo.ImageFileType = ImageFileType.Bmp;
}
else if (fileExtension == ".gif")
{
 sourceImage.ImageInfo.ImageFileType = ImageFileType.Gif;
}
else if (fileExtension == ".png")
{
 sourceImage.ImageInfo.ImageFileType = ImageFileType.Png;
}
else if (fileExtension == ".tiff")
{
 sourceImage.ImageInfo.ImageFileType = ImageFileType.Tiff;
}
pdfSection.Paragraphs.Add(sourceImage);
pdfConverter.Save(outPutPath);
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Dec 22, 2016 at 7:30
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I'm curious as to why you want to avoid an if else. You could use a switch if you wanted, but either is fine. \$\endgroup\$ Commented Dec 22, 2016 at 9:20

2 Answers 2

14
\$\begingroup\$

Naturally, you can use a dictionary to map extensions to file types:

  • "jpg" -> ImageFileType.Jpeg
  • "jpeg" -> ImageFileType.Jpeg
  • "bmp" -> ImageFileType.Bmp
  • "gif" -> ImageFileType.Gif
  • ... and so on

If the key fileExtension exists in the dictionary, you set sourceImage.ImageInfo.ImageFileType to the mapped file type, otherwise simply do nothing. That will get rid of all the if-else.

answered Dec 22, 2016 at 7:52
\$\endgroup\$
3
\$\begingroup\$

Enum.TryParse

I couldn't find the definition of ImageFileType but if this is your own type then you can parse the extensions directly as an enum. You just need to remove the . before doing this.

var imageFileType = ImageFileType.Undefined;
var parsed = Enum.TryParse<ImageFileType>(
 value: "jpg", 
 ignoreCase: true, 
 result: out imageFileType);

where

enum ImageFileType
{ 
 Undefined, // Unknown, None, etc. whatever you like
 Jpeg,
 Jpg = Jpeg,
 Png,
 //..
}

ImageFileType property

You set the file name via ImageInfo.File and then the extension... this is a little be weird. The ImageInfo class should be able to recognize the extension which means that the ImageFileType property should be read-only and return the type of the image:

class ImageInfo
{
 public string FileName { get; set; }
 public ImageFileType ImageFileType 
 {
 get
 {
 if (string.IsNullOrEmpty(FileName))
 {
 throw new InvalidOperationException($"You need to set the {nameof(FileName)} property first.");
 }
 var imageFileType = ImageFileType.Undefined;
 var parsed = Enum.TryParse<ImageFileType>(
 value: Path.GetExtension(File).Substring(1), 
 ignoreCase: true, 
 result: out imageFileType);
 return imageFileType;
 }
 }
}
answered Dec 22, 2016 at 8:11
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.