Write pdf page in memorystream

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF Rasterizer for .NET (v2)  /  Re: Write pdf page in memorystream

DynamicPDF Rasterizer for .NET (v2) Forum

 Apr 18 2016 9:56 AM
Hello,

I am using the trial version of Dynamic PDF Rasterizer.

I want to draw a particular page of a pdf document and cast it into System.Drawing.Image type without saving in a physical directory.

Can you please help?
 Apr 18 2016 11:27 AM
Following is the code I am using,

using ceTe.DynamicPDF.Rasterizer;
.
.
.
public PdfRasterizer _rasterizer = null;
.
.
.
public <myType> ConvertToImg(string sourceFilePath, int PageNo)
        {          
           
            byte[] ms = null;
            try
            {
                if ((!String.IsNullOrEmpty(sourceFilePath) && sourceFilePath.Trim().Length != 0) &&  (PageNo > 0))
                {
                    string nameOfTheFile = Path.GetFileName(sourceFilePath);
                    string fileName = sourceFilePath;
                    _rasterizer = new PdfRasterizer(sourceFilePath, PageNo, 1);
                    if (PageNo <= numberOfPages)
                    {
                        FixedImageSize fixedImageSize = new FixedImageSize(595, 841);
                        TiffImageFormat tiffImageFormat = new TiffImageFormat(TiffColorFormat.Rgb);
                        ms = _rasterizer.DrawToMultiPageTiff(tiffImageFormat, ImageSize.Dpi96);
                        .
                        .
                        .
                        .
                    }
                    _rasterizer.Dispose();
                   

                }
                else {
                    throw new Exception();
                }
            }
            catch (Exception ex)
            {
                .
                .
                .
            }
            return <myTypeObj>;
        }

The problem is, when I am converting ms to base64 string, it can not generate the correct image.

What I am doing wrong?
 Apr 18 2016 3:13 PM
Posted by a ceTe Software moderator
Hello,

Rasterizer cannot directly output the PDF as System.Drawing.Image object, but it can output a System.Drawing.Bitmap object which is actually derived from System.Drawing.Image object. Here is the sample code:

            InputPdf pdf = new InputPdf(@"Path for input PDF");
            PdfRasterizer rastObj = new PdfRasterizer(pdf);
            PdfRasterizerPage page = rastObj.Pages[0];
            System.Drawing.Bitmap imageData = page.DrawToBitmap(BitmapColorFormat.RgbA, ImageSize.Dpi72, false);

Our product does not have any methods or properties that you can use to generate a base64 string of the resulting image. You would need to create a byte array (similar to what you are doing in your code) and then convert that byte array to a base64 string by some other method. We tested your code by saving the byte array (ms) to a file and that produced a valid TIFF file. See code below.

             byte[] ms = null;

             if ((!String.IsNullOrEmpty(sourceFilePath) && sourceFilePath.Trim().Length != 0) && (PageNo > 0))
             {
                 string nameOfTheFile = Path.GetFileName(sourceFilePath);
                 string fileName = sourceFilePath;
                 _rasterizer = new PdfRasterizer(sourceFilePath, PageNo, 1);
                 if (PageNo <= numberOfPages)
                 {
                     FixedImageSize fixedImageSize = new FixedImageSize(595, 841);
                     TiffImageFormat tiffImageFormat = new TiffImageFormat(TiffColorFormat.Rgb);
                     ms = _rasterizer.DrawToMultiPageTiff(tiffImageFormat, ImageSize.Dpi96);
                 }
             }

             File.WriteAllBytes("test.tiff", ms); //writing the byte array to file.

Please do the same on your end and see if the image that results from saving the byte array (ms) to file is valid. If it is then you may want to take a look at your base64 conversion code to see if it is working correctly.

Thanks,
ceTe Software Support Team.

All times are US Eastern Standard time. The time now is 7:37 PM.