在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → PDF阅读器,使用Adobe的类库将PDF文件转化为JPG文件!然后播放!(需要安装Acrobat)

PDF阅读器,使用Adobe的类库将PDF文件转化为JPG文件!然后播放!(需要安装Acrobat)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.27M
  • 下载次数:21
  • 浏览次数:516
  • 发布时间:2019-02-05
  • 实例类别:C#语言基础
  • 发 布 人:wangsert
  • 文件格式:.zip
  • 所需积分:2
 相关标签: pdf Adobe

实例介绍

【实例简介】PDFThumbnailCSharp

【实例截图】

from clipboard

【核心代码】


using System;
using System.IO;
using System.Drawing;
using System.Configuration;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class PDFThumbnailCsharp
{
	[STAThread]
	static void Main(string[] args)
	{
		// Acrobat objects
		Acrobat.CAcroPDDoc pdfDoc;
		Acrobat.CAcroPDPage pdfPage;
		Acrobat.CAcroRect pdfRect;
		Acrobat.CAcroPoint pdfPoint;

		AppSettingsReader appSettings = new AppSettingsReader();

		string pdfInputPath = appSettings.GetValue("pdfInputPath", typeof(string)).ToString();
		string pngOutputPath = appSettings.GetValue("pngOutputPath", typeof(string)).ToString();

        string templatePortraitFile = Application.StartupPath   @"\pdftemplate_portrait.gif";
		string templateLandscapeFile = Application.StartupPath   @"\pdftemplate_portrait.gif";;

		try
		{
			// Get list of files to process from the input path
            // Could change to read list from database instead
			string[] files = Directory.GetFiles(pdfInputPath, "*.pdf");

			for (int n=0; n < files.Length; n  )
			{
				string inputFile = files[n].ToString();
				string outputFile = pngOutputPath   files[n].Substring(files[n].LastIndexOf(@"\") 1).Replace(".pdf", ".png");

                /* Could skip if thumbnail already exists in output path
                FileInfo fi = new FileInfo(inputFile);
				if (!fi.Exists) {} */

				// Create the document (Can only create the AcroExch.PDDoc object using late-binding)
				// Note using VisualBasic helper functions, have to add reference to DLL in
				// C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Microsoft.VisualBasic.dll
				// Will always be available as .NET framework ships with all
				pdfDoc = (Acrobat.CAcroPDDoc) Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");

				int ret = pdfDoc.Open(inputFile);

				if (ret == 0)
				{
					throw new FileNotFoundException();
				}

				// Get the number of pages (to be used later if you wanted to store that information)
				int pageCount = pdfDoc.GetNumPages();

				// Get the first page
				pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(0);

				pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
				
				pdfRect = (Acrobat.CAcroRect) Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");

				pdfRect.Left = 0;
				pdfRect.right = pdfPoint.x;
				pdfRect.Top = 0;
				pdfRect.bottom = pdfPoint.y;

				// Render to clipboard, scaled by 100 percent (ie. original size)
                // Even though we want a smaller image, better for us to scale in .NET
                // than Acrobat as it would greek out small text
                // see http://www.adobe.com/support/techdocs/1dd72.htm
				pdfPage.CopyToClipboard(pdfRect, 0, 0, 100);

				IDataObject clipboardData = Clipboard.GetDataObject();

				if (clipboardData.GetDataPresent(DataFormats.Bitmap))
				{
					Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);

					// Size of generated thumbnail in pixels
					int thumbnailWidth = 45;
					int thumbnailHeight = 59;

					string templateFile;

					// Switch between portrait and landscape
					if (pdfPoint.x < pdfPoint.y)
					{
						templateFile = templatePortraitFile;
					}
					else
					{
						templateFile = templateLandscapeFile;
						// Swap width and height (little trick not using third temp variable)
						thumbnailWidth = thumbnailWidth ^ thumbnailHeight;
						thumbnailHeight = thumbnailWidth ^ thumbnailHeight;
						thumbnailWidth = thumbnailWidth ^ thumbnailHeight;
					}
					
					// Load the template graphic
					Bitmap templateBitmap = new Bitmap(templateFile);

					// Render to small image using the bitmap class
					Image pdfImage = pdfBitmap.GetThumbnailImage(thumbnailWidth, thumbnailHeight,
																 null, IntPtr.Zero);
					
					// Create new blank bitmap (  7 for template border)						 
					Bitmap thumbnailBitmap = new Bitmap(thumbnailWidth   7, thumbnailHeight   7,
														System.Drawing.Imaging.PixelFormat.Format32bppArgb);

					// To overlayout the template with the image, we need to set the transparency
                    // http://www.sellsbrothers.com/writing/default.aspx?content=dotnetimagerecoloring.htm
					templateBitmap.MakeTransparent();

					using (Graphics thumbnailGraphics = Graphics.FromImage(thumbnailBitmap))
					{
						// Draw rendered pdf image to new blank bitmap
						thumbnailGraphics.DrawImage(pdfImage, 2, 2, thumbnailWidth, thumbnailHeight);

						// Draw template outline over the bitmap (pdf with show through the transparent area)
						thumbnailGraphics.DrawImage(templateBitmap, 0, 0);

						// Save as .png file
						thumbnailBitmap.Save(outputFile, System.Drawing.Imaging.ImageFormat.Png);

						Console.WriteLine("Generated thumbnail... {0}", outputFile);
					}

					pdfDoc.Close();

					// Not sure how why it is to do this, but Acrobat is not the best behaved COM object
					// see http://blogs.msdn.com/yvesdolc/archive/2004/04/17/115379.aspx
					Marshal.ReleaseComObject(pdfPage);
					Marshal.ReleaseComObject(pdfRect);
					Marshal.ReleaseComObject(pdfDoc);
				}
			}
		}
		catch (System.Exception ex)
		{
			Console.Write(ex.ToString());
		}
		
	}
}


标签: pdf Adobe

实例下载地址

PDF阅读器,使用Adobe的类库将PDF文件转化为JPG文件!然后播放!(需要安装Acrobat)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警