Export Jpeg image to pdf

13 posts, 0 answers
  1. AviCode
    AviCode avatar
    2 posts
    Member since:
    Nov 2014

    Posted 24 Nov 2014 Link to this post

    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.}
  2. Kammen
    Admin
    Kammen avatar
    312 posts

    Posted 25 Nov 2014 Link to this post

    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.

     
  3. Mubahil
    Mubahil avatar
    5 posts
    Member since:
    Dec 2014

    Posted 05 Dec 2014 in reply to Kammen Link to this post

    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.
  4. AviCode
    AviCode avatar
    2 posts
    Member since:
    Nov 2014

    Posted 07 Dec 2014 in reply to Mubahil Link to this post

    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;
  5. Mubahil
    Mubahil avatar
    5 posts
    Member since:
    Dec 2014

    Posted 08 Dec 2014 Link to this post

    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.
  6. Kammen
    Admin
    Kammen avatar
    312 posts

    Posted 08 Dec 2014 Link to this post

    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.

     
  7. Mubahil
    Mubahil avatar
    5 posts
    Member since:
    Dec 2014

    Posted 09 Dec 2014 in reply to Kammen Link to this post

    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.
  8. Petya
    Admin
    Petya avatar
    975 posts

    Posted 11 Dec 2014 Link to this post

    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.

     
  9. Mubahil
    Mubahil avatar
    5 posts
    Member since:
    Dec 2014

    Posted 16 Dec 2014 in reply to Petya Link to this post

    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
  10. Petya
    Admin
    Petya avatar
    975 posts

    Posted 18 Dec 2014 Link to this post

    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.

     
  11. Mubahil
    Mubahil avatar
    5 posts
    Member since:
    Dec 2014

    Posted 19 Dec 2014 in reply to Petya Link to this post

    Thanks Petya,
    It worked.
  12. Amar
    Amar avatar
    2 posts
    Member since:
    Nov 2010

    Posted 21 Apr 2015 in reply to Kammen Link to this post

    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

  13. Petya
    Admin
    Petya avatar
    975 posts

    Posted 22 Apr 2015 Link to this post

    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.

     
Back to Top