C #. convert a PDF file into an image file

Source: Internet
Author: User

This article uses the Adobe batch bat9.0 COM component to convert each page of the PDF file into a corresponding image file.
Development Environment: VS2010,. Net Framework4.0, Adobe javasbat9.0.
Add COM reference to the project: Adobe Acrobat 9.0 Type Library (must be installed with Adobe batch bat9.0 ).
 
Ideas:
1. the COM object to be used:
1) CAcroPDDoc: Acrobat Document Object.
2) CAcroPDPage: Page Object.
3) CAcroRect: used to describe the object of a rectangular area in the page.
4) CAcroPoint: actually represents the Size.
 
2. conversion process:
1) Open the document.
2) retrieve each page.
3) obtain the size of each page and generate a rectangle that represents the page.
4) encode the specified area of the current page into an image and copy it to the clipboard.
5) extract the image from the clipboard and save it as an image file.
 
Conversion Function Code:
Public static void convert00002image (string pdfFilePath, string imageDirectoryPath,
Int beginPageNum, int endPageNum, ImageFormat format, double zoom = 1 ){

Acrobat. CAcroPDDoc invalid Doc = null;
Acrobat. CAcroPDPage pdfPage = null;
Acrobat. CAcroRect pdfRect = null;
Acrobat. CAcroPoint protocol point = null;

// 1)
// Generate the Com object for the Operation PDF File
Export Doc = (Acrobat. CAcroPDDoc) Microsoft. VisualBasic. Interaction. CreateObject ("Export exch. PDDoc ","");

// Check input parameters
If (! Export Doc. Open (pdfFilePath )){
Throw new FileNotFoundException (string. Format ("source file {0} does not exist! ", PdfFilePath ));
}

If (! Directory. Exists (imageDirectoryPath )){
Directory. CreateDirectory (imageDirectoryPath );
}

If (beginPageNum <= 0 ){
BeginPageNum = 1;
}

If (endPageNum> upload Doc. GetNumPages () | endPageNum <= 0 ){
EndPageNum = upload Doc. GetNumPages ();
}

If (beginPageNum> endPageNum ){
Throw new ArgumentException ("Parameter \" beginPageNum \ "must be less than \" endPageNum \"! ");
}

If (format = null ){
Format = ImageFormat. Png;
}

If (zoom <= 0 ){
Zoom = 1;
}

// Conversion
For (int I = beginPageNum; I <= endPageNum; I ++ ){
// 2)
// Retrieve the current page
PdfPage = (Acrobat. CAcroPDPage) invalid Doc. AcquirePage (I-1 );

// 3)
// Get the size of the current page
Export point = (Acrobat. CAcroPoint) pdfPage. GetSize ();
// Generate a rectangle object in the cropping area of a page
PdfRect = (Acrobat. CAcroRect) Microsoft. VisualBasic. Interaction. CreateObject ("batch exch. Rect ","");

// Calculate the actual width and height of the current page after scaling. When zoom = 1, keep the original proportion.
Int imgWidth = (int) (double) destination point. x * zoom );
Int imgHeight = (int) (double) destination point. y * zoom );

// Set the size of the cropped rectangle to the size of the current page.
PdfRect. Left = 0;
PdfRect. right = (short) imgWidth;
PdfRect. Top = 0;
PdfRect. bottom = (short) imgHeight;

// 4)
// Edit the content of the cropping area on the current page into an image and copy it to the clipboard.
PdfPage. CopyToClipboard (pdfRect, 0, 0, (short) (100 * zoom ));

// 5)
IDataObject clipboardData = Clipboard. GetDataObject ();

// Check whether the object in the clipboard is an image. If the object is an image, save it as an image file in the specified format.
If (clipboardData. GetDataPresent (DataFormats. Bitmap )){
Bitmap pdfBitmap = (Bitmap) clipboardData. GetData (DataFormats. Bitmap );

PdfBitmap. Save (
Path. Combine (imageDirectoryPath, I. ToString ("0000") + "." + format. ToString (), format );

PdfBitmap. Dispose ();
}
}

// Close and release related COM objects
Using Doc. Close ();
Marshal. ReleaseComObject (pdfRect );
Marshal. ReleaseComObject (volume point );
Marshal. ReleaseComObject (pdfPage );
Marshal. ReleaseComObject (release DOC );
}

Source code download: http://www.bkjia.com/uploadfile/2012/0410/20120410020111515.rar


From the white ocean

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.