Starting with Version 2.0, AspPDF is capable of converting a PDF page
to an image via the method PdfPage.ToImage.
The ToImage method returns an instance of the PdfPreview object
which performs the PDF-to-image conversion and generates the page image.
The image can then be saved to disk, memory or an HTTP stream. The ToImage method
accepts an optional PdfParam object or parameter string argument.
PdfPreview generates images in PNG format. This versatile image format
is ideal for the task since it supports true colors and uses artifact-free
lossless compression. PNG format is fully supported by all major browsers.
The following code sample generates a PDF document from a URL, saves/reopens it,
and converts page 1 of the document to an image.
The saving/reopening step is necessary because the PdfPage.ToImage method can only be used
on existing, not new, documents.
| VBScript |
Set Pdf = Server.CreateObject("Persits.Pdf")
Set Doc = Pdf.CreateDocument
Doc.ImportFromUrl "http://www.persits.com", "landscape=true"
' Save and reopen as Page.Preview only works on new documents.
Set NewDoc = Pdf.OpenDocumentBinary( Doc.SaveToMemory )
' Create preview for Page 1 at 50% scale.
Set Page = NewDoc.Pages(1)
Set Preview = Page.ToImage("ResolutionX=36; ResolutionY=36" )
Preview.SaveHttp( "filename=preview.png" )
|
| C# |
IPdfManager objPdf = new PdfManager();
IPdfDocument objDoc = objPdf.CreateDocument( Missing.Value );
objDoc.ImportFromUrl( "http://www.persits.com", "landscape=true",
Missing.Value, Missing.Value );
// Save and reopen as Page.Preview only works on new documents.
IPdfDocument objNewDoc =
objPdf.OpenDocumentBinary( objDoc.SaveToMemory(), Missing.Value );
// Create preview for Page 1 at 50% scale.
IPdfPage objPage = objNewDoc.Pages[1];
IPdfPreview objPreview = objPage.ToImage("ResolutionX=36; ResolutionY=36" );
objPreview.SaveHttp( "filename=preview.png" );
|
Click on the links below to run this code sample:
http://localhost/asppdf/manual_14/14_pdftoimage.asp
http://localhost/asppdf/manual_14/14_pdftoimage.aspx
By default, the resultant image's width and height in pixels match the page's width and height in user units.
For example, a standard US Letter page (8.5 x 11 inches or 612 x 792 user units)
becomes a 612 x 792 pixel image (in case of a landscape-oriented page,
the width and height of the image matches the height and width of the page, respectively.)
To set the page image to a desired size, the ResolutionX and ResolutionY parameters
to the ToImage method should be used. These parameters are 72 (dpi) by default.
Note that in the code sample above, these parameters are both set to 36 which makes
the image dimensions half of what they would be by default. Setting these parameters to a
number larger (smaller) than 72 makes the resultant image proportionally larger (smaller).
The ResolutionX and ResolutionY values usually equal each other to avoid a distortion in the
image's aspect ratio.
To obtain the pixel dimensions of the resultant image, use the PdfPreview object's properties
Width and Height.