您好,登錄后才能下訂單哦!
/// <summary>
/// 將PDF文檔轉換為圖片的方法,你可以像這樣調用該方法:ConvertPDF2Image("F:\\A.pdf", "F:\\", "A", 0, 0, null, 0);
/// 因為大多數的參數都有默認值,startPageNum默認值為1,endPageNum默認值為總頁數,
/// p_w_picpathFormat默認值為ImageFormat.Jpeg,resolution默認值為1
/// </summary>
/// <param name="pdfInputPath">PDF文件路徑</param>
/// <param name="p_w_picpathOutputPath">圖片輸出路徑</param>
/// <param name="p_w_picpathName">圖片的名字,不需要帶擴展名</param>
/// <param name="startPageNum">從PDF文檔的第幾頁開始轉換,默認值為1</param>
/// <param name="endPageNum">從PDF文檔的第幾頁開始停止轉換,默認值為PDF總頁數</param>
/// <param name="p_w_picpathFormat">設置所需圖片格式</param>
/// <param name="resolution">設置圖片的分辨率,數字越大越清晰,默認值為1</param>
publicstaticvoid ConvertPDF2Image(string pdfInputPath, string p_w_picpathOutputPath,
string p_w_picpathName, int startPageNum, int endPageNum, ImageFormat p_w_picpathFormat, double resolution)
{
Acrobat.CAcroPDDoc pdfDoc = null;
Acrobat.CAcroPDPage pdfPage = null;
Acrobat.CAcroRect pdfRect = null;
Acrobat.CAcroPoint pdfPoint = null;
// Create the document (Can only create the AcroExch.PDDoc object using late-binding)
// Note using VisualBasic helper functions, have to add reference to DLL
pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");
// validate parameter
if (!pdfDoc.Open(pdfInputPath)) { thrownew FileNotFoundException(); }
if (!Directory.Exists(p_w_picpathOutputPath)) { Directory.CreateDirectory(p_w_picpathOutputPath); }
if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfDoc.GetNumPages() || endPageNum <= 0) { endPageNum = pdfDoc.GetNumPages(); } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; }
if (p_w_picpathFormat == null) { p_w_picpathFormat = ImageFormat.Jpeg; }
if (resolution <= 0) { resolution = 1; }
// start to convert each page
for (int i = startPageNum; i <= endPageNum; i++)
{
pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i - 1);
pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");
int imgWidth = (int)((double)pdfPoint.x * resolution);
int imgHeight = (int)((double)pdfPoint.y * resolution);
pdfRect.Left = 0;
pdfRect.right = (short)imgWidth;
pdfRect.Top = 0;
pdfRect.bottom = (short)imgHeight;
// Render to clipboard, scaled by 100 percent (ie. original size)
// Even though we want a smaller p_w_picpath, better for us to scale in .NET
// than Acrobat as it would greek out small text
pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100 * resolution));
IDataObject clipboardData = Clipboard.GetDataObject();
if (clipboardData.GetDataPresent(DataFormats.Bitmap))
{
Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
pdfBitmap.Save(Path.Combine(p_w_picpathOutputPath, p_w_picpathName) + ".jpg", p_w_picpathFormat);
pdfBitmap.Dispose();
}
}
pdfDoc.Close();
Marshal.ReleaseComObject(pdfPage);
Marshal.ReleaseComObject(pdfRect);
Marshal.ReleaseComObject(pdfDoc);
Marshal.ReleaseComObject(pdfPoint);
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。