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);
\$\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\$ 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.

\$\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;
        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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