This is a migrated thread and some comments may be shown as answers.
Export Jpeg image to pdf
21 Answers 451 Views
This is a migrated thread and some comments may be shown as answers.
AviCode
Top achievements
Rank 1
AviCode asked on 24 Nov 2014, 09:55 AM
Hello

I would like to create PDF file, as container for images, (the source of the images is scanner).
I order to make small file size, I'm converting the images from BMP to JPEG and then do the export to PDF file,
but the output file size is huge.
I'm following this example.


1. How can i control the PDF file size?
2. Is the PDF export saves JPEG  image as is or decode and then save it?


My Code:

01.void f(List<Bitmap> Images)
02.{
03.  DateTime now = DateTime.Now;
04.  string name = "scan-" + now.ToString("yyyyMMdd") + "-" + now.ToString("HHmmss") + "_3" + ".pdf";
05.             
06.  using (FileStream FileStream = new FileStream(@"c:\Users\avicode\Desktop\tmp\" + name,FileMode.Create))
07.  using (MemoryStream ms = new MemoryStream())
08.             
09.  if (Images.Count >= 1)
10.  {
11.    RadDocument document = new RadDocument();
12.    for (int i = 0; i < Images.Count; i++)
13.    {
14.      System.Drawing.Image oBmpImage = (System.Drawing.Image)Images[Images.Count - 1];
15.      oBmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
16.      Telerik.Windows.Documents.Model.Section section = new Telerik.Windows.Documents.Model.Section();
17.      Telerik.Windows.Documents.Model.Paragraph paragraph = new Telerik.Windows.Documents.Model.Paragraph();
18. 
19.      ImageInline image = new ImageInline(ms);
20.      paragraph.Inlines.Add(image);
21.      section.Blocks.Add(paragraph);
22.      document.Sections.Add(section);
23. 
24.    }
25.    PdfFormatProvider oPdf = new PdfFormatProvider();
26.    oPdf.Export(document,FileStream);
27.    }
28.}

21 Answers, 1 is accepted

Sort by
0
Kammen
Telerik team
answered on 25 Nov 2014, 05:26 PM
Hello,

Thank you for contacting us.

Below you can find sample code that gets an array of image paths and exports them to PDF.

string[] images = new string[] { @"image1.jpg", @"image2.jpg", @"image3.jpg" };
 
RadFixedDocument document = new RadFixedDocument();
foreach (string image in images)
{
    using (Stream stream = File.OpenRead(image))
    {
        ImageSource imageSource = new ImageSource(stream);
        document.Pages.AddPage().Content.AddImage(imageSource);
    }
}
 
PdfFormatProvider provider = new PdfFormatProvider();
using (Stream output = new FileStream("images.pdf", FileMode.OpenOrCreate))
{
    provider.ExportSettings.ImageQuality = ImageQuality.Medium;
    provider.Export(document, output);
}

Note that this code uses Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider that is part of RadPdfProcessing. Your code sample uses the PdfFormatProvider related to RadRichTextBox.

As you can see from the example ExportSettings.ImageQuality is the property which can be used to specify the quality of the exported image. The higher is the quality, the bigger is the final PDF document.

Hope this answers your questions.

Regards,
Kammen
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Mubahil
Top achievements
Rank 1
answered on 06 Dec 2014, 04:47 AM
When I try to export the images to PDF the images gets stretched and cropped.
How to fit the image in the .pdf document using fit to page. Image should get smaller to fit in the page.

Thanks.
0
AviCode
Top achievements
Rank 1
answered on 07 Dec 2014, 08:43 AM
Hi,
Try to change the page setting, for example:

1.section.PageSize = PaperTypeConverter.ToSize(PaperTypes.A4);
2.section.PageMargin = new Telerik.Windows.Documents.Layout.Padding(0);
3.section.PageOrientation = PageOrientation.Portrait;
0
Mubahil
Top achievements
Rank 1
answered on 08 Dec 2014, 01:11 PM
Hi,

I was using the RadFixedDocument, RadFixedPage classes for exporting the PDF. These classes are part of Telerik.Windows.Documents.Fixed.Model namespace using the dll "Telerik.Windows.Documents.Fixed.Model. in the dll of Version 2014.3.1202.40.

I have the license of telerik in which the dll version are "2012.1.326.40", where can I find the mentioned classes  in the older version.

Thanks.
0
Kammen
Telerik team
answered on 08 Dec 2014, 01:26 PM
Hello

@Mubahil
There is no setting that will allow image to fit to page. By default Images have the same size as its ImageSource. If you want to change it, you can explicitly set the image size explicitly. You can calculate the fit size and then set it to the image. The code look like this:
private static void DrawImage(RadFixedPage page, ImageSource source)
{
    Size size = CalculateFitImageToPage(page.Size, source);
    Image image = new Image();
    image.ImageSource = source;
    image.Width = size.Width;
    image.Height = size.Height;
             
    page.Content.Add(image);
}
 
private static Size CalculateFitImageToPage(Size fitSize, ImageSource source)
{
    double k1 = fitSize.Width / source.Width;
    double k2 = fitSize.Height / source.Height;
 
    double k = Math.Min(k1, k2);
 
    return new Size(source.Width * k, source.Height * k);
}

Otherwise, you can change the page size to match the size of the image as shown below:
private static void DrawImage(RadFixedPage page, ImageSource source)
{
    page.Size = new Size(source.Width, source.Height);
    page.Content.AddImage(source);
}

As to the versions question, RadPdfProcessing and its related functionality was first introduced in Q2 2014, so I'm afraid you cannot use it in version 2012.1.326.40.
 
Hope this answers your question.

@AviCode
This code will work for RadDocument which is the base document of the RadRichTextBox model.

Regards,
Kammen
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Mubahil
Top achievements
Rank 1
answered on 09 Dec 2014, 08:53 AM
Thanks Kammen,

In that case is there any way in older version (2012.1.326.40) of telerik by which JPEG to PDF conversion is possible.
0
Petya
Telerik team
answered on 11 Dec 2014, 06:25 PM
Hello Mubahil,

In that case you can use the approach that AviCode suggested - RadRichTextBox's API. You'd need to insert an image in the document as per this article. Then, use the respective format provider to export the document.

I hope this is useful.

Regards,
Petya
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Mubahil
Top achievements
Rank 1
answered on 16 Dec 2014, 11:12 AM
Thanks Petya,

It worked for me. Only issue I am facing is that I am not able to set the page margin to 0.
There is a portion on left and top that is blank after the image is exported.
Is there a way to set the margin to 0.

Below is the code I am using.

private void ConvertToPdf(List<string> Images, string OutputFilePath, string OutputFileName)
        {

            if (Images.Count >= 1)
            {

                RadDocument document = new RadDocument();
                

                double documentHeight = document.SectionDefaultPageSize.Height;
                double documentWidth = document.SectionDefaultPageSize.Width;
                for (int i = 0; i < Images.Count; i++)
                {
                    using (Stream input = new FileStream(Images[i], FileMode.OpenOrCreate))
                    {
                        Telerik.Windows.Documents.Model.Section section = new Telerik.Windows.Documents.Model.Section();
                        Telerik.Windows.Documents.Model.Paragraph paragraph = new Telerik.Windows.Documents.Model.Paragraph();

                        ImageInline image = new ImageInline(input);

                        // To Resize the image to fit in the page
                        image = ResizeImage(documentHeight, documentWidth, image);
                        
                        paragraph.Inlines.Add(image);
                        
                        section.Blocks.Add(paragraph);

                        document.Sections.Add(section);

                    }
                }

                PdfFormatProvider provider = new PdfFormatProvider();
                if (!Directory.Exists(OutputFilePath))
                {
                    Directory.CreateDirectory(OutputFilePath);
                }

                using (Stream output = new FileStream(Path.Combine(OutputFilePath, OutputFileName), FileMode.OpenOrCreate))
                {
                    provider.Export(document, output);
                }
            }
        }

Regards,
Mubahil
0
Petya
Telerik team
answered on 18 Dec 2014, 04:08 PM
Hello Mubahil,

Try changing the margin of the created sections:
Telerik.Windows.Documents.Model.Section section = new Telerik.Windows.Documents.Model.Section();
section.PageMargin = new Padding(2);

Let me know how this works.

Regards,
Petya
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Mubahil
Top achievements
Rank 1
answered on 19 Dec 2014, 09:53 AM
Thanks Petya,
It worked.
0
Amar
Top achievements
Rank 1
answered on 21 Apr 2015, 10:28 AM

Hi 

I am getting error on following..

private static void DrawImage(RadFixedPage page, ImageSource source)
{
  Size size = CalculateFitImageToPage(page.Size, source);
  Image image = new Image();
  image.ImageSource = source;
  image.Width = size.Width;
  image.Height = size.Height;
 
  page.Content.Add(image);
}

Please also confirm if it is capable of handling 1000+ pages.

Regards
Amar

Error   1   'System.Windows.Controls.Image' does not contain a definition for 'ImageSource' and no extension method 'ImageSource' accepting a first argument of type 'System.Windows.Controls.Image' could be found (are you missing a using directive or an assembly reference?)   C:\Users\Amar\documents\visual studio 2013\Projects\CopyEbooks\CopyEbooks\MainWindow.xaml.cs    133 19  CopyEbooks

Error   2   The best overloaded method match for 'Telerik.Windows.Documents.Core.Data.DocumentElementCollectionBase<Telerik.Windows.Documents.Fixed.Model.Common.ContentElementBase,Telerik.Windows.Documents.Fixed.Model.Common.IContainerElement>.Add(Telerik.Windows.Documents.Fixed.Model.Common.ContentElementBase)' has some invalid arguments    C:\Users\Amar\documents\visual studio 2013\Projects\CopyEbooks\CopyEbooks\MainWindow.xaml.cs    137 13  CopyEbooks

0
Petya
Telerik team
answered on 22 Apr 2015, 01:42 PM
Hello Amar,

Looking at the errors, it looks like there might be a namespace mismatch in the project you are trying this in.

Please make sure all required for RadPdfProcessing assembly references have been added. Additionally, the Image and ImageSource classes need to be from the Telerik.Windows.Documents.Fixed.Model.Objects and Telerik.Windows.Documents.Fixed.Model.Resources namespaces, respectively. You might also want to check the documentation for the two concepts here and here.

I hope this helps.

Regards,
Petya
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
sarvesh
Top achievements
Rank 1
answered on 05 Nov 2019, 05:12 PM

 private void PdfCreate(List<AttachmentsFileModel> attachmentsFileModel)
        {
            RadFixedDocument document = new RadFixedDocument();
            foreach (var item in attachmentsFileModel)
            {
                string[] images = new string[] { item.strFileUri };
                foreach (string imagePdf in images)
                {
                    
                    using (Stream stream = File.OpenRead(imagePdf))
                    {
                        ImageSource imageSource = new ImageSource(stream);
                        document.Pages.AddPage().Content.AddImage(imageSource);
                    }
                }
            }
            PdfFormatProvider provider = new PdfFormatProvider();
            var documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string imagePdfPath = Path.Combine(documentsDirectory, Guid.NewGuid() + ".pdf");
            using (Stream output = new FileStream(imagePdfPath, FileMode.OpenOrCreate))
            {
                provider.ExportSettings.ImageQuality = ImageQuality.High;
                provider.Export(document, output);
            }

        } 

 

Its generate pdf file but crop actual image height and width please give me more details how to create pdf with actual images.

Thanks 

Sarvesh Singh

0
Nikolay Demirev
Telerik team
answered on 08 Nov 2019, 08:42 AM

Hello Sarvesh,

Have you tried to set the page size to be equal to the page size summed with the page margins? Here is a sample code:

page.Size = new Size(source.Width, source.Height);

Regards,
Nikolay Demirev
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
sarvesh
Top achievements
Rank 1
answered on 08 Nov 2019, 12:54 PM

Hi Kammen,

I am using the below set of code to set the image quality and export to pdf but setting " ImageQuality.Medium" or " ImageQuality.High" giving exception on the basis of the imported image like if i am setting the ImageQuality.Medium then it giving exception on importing high resolution image but working in case of medium resolution images and if i am setting the ImageQuality.High then it giving exception on
importing low/medium resolution image and working in case of High resolution images. Can you please provide your suggestion over the same.

 

Thank you.

 

using (Stream output = new FileStream("images.pdf", FileMode.OpenOrCreate))
{
    provider.ExportSettings.ImageQuality = ImageQuality.Medium;
    provider.Export(document, output);
}

0
sarvesh
Top achievements
Rank 1
answered on 11 Nov 2019, 12:19 PM

Hi,

I am using below set of code to create the imageSource from image file but it not creating it proper if i am selecting low images like Mobile screenshots from gallery. It raising exception "Can not export other than Jpeg and Jpeg2000 and ImageQuality different than High at Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource.DoOnUnknownData"

 

using (Stream stream = File.OpenRead(image))
    {
        ImageSource imageSource = new ImageSource(stream);
        document.Pages.AddPage().Content.AddImage(imageSource);
    }

Please let me know what can i do to support low images as well.

0
sarvesh
Top achievements
Rank 1
answered on 11 Nov 2019, 12:20 PM
Hi,
I am using below set of code to create the imageSource from image file but it not creating it proper if i am selecting low images like Mobile screenshots from gallery. It raising exception "Can not export other than Jpeg and Jpeg2000 and ImageQuality different than High at Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource.DoOnUnknownData"

using (Stream stream = File.OpenRead(image))
    {
        ImageSource imageSource = new ImageSource(stream);
        document.Pages.AddPage().Content.AddImage(imageSource);
    }
Please let me know what can i do to support low images as well.
0
sarvesh
Top achievements
Rank 1
answered on 12 Nov 2019, 10:55 AM

Hi,

I am facing this issue in Xamarin. Please let me know if anybody have any suggestion or solution on this..

 

Thank you.

 

0
Nikolay Demirev
Telerik team
answered on 12 Nov 2019, 04:03 PM

Hello Sarvesh,

The binaries included in the Telerik UI for Xamarin suite are targeting .NET Standard. The .NET Standard does not have all the APIs .NET Framework has. That is the reason why the library can not convert the image format or change its quality. In order to export documents containing images, you have to use JPEG or JPEG2000 image formats. Images in those formats do not need any modification before including them in PDF documents.

Regards,
Nikolay Demirev
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
sarvesh
Top achievements
Rank 1
answered on 13 Nov 2019, 01:22 PM

Hi Nikolay,

We are selecting .jpg images form iOS device gallery and this issues is coming only if we are selecting the screenshot images taken. Can you please let us know Is there any restriction over the quality of the images to export them into pdf???

0
Nikolay Demirev
Telerik team
answered on 18 Nov 2019, 10:56 AM

Hi Sarvesh,

The iOS screenshots are PNG images and are not supported. Try to convert them to jpeg and then insert them into the PDF. 

There is no restriction about the quality of the image. The only restriction is that the ImageQuality property of the PdfExportSettings should not be changed from its default value (High), because currently, the library can not downgrade the image quality or convert PNG files to JPEG in order to insert them in a PDF document.

Regards,
Nikolay Demirev
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Asked by
AviCode
Top achievements
Rank 1
Answers by
Kammen
Telerik team
Mubahil
Top achievements
Rank 1
AviCode
Top achievements
Rank 1
Petya
Telerik team
Amar
Top achievements
Rank 1
sarvesh
Top achievements
Rank 1
Nikolay Demirev
Telerik team
Share this question
or