[.NETコントロール] Office 2007文書(Excel、Wordファイル)をLEADTOOLSでサポートしている画像形式に変換できますか?

文書番号 : 32797     文書種別 : 使用方法     登録日 : 2012/05/09     最終更新日 : 2014/11/20
文書を印刷する
対象製品
LEADTOOLS 17.5J Document Imaging
詳細
Microsoft Officeドキュメント形式は、Documentシリーズにて対応しています。
JPEGやPNGファイル形式を読み込むのと同様に、Word(DOC/DOCX)、Excel(XLS/XLSX)、PowerPoint(PPT/PPTX)形式のページをラスタ画像として読み込むことが可能です。
また、LEADTOOLSの機能のみではなく、Microsoft Office アドインで提供している機能を使用して読み込む場合には、下記の内容をご参照ください。

Microsoft Office 2007ではOffice文書をPDF、XPSファイルとして保存するアドインが提供されています。この機能は「Microsoft Office 2007 プライマリ相互運用機能アセンブリ」で使用できますので、このアセンブリを使用して初めにPDFもしくはXPSファイルに変換し、次に変換したPDFもしくはXPSをLEADTOOLSで読み込み、TIFF CCITT G4ファイルに変換する方法が考えられます。


※PDFの読み込みにはLEADTOOLS 17.5J PDF Read Plug-Inが必要です。
※XPSの読み込みにはLEADTOOLS 17.5J Documentシリーズが必要です。

※「Excel 2007 プライマリ相互運用機能アセンブリ」を使用してExcelシートをPDF、XPSファイルに変換する方法につきましては、こちらをご覧下さい。

※「Word 2007 プライマリ相互運用機能アセンブリ」を使用してWord文書をPDF、XPSファイルに変換する方法につきましては、こちらをご覧下さい。

※「Microsoft Office 2007 プライマリ相互運用機能アセンブリ」の再頒布可能パッケージはこちらからダウンロードできます。


以下はExcelファイルをTIFF CCITT G4ファイルに変換するサンプルです。

サンプルコード(VB.NET)
Imports Microsoft.Office.Interop.Excel
Imports Leadtools
Imports Leadtools.Codecs

Public Class Form1
  Dim excelApplication As ApplicationClass = New ApplicationClass()
  Dim excelWorkbook As Workbook = Nothing
  Dim paramSourceBookPath As String = "C:¥Temp¥Test.xlsx"
  Dim paramExportFilePath As String = "C:¥Temp¥Test.pdf"
  Dim paramExportFormat As XlFixedFormatType = XlFixedFormatType.xlTypePDF
  Dim paramExportQuality As XlFixedFormatQuality = XlFixedFormatQuality.xlQualityStandard
  Dim paramOpenAfterPublish As Boolean = False
  Dim paramIncludeDocProps As Boolean = True
  Dim paramIgnorePrintAreas As Boolean = True
  Dim paramFromPage As Object = Type.Missing
  Dim paramToPage As Object = Type.Missing

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
      ' Excelワークブックを開きます。
      excelWorkbook = excelApplication.Workbooks.Open(paramSourceBookPath)

      ' PDFファイルとして保存します。
      If Not excelWorkbook Is Nothing Then
        excelWorkbook.ExportAsFixedFormat(paramExportFormat, _
          paramExportFilePath, paramExportQuality, _
          paramIncludeDocProps, paramIgnorePrintAreas, _
          paramFromPage, paramToPage, paramOpenAfterPublish)
      End If
    Catch ex As Exception
      ' エラーを返します。
    Finally
      ' Excleワークブックを閉じます。
      If Not excelWorkbook Is Nothing Then
        excelWorkbook.Close(False)
        excelWorkbook = Nothing
      End If

      ' Excelを終了し、ApplicationClassオブジェクトを解放します。
      If Not excelApplication Is Nothing Then
        excelApplication.Quit()
        excelApplication = Nothing
      End If

      GC.Collect()
      GC.WaitForPendingFinalizers()
      GC.Collect()
      GC.WaitForPendingFinalizers()
    End Try
  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim codecs As New RasterCodecs
    Dim tempimage As RasterImage

    codecs.Options.Pdf.Load.GraphicsAlpha = 4
    codecs.Options.Pdf.Load.TextAlpha = 4
    codecs.Options.Pdf.Load.DisplayDepth = 24

    Dim rasterizeDocumentLoadOptions As CodecsRasterizeDocumentLoadOptions = codecs.Options.RasterizeDocument.Load
    rasterizeDocumentLoadOptions.XResolution = 150
    rasterizeDocumentLoadOptions.YResolution = 150

    ' PDFファイルを読み込みます。
    tempimage = codecs.Load(paramExportFilePath, 24, CodecsLoadByteOrder.Bgr, 1, 1)

    ' TIFF CCITT G4形式で保存します。
    codecs.Save(tempimage, "C:¥Temp¥Test.tif", RasterImageFormat.CcittGroup4, 0)

  End Sub
End Class

サンプルコード(C#)
using Microsoft.Office.Interop.Excel; 
using Leadtools; 
using Leadtools.Codecs; 

public class Form1 
{ 
  ApplicationClass excelApplication = new ApplicationClass(); 
  Workbook excelWorkbook = null; 
  string paramSourceBookPath = "C:¥¥Temp¥¥Test.xlsx"; 
  string paramExportFilePath = "C:¥¥Temp¥¥Test.pdf"; 
  XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF; 
  XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityStandard; 
  bool paramOpenAfterPublish = false; 
  bool paramIncludeDocProps = true; 
  bool paramIgnorePrintAreas = true; 
  object paramFromPage = Type.Missing; 
  object paramToPage = Type.Missing; 
        
  private void Button1_Click(object sender, System.EventArgs e) 
  { 
    try
    { 
      // Excelワークブックを開きます。
      excelWorkbook = excelApplication.Workbooks.Open(paramSourceBookPath,
          Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
          Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
          Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            
      // PDFファイルとして保存します。
      if ((excelWorkbook != null))
      {
        excelWorkbook.ExportAsFixedFormat(paramExportFormat, paramExportFilePath,
          paramExportQuality, paramIncludeDocProps, paramIgnorePrintAreas,
          paramFromPage, paramToPage, paramOpenAfterPublish,Type.Missing);
      } 
    } 
    catch (Exception ex)
    {
      // エラーを返します。
    } 
    finally
    { 
      // Excleワークブックを閉じます。
      if ((excelWorkbook != null))
      {
        excelWorkbook.Close(false,Type.Missing,Type.Missing);
        excelWorkbook = null;
      } 
                  
      // Excelを終了し、ApplicationClassオブジェクトを解放します。
      if ((excelApplication != null))
      { 
        excelApplication.Quit(); 
        excelApplication = null; 
      } 
            
      GC.Collect(); 
      GC.WaitForPendingFinalizers(); 
      GC.Collect(); 
      GC.WaitForPendingFinalizers(); 
    } 
  } 
    
  private void Button2_Click(object sender, System.EventArgs e) 
  { 
    RasterCodecs codecs = new RasterCodecs(); 
    RasterImage tempimage; 
        
    codecs.Options.Pdf.Load.GraphicsAlpha = 4; 
    codecs.Options.Pdf.Load.TextAlpha = 4; 
    codecs.Options.Pdf.Load.DisplayDepth = 24; 
        
    CodecsRasterizeDocumentLoadOptions rasterizeDocumentLoadOptions = codecs.Options.RasterizeDocument.Load;
    rasterizeDocumentLoadOptions.XResolution = 150;
    rasterizeDocumentLoadOptions.YResolution = 150;
        
    // PDFファイルを読み込みます。        
    tempimage = codecs.Load(paramExportFilePath, 24, CodecsLoadByteOrder.Bgr, 1, 1); 
        
    // TIFF CCITT G4形式で保存します。
    codecs.Save(tempimage, "C:¥¥Temp¥¥Test.tif", RasterImageFormat.CcittGroup4, 0); 
  } 
}

【補足】
ASP.NETでご利用の場合、ファイルの取得/保存先パス(paramSourceBookPath、paramExportFilePath、 codecs.Save)の箇所を、Server.MapPathメソッドを使用して仮想パスに対する物理パスに変更いただければ、その他のコードはそのままご利用いただけます。
------------------------------------------------------------
例:
Dim paramSourceBookPath As String = Server.MapPath("Resources/Test.xlsx")
------------------------------------------------------------
関連情報
キーワード
HowTo 読み込みと保存 Editor