How to convert PDF to image with same resolution as original PDF

I am using Aspose.Pdf to convert PDF to Image and latter convert Image back to Pdf. I would like to keep resolution, width and height of the new Pdf same as original Pdf.

            using (var newDocument = new Document())
            {
                using (var originalDocument = new Document("c:\\source.pdf"))
                {
                    foreach (Aspose.Pdf.Page page in originalDocument.Pages)
                    {
                        if (page.Rotate != Aspose.Pdf.Rotation.None)
                        {
                            using (var ms = new MemoryStream())
                            {

                                // Create Jpegdevice with specified attributes
                                // Width, Height, Resolution, Quality
                                // Quality [0-100], 100 is Maximum
                                // Create Resolution object
                                Resolution resolution = new Resolution(Convert.ToInt32(page.ArtBox.Width), Convert.ToInt32(page.ArtBox.Height));
                                JpegDevice device = new JpegDevice(resolution, 100);

                                // Convert a particular page and save the image to stream
                                device.Process(page, ms);                                    

                                var newPage = newDocument.Pages.Add();
                                // Set margins so image will fit, etc.
                                newPage.PageInfo.Margin.Bottom = 0;
                                newPage.PageInfo.Margin.Top = 0;
                                newPage.PageInfo.Margin.Left = 0;
                                newPage.PageInfo.Margin.Right = 0;


                                // Create an image object
                                Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();

                                // Set the image file stream
                                image1.ImageStream = ms;

                                // Add the image into paragraphs collection of the section
                                newPage.Paragraphs.Add(image1);

                                // YES, save after every page otherwise we get out of memory exception
                                newDocument.Save(txtFolder.Text + "\\out.pdf");
                            }                                
                        }
                        else
                        {
                            newDocument.Pages.Add(page);
                            newDocument.Save(txtFolder.Text + "\\out.pdf");
                        }
                    }
                }
            }

Qustions

1> The original PDF size is 18MB but the above code creates large file of size 163MB. I would like keep the size as it is or least somewhat near to original file size by keeping the same resolution.

2>Resolution class also takes int valueX and int valueY parameters. And JpegDevice class takes int width and int height parameters. However it does not specify unit, weathers its in points, pixels, inches etc?

As per Aspose.Pdf documentation

The conversion from point to pixel depends on an image’s DPI (dots per
inch) property. For example, if an image’s DPI is 96 (96 pixels for
each inch), and it is 100 points high, its height in pixels is (100 /
72) * 96 = 133.3. The general formula is: pixels = ( points / 72 ) *
DPI.

However that does not answer the question what unit the API is asking?

Original Issue
We get PDF from our clients. Some of the PDF we receive has rotation of 90 or 270 degree. However when you open them in Adobe or any other PDF viewer, the page’s orientation is correct (it is not landscape or horizontal). All this mess i have to do becuase Aspose.Pdf cannot keep the same orientation when change the rotation to None. So suggested approach is convert PDF to Image and latter convert Image back to Pdf

I have also created SO post for the same

@Laksh

Thank you for contacting support.

Would you please share sample file so that we may investigate further. Moreover, default unit in Aspose.PDF API is point where 72 points equal 1 inch.

You can take any PDF and run through the code above. I think it has to do with resolution and quality that increase the size.

@Laksh

Would you please try to set IsBlackWhite property to true and then share your kind feedback if this suites your requirements and lessens the file size.

using (var newDocument = new Document())
{
    using (var originalDocument = new Document(dataDir + "Test.pdf"))
    {
        PdfFileInfo info = new PdfFileInfo(originalDocument);
        foreach (Aspose.Pdf.Page page in originalDocument.Pages)
        {
            if (page.Rotate != Aspose.Pdf.Rotation.None)
            {
                using (var ms = new MemoryStream())
                {

                    // Create Jpegdevice with specified attributes
                    // Width, Height, Resolution, Quality
                    // Quality [0-100], 100 is Maximum
                    // Create Resolution object
                    Resolution resolution = new Resolution(Convert.ToInt32(info.GetPageWidth(page.Number)), Convert.ToInt32(info.GetPageHeight(page.Number)));
                    JpegDevice device = new JpegDevice(resolution, 100);

                    // Convert a particular page and save the image to stream
                    device.Process(page, ms);

                    var newPage = newDocument.Pages.Add();
                    // Set margins so image will fit, etc.
                    newPage.PageInfo.Margin.Bottom = 0;
                    newPage.PageInfo.Margin.Top = 0;
                    newPage.PageInfo.Margin.Left = 0;
                    newPage.PageInfo.Margin.Right = 0;


                    // Create an image object
                    Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
                    image1.IsBlackWhite = true;
                    // Set the image file stream
                    image1.ImageStream = ms;

                    // Add the image into paragraphs collection of the section
                    newPage.Paragraphs.Add(image1);

                    // YES, save after every page otherwise we get out of memory exception
                    newDocument.Save(dataDir + "Test_19.10.pdf");
                }
            }
            else
            {
                newDocument.Pages.Add(page);
                newDocument.Save(dataDir + "Test_out.pdf");
            }
        }
    }
}