Skip to content

Commit db830c8

Browse files
Update create-custom-jpeg-image-converter-net-standard.md
1 parent ef546c7 commit db830c8

File tree

1 file changed

+59
-56
lines changed

1 file changed

+59
-56
lines changed

knowledge-base/create-custom-jpeg-image-converter-net-standard.md

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ res_type: kb
1919
</thead>
2020
<tbody>
2121
<tr>
22-
<td>up to 2023.1.315</td>
22+
<td>up to 2023.1.410</td>
2323
<td>RadPdfProcessing</td>
2424
<td rowspan="2" ><a href="https://www.telerik.com/blogs/author/martin-velikov">Martin Velikov</a></td>
2525
</tr>
@@ -32,90 +32,93 @@ res_type: kb
3232

3333
## Description
3434

35-
**.NET Standard** specification does not define APIs for converting images or scaling their quality. That is why to export to PDF format a document containing images different than Jpeg and Jpeg2000 or ImageQuality different than High, you will need to provide an implementation of the **JpegImageConverterBase** abstract class. This implementation should be passed to the **JpegImageConverter** property of the **FixedExtensibilityManager**.
35+
**.NET Standard** specification does not define APIs for converting images or scaling their quality. That is why, when inserting images in a PDF document different than Jpeg and Jpeg2000 or ImageQuality different than High, you will need to implement the **JpegImageConverterBase** abstract class. This implementation should be passed to the **JpegImageConverter** property of the **FixedExtensibilityManager**.
3636

3737
>important With the [R2 2023 changes](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/changes-and-backward-compatibility/backward-compatibility#whats-different-in-2023-r2) SkiaSharp replaced ImageSharp as the required dependency.
3838
3939
## Solution
4040

41-
The following code snippets demonstrate how to create a custom implementation of the JpegImageConverterBase abstract class using the [SixLabors.ImageSharp](https://github.com/SixLabors/ImageSharp) library and set it to the JpegImageConverter property of the FixedExtensibilityManager. We are using the ImageSharp library to convert the images from one of the library's supported formats to Jpeg and to change their quality if it is set.
41+
The following code snippets demonstrate how to create a custom implementation of the JpegImageConverterBase abstract class using the [SixLabors.ImageSharp](https://github.com/SixLabors/ImageSharp) library and set it to the JpegImageConverter property of the FixedExtensibilityManager. We are using the ImageSharp library to convert the images from one of the library's supported formats to Jpeg and to change their quality if it is set. Note that this approach is valid up to version 2023.1.410 of RadPdfProcessing.
4242

4343
#### __[C#] Create a custom implementation inheriting the JpegImageConverterBase abstract class__
4444

4545
{{region kb-create-custom-jpeg-image-converter1}}
4646

47-
internal class CustomJpegImageConverter : JpegImageConverterBase
48-
{
49-
public override bool TryConvertToJpegImageData(byte[] imageData, ImageQuality imageQuality, out byte[] jpegImageData)
47+
public class CustomJpegImageConverter : JpegImageConverterBase
5048
{
51-
try
49+
public override bool TryConvertToJpegImageData(byte[] imageData, ImageQuality imageQuality, out byte[] jpegImageData)
5250
{
53-
IImageFormat imageFormat;
54-
using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(imageData, out imageFormat))
51+
try
5552
{
56-
if (imageFormat.GetType() == typeof(PngFormat))
53+
IImageFormat imageFormat;
54+
using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(imageData, out imageFormat))
5755
{
58-
image.Mutate(x => x.BackgroundColor(Color.White));
56+
if (imageFormat.GetType() == typeof(PngFormat))
57+
{
58+
image.Mutate(x => x.BackgroundColor(Color.White));
59+
}
60+
61+
JpegEncoder options = new JpegEncoder
62+
{
63+
Quality = (int)imageQuality,
64+
};
65+
66+
using (MemoryStream ms = new MemoryStream())
67+
{
68+
image.SaveAsJpeg(ms, options);
69+
70+
jpegImageData = ms.ToArray();
71+
}
5972
}
6073

61-
JpegEncoder options = new JpegEncoder
74+
return true;
75+
}
76+
catch (Exception ex)
77+
{
78+
if (ex is UnknownImageFormatException || ex is ImageProcessingException)
6279
{
63-
Quality = (int)imageQuality,
64-
};
65-
66-
using (MemoryStream ms = new MemoryStream())
80+
jpegImageData = null;
81+
return false;
82+
}
83+
else
6784
{
68-
image.SaveAsJpeg(ms, options);
69-
70-
jpegImageData = ms.ToArray();
85+
throw ex;
7186
}
7287
}
73-
74-
return true;
75-
}
76-
catch (Exception ex)
77-
{
78-
if (ex is UnknownImageFormatException || ex is ImageProcessingException)
79-
{
80-
jpegImageData = null;
81-
return false;
82-
}
83-
else
84-
{
85-
throw ex;
86-
}
8788
}
8889
}
89-
}
9090

9191
{{endregion}}
9292

9393
#### __[C#] Set the custom implementation to the JpegImageConverter property of the FixedExtensibilityManager__
9494

9595
{{region kb-create-custom-jpeg-image-converter2}}
9696

97-
JpegImageConverterBase customJpegImageConverter = new CustomJpegImageConverter();
98-
FixedExtensibilityManager.JpegImageConverter = customJpegImageConverter;
99-
100-
101-
// RadPdfProcessing version 2023.1.307
102-
PdfFormatProvider provider = new PdfFormatProvider();
103-
provider.ExportSettings.ImageQuality = ImageQuality.High;
104-
provider.ExportSettings.ImageCompression = new ImageFilterTypes[] { ImageFilterTypes.FlateDecode };
105-
RadFixedDocument document;
106-
string input = "input.pdf";
107-
using (Stream stream = File.OpenRead(input))
108-
{
109-
document = provider.Import(stream);
110-
}
111-
112-
string outputFilePath = "output.pdf";
113-
File.Delete(outputFilePath);
114-
using (Stream output = File.OpenWrite(outputFilePath))
115-
{
116-
provider.Export(document, output);
117-
}
118-
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
97+
JpegImageConverterBase customJpegImageConverter = new CustomJpegImageConverter();
98+
FixedExtensibilityManager.JpegImageConverter = customJpegImageConverter;
99+
100+
// RadPdfProcessing version up to 2023.1.410
101+
PdfFormatProvider provider = new PdfFormatProvider();
102+
string imageFolderPath = @"..\..\..\images";
103+
string[] imageFiles = Directory.GetFiles(imageFolderPath);
104+
RadFixedDocument fixedDocument = new RadFixedDocument();
105+
RadFixedDocumentEditor documentEditor = new RadFixedDocumentEditor(fixedDocument);
106+
foreach (string imageFilePath in imageFiles)
107+
{
108+
FileStream fileStream = new FileStream(imageFilePath, FileMode.Open);
109+
Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource _imageSource = new Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource(fileStream);
110+
documentEditor.InsertImageInline(_imageSource);
111+
documentEditor.InsertLineBreak();
112+
}
113+
documentEditor.Dispose();
114+
115+
string outputFilePath = @"output.pdf";
116+
File.Delete(outputFilePath);
117+
using (Stream output = File.OpenWrite(outputFilePath))
118+
{
119+
provider.Export(fixedDocument, output);
120+
}
121+
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
119122

120123
{{endregion}}
121124

0 commit comments

Comments
 (0)