Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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);
share|improve this question
    
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. – JᴀʏMᴇᴇ 9 hours ago

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.

share|improve this answer

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;
        }
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.