We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Best way to transform a PDF into an "image" PDF

Hi,


I need to transform PDFs into “image” PDFs for a capricious 3rd party system which only accepts few PDFs formats (because it is first of all designed for images processing).
To be sure to create for this system PDFs it will accept I use Aspose.Pdf (v 7.6.0.0) like this:
(I created this out of different posts I found on the forum)

Transform PDF into images:
int len = 0;
Document pdfDocument = new Document(inImageFile);
            <span style="color:blue;">for</span> (<span style="color:blue;">int</span> pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
            {
                <span style="color:blue;">string</span> fileName = <span style="color:#2b91af;">Path</span>.Combine(<span style="color:#2b91af;">Path</span>.GetDirectoryName(outPdfFile), <span style="color:blue;">string</span>.Format(<span style="color:#a31515;">"{0}_page{1}.png"</span>, <span style="color:#2b91af;">Path</span>.GetFileNameWithoutExtension(outPdfFile), pageCount));

                <span style="color:blue;">using</span> (<span style="color:#2b91af;">FileStream</span> imageStream = <span style="color:blue;">new</span> <span style="color:#2b91af;">FileStream</span>(fileName, <span style="color:#2b91af;">FileMode</span>.Create))
                {
                    <span style="color:#2b91af;">Resolution</span> resolution = <span style="color:blue;">new</span> <span style="color:#2b91af;">Resolution</span>(300);
                    <span style="color:#2b91af;">PngDevice</span> pngDevice = <span style="color:blue;">new</span> <span style="color:#2b91af;">PngDevice</span>(resolution);

                    pngDevice.Process(pdfDocument.Pages[pageCount], imageStream);

                    imageStream.Close();
                }
            }

            len = pdfDocument.Pages.Count;

            ConvertImagesToPdf(len, <span style="color:#a31515;">".png"</span>, outPdfFile);</pre></div><div>=> I am using PNG because it is not destructive and compressed compared to BMP. (Some incoming PDFs are B&W and other with colors, as there is not for now any api to know it, I prefered to convert everything into PNG)</div><div><br></div><div>And this is the code to transform the images into PDF:</div><div><pre style="font-family: Consolas; font-size: 13px; background-color: white; background-position: initial initial; background-repeat: initial initial;"><span style="color:#2b91af;">Pdf</span> pdf = <span style="color:blue;">new</span> <span style="color:#2b91af;">Pdf</span>();

        <span style="color:blue;">string</span> firstImg = <span style="color:#2b91af;">Path</span>.Combine(<span style="color:#2b91af;">Path</span>.GetDirectoryName(outputFileName), <span style="color:blue;">string</span>.Format(<span style="color:#a31515;">"{0}_page{1}{2}"</span>, <span style="color:#2b91af;">Path</span>.GetFileNameWithoutExtension(outputFileName), 1, ext));

        <span style="color:blue;">using</span> (<span style="color:#2b91af;">Dotnet</span> image = <span style="color:#2b91af;">Dotnet</span>.FromFile(firstImg))
        {
            <span style="color:#2b91af;">FrameDimension</span> dimension = <span style="color:blue;">new</span> <span style="color:#2b91af;">FrameDimension</span>(image.FrameDimensionsList[0]);
            <span style="color:green;">// Select active frame.</span>
            image.SelectActiveFrame(dimension, 0);

            pdf.PageSetup.PageHeight = (<span style="color:blue;">float</span>)PixelToPoint(image.Height, image.VerticalResolution);
            pdf.PageSetup.PageWidth = (<span style="color:blue;">float</span>)PixelToPoint(image.Width, image.HorizontalResolution);
        }

        pdf.PageSetup.Margin.Top = 0;
        pdf.PageSetup.Margin.Bottom = 0;
        pdf.PageSetup.Margin.Left = 0;
        pdf.PageSetup.Margin.Right = 0;

        <span style="color:blue;">for</span> (<span style="color:blue;">int</span> i = 1; i <= imageCount; i++)
        {
            <span style="color:blue;">string</span> inputFileName = <span style="color:#2b91af;">Path</span>.Combine(<span style="color:#2b91af;">Path</span>.GetDirectoryName(outputFileName), <span style="color:blue;">string</span>.Format(<span style="color:#a31515;">"{0}_page{1}{2}"</span>, <span style="color:#2b91af;">Path</span>.GetFileNameWithoutExtension(outputFileName), i, ext));

            Aspose.Pdf.Generator.<span style="color:#2b91af;">Section</span> sec1 = pdf.Sections.Add();

            <span style="color:blue;">using</span> (<span style="color:#2b91af;">Dotnet</span> image = <span style="color:#2b91af;">Dotnet</span>.FromFile(inputFileName))
            {
                <span style="color:#2b91af;">FrameDimension</span> dimension = <span style="color:blue;">new</span> <span style="color:#2b91af;">FrameDimension</span>(image.FrameDimensionsList[0]);
                <span style="color:green;">// Select active frame.</span>
                image.SelectActiveFrame(dimension, 0);

                sec1.PageInfo.PageHeight = (<span style="color:blue;">float</span>)PixelToPoint(image.Height, image.VerticalResolution);
                sec1.PageInfo.PageWidth = (<span style="color:blue;">float</span>)PixelToPoint(image.Width, image.HorizontalResolution);
            }

            sec1.PageInfo.Margin.Top = 0;
            sec1.PageInfo.Margin.Bottom = 0;
            sec1.PageInfo.Margin.Left = 0;
            sec1.PageInfo.Margin.Right = 0;

            <span style="color:green;">//Create an image object in the section</span>
            Aspose.Pdf.Generator.<span style="color:#2b91af;">Image</span> image1 = <span style="color:blue;">new</span> Aspose.Pdf.Generator.<span style="color:#2b91af;">Image</span>(sec1);

            <span style="color:green;">//Add image object into the Paragraphs collection of the section</span>
            sec1.Paragraphs.Add(image1);

            <span style="color:green;">//Set the path of image file</span>
            image1.ImageInfo.File = inputFileName;

            <span style="color:green;">//Set the type of image using ImageFileType enumeration</span>
            image1.ImageInfo.ImageFileType = GetTypeFromExt(ext);

            image1.ImageInfo.FixWidth = sec1.PageInfo.PageWidth;

            image1.ImageInfo.FixHeight = sec1.PageInfo.PageHeight;
        }

        pdf.Save(outputFileName);</pre></div><div>1) Is it for you the best way to acheive my process with the current Aspose version ?</div><div>2) Some people complain it is slow, is there a way to make it quicker ?</div><div><br></div><div>Thnaks</div><div><br></div><div><br></div><p class="MsoNormal"><o:p></o:p></p>

Hi Daniel,


Thanks for using our products and sorry for the delayed response.

I have gone through the code snippets which you have shared to convert PDF pages to Image (PNG) format and also the code to convert Image files to PDF format using Pdf class under Aspose.Pdf.Generator namespace and I would like to share that these are the correct and optimal approaches to accomplish your requirements. However if you are facing any performance related issue while fulfilling these requirements, please share the resource files so that we can test the scenario at our end.