PNG Merge: MVC

2 Answers 33 Views
PdfProcessing
Joel
Top achievements
Rank 1
Veteran
Iron
Iron
Joel asked on 04 Jun 2021, 06:40 PM
As a continuation of this forum post (which seems to be archived):

https://www.telerik.com/forums/png-merge

All the given examples show how merging two images can be done in JavaScript.  I need this to happen in ASP.NET Core MVC 5.0.

I assume this will look something like how we marge PDF files.  I merge two PDF files like this:

            MemoryStream newStream = new MemoryStream();
            using (PdfStreamWriter fileWriter = new PdfStreamWriter(newStream, true))
            {
                using (PdfFileSource fileSource = new PdfFileSource(primaryStream))
                {
                    foreach (PdfPageSource pageSource in fileSource.Pages)
                    {
                        fileWriter.WritePage(pageSource);
                    }
                }

                using (PdfFileSource fileSource = new PdfFileSource(appendStream))
                {
                    foreach (PdfPageSource pageSource in fileSource.Pages)
                    {
                        fileWriter.WritePage(pageSource);
                    }
                }
            }


Aleksandar
Telerik team
commented on 09 Jun 2021, 08:28 AM

You would like to merge two or more images and the resulting file should be a PDF, is that the case? If so you can check the RadPdfProcessing Library and in particular the section on adding images as content elements to a RadFixedPage. 

If you need to merge two images and the resulting file to be an image, I'm afraid we do not provide such a library that would allow you to achieve the desired result.

Joel
Top achievements
Rank 1
Veteran
Iron
Iron
commented on 14 Jun 2021, 10:22 PM

This is what I have so far... and this DOES NOT WORK:

                using (PdfStreamWriter fileWriter = new PdfStreamWriter(newStream, true))
                {
                    RadFixedPage primaryPage = new RadFixedPage();
                    primaryPage.Content.AddImage(new ImageSource(primaryStream));
                    fileWriter.WritePage(primaryPage);

                    RadFixedPage appendPage = new RadFixedPage();
                    appendPage.Content.AddImage(new ImageSource(appendStream));
                    fileWriter.WritePage(appendPage);
                }

I get the following error on the fileWriter.WritePage(primaryPage); line:

System.InvalidOperationException
  HResult=0x80131509
  Message=FixedExtensibilityManager.JpegImageConverter cannot be null. The .NET Standard does not define APIs for converting images or scaling their quality. In order to export images different than Jpeg and Jpeg2000 or ImageQuality different than High you will need to reference the Telerik.Documents.ImageUtils assembly/NuGet in your project and to set its basic implementation to the FixedExtensibilityManager.JpegImageConverter property or to create a custom one inheriting the JpegImageConverterBase class. For more information go to: https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/cross-platform
  Source=Telerik.Documents.Fixed
  StackTrace:
   at Telerik.Windows.Documents.Fixed.Model.Resources.EncodedImageData.TryCreateFromUnknownImageDataWithScaledQuality(Byte[] data, ImageQuality imageQuality, EncodedImageData& encodedImageData)
   at Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource.DoOnUnknownData(Byte[] unknownData, ImageQuality imageQuality, Action`1 doOnEncodedData)
   at Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource.InitializeImageInfoFromUnknownData(Byte[] unknownData, ImageQuality imageQuality)
   at Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource.EnsureImageInfo()
   at Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource.get_Width()
   at Telerik.Windows.Documents.Fixed.Model.Objects.Image.get_Width()
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.ContentElementWriters.ImageWriter.WriteOverride(PdfWriter writer, IPdfContentExportContext context, Image element)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.ContentElementWriters.MarkableContentElementWriter`1.Write(PdfWriter writer, IPdfContentExportContext context, Object element)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.ContentElementWriters.ContentRootWriter.WriteOverride(PdfWriter writer, IPdfContentExportContext context, IContentRootElement element)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.ContentElementWriters.ContentElementWriter`1.Write(PdfWriter writer, IPdfContentExportContext context, Object element)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.ContentElementWriters.ContentElementWriterBase.WriteElement(PdfWriter writer, IPdfContentExportContext context, Object element)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Model.Elements.DocumentStructure.ContentStream.BuildContentData(IPdfExportContext context, IResourceHolder resourceHolder, IContentRootElement contentRootElement)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Model.Elements.Objects.FormXObject.CopyPropertiesFrom(IPdfExportContext context, IContentRootElement contentRoot, Rect boundingBox)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Streaming.PdfPageStreamWriter.GetXFormReference(RadFixedPage pageRoot)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Streaming.PdfPageStreamWriter.WriteContent(RadFixedPage page)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Streaming.PdfStreamWriter.WritePage(RadFixedPage page)
   at Gsi.Azure.Blob.Api.Services.StorageFileService.<AppendAsync>d__43.MoveNext() in C:\GSI Cloud\Gsi.Cloud.App\Gsi.Azure.Blob\Gsi.Azure.Blob.Api\Services\StorageFileService.ext.cs:line 717

2 Answers, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 17 Jun 2021, 08:38 AM

Hi Joel,

To be able to export the images, you will need to set the FixedExtensibilityManager.JpegImageConverter property. PdfProcessing provides a default implementation that you could use. Please, check the Cross-Platform Support help topic for more details on the matter. Example 3 shows how you can use the default JpegImageConverter.

Hope this is helpful.

Regards,
Tanya
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Joel
Top achievements
Rank 1
Veteran
Iron
Iron
commented on 17 Jun 2021, 04:42 PM

It seems you're just giving me some bread crumbs here without helping me move forward. I have 3 objects in the code I gave to you:

- PdfStreamWriter
- RadFixedPage
- ImageSource

Where do I attach the JpegImageConverter? None of those have a property of JpegImageConverter
Joel
Top achievements
Rank 1
Veteran
Iron
Iron
commented on 17 Jun 2021, 04:45 PM

Also, to be thorough, I am on Framework 5.0. Do you have documentation specific to this Framework?
Joel
Top achievements
Rank 1
Veteran
Iron
Iron
commented on 17 Jun 2021, 06:09 PM

I followed your document generator example and I see there is a bit to learn. However, I still get the same error:

https://github.com/telerik/document-processing-sdk/blob/master/PdfProcessing/CreateDocumentWithImages/DocumentGenerator.cs
0
Joel
Top achievements
Rank 1
Veteran
Iron
Iron
answered on 17 Jun 2021, 06:11 PM

This is my current Document Generator:


internal class DocumentGenerator
    {
        public static readonly Size PageSize = new Size(Telerik.Windows.Documents.Media.Unit.MmToDip(210), Unit.MmToDip(297));
        public static readonly Thickness Margins = new Thickness(Unit.MmToDip(10));
        public static readonly Size RemainingPageSize = new Size(PageSize.Width - Margins.Left - Margins.Right, PageSize.Height - Margins.Top - Margins.Bottom);
        public const int OpaqueAlpha = 255;

        public RadFixedDocument GenerateDocument(
            Stream primaryStream,
            Stream appendStream)
        {
            RadFixedDocument result = new RadFixedDocument();

            AddPageWithImage(result, CreateImageSource(primaryStream), "Primary Document");
            AddPageWithImage(result, CreateImageSource(appendStream), "Appended Document");

            return result;
        }

        private void AddPageWithImage(
            RadFixedDocument document, 
            ImageSource imageSource,
            string description)
        {
            RadFixedPage page = document.Pages.AddPage();
            page.Size = PageSize;
            FixedContentEditor editor = new FixedContentEditor(page);
            editor.GraphicProperties.StrokeThickness = 0;
            editor.GraphicProperties.IsStroked = false;
            editor.GraphicProperties.FillColor = new RgbColor(200, 200, 200);
            editor.DrawRectangle(new Rect(0, 0, PageSize.Width, PageSize.Height));
            editor.Position.Translate(Margins.Left, Margins.Top);

            Block block = new Block
            {
                HorizontalAlignment = HorizontalAlignment.Center
            };

            block.TextProperties.FontSize = 22;
            block.InsertText(description);
            Size blockSize = block.Measure(RemainingPageSize);
            editor.DrawBlock(block, RemainingPageSize);

            editor.Position.Translate(Margins.Left, blockSize.Height + Margins.Top + 20);

            Block imageBlock = new Block
            {
                HorizontalAlignment = HorizontalAlignment.Center
            };
            imageBlock.InsertImage(imageSource);
            editor.DrawBlock(imageBlock, RemainingPageSize);
        }

        private static ImageSource CreateImageSource(
            Stream stream,
            ImageQuality? imageQuality = null)
        {
            if (imageQuality.HasValue)
            {
                return new ImageSource(stream, imageQuality.Value);
            }
            else
            {
                return new ImageSource(stream);
            }
        }

Joel
Top achievements
Rank 1
Veteran
Iron
Iron
commented on 17 Jun 2021, 06:16 PM

The error occurs on this line:

imageBlock.InsertImage(imageSource);
Tanya
Telerik team
commented on 22 Jun 2021, 09:33 AM

Hi Joel,

It will be enough if you add the following line prior to starting the document creation:

Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.JpegImageConverter = new Telerik.Documents.ImageUtils.JpegImageConverter();

Please, note that you should add a reference to Telerik.Documents.ImageUtils.dll so that you can use the Telerik.Documents.ImageUtils.JpegImageConverter class.

Regards,
Tanya
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
PdfProcessing
Asked by
Joel
Top achievements
Rank 1
Veteran
Iron
Iron
Answers by
Tanya
Telerik team
Joel
Top achievements
Rank 1
Veteran
Iron
Iron
Share this question
or