.Net源码论坛 [ 繁体中文 ]

发新话题 回复该主题

PDF,XPS,Images,Office 2007之间的转换方法 [复制链接]

楼主
在实现这些文件格式转换时,使用了第三方的一个组件:Amyuni PDF Creator .NET,Licence好像需要399美刀。这里使用的是30天试用Licence。主要需要是三个dll,在项目中添加对他们的引用就可以了.在B/S应用中部署时可能碰到问题在此blog第二条描述解决办法。
  1. private string licensee
  2.             = "PDF Creator Evaluation license";
  3.         private string activationCode
  4.             = "07EFCDAB0100010048A7F99BCA4506BAE4D4F8695A701B085D550F57C0436768AD20DB28763CE97934AFB3DC8D2228B55AB06E9466AA8A772032BF46FBFAB5";
复制代码
组件提供了部分转换方法,可以将PDF文件通过下面的方法转换成相应的格式: PDF->XPS:
  1.     acPDFCreatorLib.Initialize();
  2.             acPDFCreatorLib.SetLicenseKey(licensee, activationCode);

  3.             System.IO.FileStream pdfFile = new System.IO.FileStream("license.pdf", FileMode.Open);
  4.             IacDocument document = new IacDocument(null);

  5.             //Open the pdf document
  6.             document.Open(pdfFile, "");

  7.             bool flag = document.ExportToXPS("license.xps", IacXPSExportOptions.acXPSExportOptionsNone);

  8.             pdfFile.Close();
  9. XPS->PDF:
  10.             acPDFCreatorLib.Initialize();
  11.             acPDFCreatorLib.SetLicenseKey(licensee, activationCode);

  12.             System.IO.FileStream pdfFile = new System.IO.FileStream("license.xps", FileMode.Open, FileAccess.Read, FileShare.Read);
  13.             IacDocument document = new IacDocument(null);

  14.             //Open the pdf document
  15.             document.Open(pdfFile, "");

  16.             System.IO.FileStream file1 = new System.IO.FileStream("new.pdf", System.IO.FileMode.Create,
  17. System.IO.FileAccess.Write, System.IO.FileShare.Read);

  18.             bool flag = document.Save(file1, IacFileSaveOption.acFileSaveView);

  19.             pdfFile.Close();
  20.             file1.Close();
  21. Image -> PDF:

  22.       private void ConverToPDF(string sourcePath, string targetPath)
  23.         {
  24.             // initialize library and set the license key
  25.             acPDFCreatorLib.Initialize();
  26.             acPDFCreatorLib.SetLicenseKey(licensee, activationCode);

  27.             //creating a new document
  28.             IacDocument document = new IacDocument(null);
  29.             System.IO.FileStream file1 = new System.IO.FileStream(targetPath, System.IO.FileMode.Create,
  30.             System.IO.FileAccess.Write, System.IO.FileShare.Read);
  31.             document.StartSave(file1, Amyuni.PDFCreator.IacFileSaveOption.acFileSaveView);

  32.             int pageIndex = 1;

  33.             //create a picture object on each page
  34.             IacPage page = document.GetPage(pageIndex);
  35.             String objectName = "image";
  36.             Amyuni.PDFCreator.IacObject picture = page.CreateObject(IacObjectType.acObjectTypePicture, objectName);

  37.             //position the picture
  38.             int WidthInPixels = page.GetPageFormat().Width;
  39.             int HeightInPixels = page.GetPageFormat().Length;
  40.             
  41.             Image IMG = Image.FromFile(sourcePath);
  42.             GraphicsUnit GU = GraphicsUnit.Millimeter;
  43.             RectangleF Bounds = IMG.GetBounds(ref GU);
  44.             if (WidthInPixels > Bounds.Width * 10)
  45.             {
  46.                 WidthInPixels = (int)Bounds.Width;
  47.             }
  48.             HeightInPixels = (int)((WidthInPixels / Bounds.Width * 10) * Bounds.Height * 10);

  49.             picture.Attribute("Right").Value = WidthInPixels;
  50.             picture.Attribute("Bottom").Value = HeightInPixels;
  51.             picture.Attribute("FileName").Value = sourcePath;

  52.             //insert a bookmark that points to this page
  53.             document.RootBookmark.InsertChild(pageIndex, objectName, "image");
  54.             //save the current page
  55.             document.SavePageNumber(pageIndex);
  56.             //clear the page contents to save memory
  57.             page.Clear();
  58.             // add a new page after the current one
  59.             document.AddPage(pageIndex);

  60.             //close the pdf document
  61.             document.EndSave();
  62.         }
复制代码
Office 2007文件格式转换为PDF和XPS格式的方法也比较简单,微软提供了一个转换为XPS和PDF的AddIn,安装后调用Office类库可以实现转换,MSDN上有相应的代码
分享 转发
从此我不再仰脸看青天,不再低头看白水,只谨慎着我双双的脚步,我要一不一不踏在泥土上,打上深深的脚印!
TOP
沙发

好!解释的真的是挺全的24
TOP
发新话题 回复该主题