patterncsharpModerate
Classifying image by file extension within a PDF creator
Viewed 0 times
fileimageextensionclassifyingwithincreatorpdf
Problem
To create a
Any patterns or any design strategies? It may be a over do for the example , but will be useful.
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);Solution
Naturally, you can use a dictionary to map extensions to file types:
If the key
That will get rid of all the if-else.
- "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.
Context
StackExchange Code Review Q#150565, answer score: 14
Revisions (0)
No revisions yet.