作成日: 2018/03/16 最終更新日: 2018/03/16
文書種別
使用方法
詳細
ComponentOne Studio for WinForms収録の1種類のコンポーネントだけで、ExcelファイルをPDFへ変換することはできません。
ただし、本製品収録のいくつかのコンポーネントを組み合わせることで、この変換を行うことができます。
ここでは、Excel for .NETとPDF for .NETを使用してPDFファイルへ変換を行う方法をご紹介します。
この2つはいずれもUIを持たないコンポーネント製品であるため、すべてコーディングによって制御する必要がありますが、DrawStringやDrawImageなど、C1PdfDocumentの数多くのDrawXXXXメソッドを用いて細かく制御しながらPDFを作成できます。
実際の手順は、以下のようになります。
1. 新規Windpowsフォームプロジェクトを作成し、ツールボックスからC1XLBookコントロールをフォームに追加します。
2. C1XLBookのLoadメソッドで既存Excelファイルをロードします。
3. C1XLBookのシート上の画像をXLPictureShapeオブジェクトとして保存します。
4. フォーム上にC1PdfDocumentをコントロールを貼り付けます。
5. C1PdfDocumentのDrawStringメソッドメソッドを用いて、C1XLBookのシート上の各セルテキストを描画します。
6. C1PdfDocumentにDrawImageメソッドを用いて、C1XLBookのシート上の画像を描画します。
7. C1PdfDocumentにDrawLineメソッドを用いて、グリッド線を描画します。
※以上により、Excelファイルのコンテンツをグリッド形式で描画したC1PdfDocumentが設定されます。
8. C1PdfDocumentのSaveメソッドを用いて、これをPDFファイルとして保存します。
※下記サンプルコードでは、既存Excelファイルが以下のような内容になっていると仮定しています。
1行目 2行目 3行目
------------------------------------
1 AAAA 任意の画像
2 BBBB
3 CCCC
4 DDDD
------------------------------------
◎サンプルコード(VB)
◎サンプルコード(C#)
※ご注意
1) C1XLBookのLoadメソッドで既存のExcelファイルをロードするとき、オートシェイプやグラフなど、保持されない設定があります。詳細は、以下のナレッジ:80534を参照してください。
2) PDFへ出力する際、各コンテンツの描画位置、フォントサイズやスタイル、グリッドの枠線など、すべて手動で設定する必要があります。
ただし、本製品収録のいくつかのコンポーネントを組み合わせることで、この変換を行うことができます。
ここでは、Excel for .NETとPDF for .NETを使用してPDFファイルへ変換を行う方法をご紹介します。
この2つはいずれもUIを持たないコンポーネント製品であるため、すべてコーディングによって制御する必要がありますが、DrawStringやDrawImageなど、C1PdfDocumentの数多くのDrawXXXXメソッドを用いて細かく制御しながらPDFを作成できます。
実際の手順は、以下のようになります。
1. 新規Windpowsフォームプロジェクトを作成し、ツールボックスからC1XLBookコントロールをフォームに追加します。
2. C1XLBookのLoadメソッドで既存Excelファイルをロードします。
3. C1XLBookのシート上の画像をXLPictureShapeオブジェクトとして保存します。
4. フォーム上にC1PdfDocumentをコントロールを貼り付けます。
5. C1PdfDocumentのDrawStringメソッドメソッドを用いて、C1XLBookのシート上の各セルテキストを描画します。
6. C1PdfDocumentにDrawImageメソッドを用いて、C1XLBookのシート上の画像を描画します。
7. C1PdfDocumentにDrawLineメソッドを用いて、グリッド線を描画します。
※以上により、Excelファイルのコンテンツをグリッド形式で描画したC1PdfDocumentが設定されます。
8. C1PdfDocumentのSaveメソッドを用いて、これをPDFファイルとして保存します。
※下記サンプルコードでは、既存Excelファイルが以下のような内容になっていると仮定しています。
1行目 2行目 3行目
------------------------------------
1 AAAA 任意の画像
2 BBBB
3 CCCC
4 DDDD
------------------------------------
◎サンプルコード(VB)
C1XLBook1.Load("..¥..¥TestBook.xlsx")
Dim sheet As XLSheet = C1XLBook1.Sheets(0)
Dim shap1 As XLPictureShape = CType(sheet.Shapes(0), XLPictureShape)
Dim font1 As Font = New Font("MS Gothic", 10, FontStyle.Bold)
Dim font2 As Font = New Font("MS Gothic", 10)
C1PdfDocument1.DrawString(sheet(0, 0).Text, font1, Brushes.Black, New Point(10, 20))
C1PdfDocument1.DrawString(sheet(1, 0).Text, font2, Brushes.Black, New Point(10, 40))
C1PdfDocument1.DrawString(sheet(2, 0).Text, font2, Brushes.Black, New Point(10, 60))
C1PdfDocument1.DrawString(sheet(3, 0).Text, font2, Brushes.Black, New Point(10, 80))
C1PdfDocument1.DrawString(sheet(0, 1).Text, font1, Brushes.Black, New Point(180, 20))
C1PdfDocument1.DrawString(sheet(1, 1).Text, font2, Brushes.Black, New Point(180, 40))
C1PdfDocument1.DrawString(sheet(2, 1).Text, font2, Brushes.Black, New Point(180, 60))
C1PdfDocument1.DrawString(sheet(3, 1).Text, font2, Brushes.Black, New Point(180, 80))
C1PdfDocument1.DrawImage(shap1.Image, New RectangleF(400, 20, 150, 100))
Dim pen1 As Pen = New Pen(Color.DarkGray)
C1PdfDocument1.DrawLine(pen1, 5, 15, 300, 15)
C1PdfDocument1.DrawLine(pen1, 5, 35, 300, 35)
C1PdfDocument1.DrawLine(pen1, 5, 55, 300, 55)
C1PdfDocument1.DrawLine(pen1, 5, 75, 300, 75)
C1PdfDocument1.DrawLine(pen1, 5, 95, 300, 95)
C1PdfDocument1.DrawLine(pen1, 5, 15, 5, 95)
C1PdfDocument1.DrawLine(pen1, 100, 15, 100, 95)
C1PdfDocument1.DrawLine(pen1, 300, 15, 300, 95)
C1PdfDocument1.Save("..¥..¥ExceltoPdf2.pdf")
System.Diagnostics.Process.Start("..¥..¥ExceltoPdf2.pdf")
Dim sheet As XLSheet = C1XLBook1.Sheets(0)
Dim shap1 As XLPictureShape = CType(sheet.Shapes(0), XLPictureShape)
Dim font1 As Font = New Font("MS Gothic", 10, FontStyle.Bold)
Dim font2 As Font = New Font("MS Gothic", 10)
C1PdfDocument1.DrawString(sheet(0, 0).Text, font1, Brushes.Black, New Point(10, 20))
C1PdfDocument1.DrawString(sheet(1, 0).Text, font2, Brushes.Black, New Point(10, 40))
C1PdfDocument1.DrawString(sheet(2, 0).Text, font2, Brushes.Black, New Point(10, 60))
C1PdfDocument1.DrawString(sheet(3, 0).Text, font2, Brushes.Black, New Point(10, 80))
C1PdfDocument1.DrawString(sheet(0, 1).Text, font1, Brushes.Black, New Point(180, 20))
C1PdfDocument1.DrawString(sheet(1, 1).Text, font2, Brushes.Black, New Point(180, 40))
C1PdfDocument1.DrawString(sheet(2, 1).Text, font2, Brushes.Black, New Point(180, 60))
C1PdfDocument1.DrawString(sheet(3, 1).Text, font2, Brushes.Black, New Point(180, 80))
C1PdfDocument1.DrawImage(shap1.Image, New RectangleF(400, 20, 150, 100))
Dim pen1 As Pen = New Pen(Color.DarkGray)
C1PdfDocument1.DrawLine(pen1, 5, 15, 300, 15)
C1PdfDocument1.DrawLine(pen1, 5, 35, 300, 35)
C1PdfDocument1.DrawLine(pen1, 5, 55, 300, 55)
C1PdfDocument1.DrawLine(pen1, 5, 75, 300, 75)
C1PdfDocument1.DrawLine(pen1, 5, 95, 300, 95)
C1PdfDocument1.DrawLine(pen1, 5, 15, 5, 95)
C1PdfDocument1.DrawLine(pen1, 100, 15, 100, 95)
C1PdfDocument1.DrawLine(pen1, 300, 15, 300, 95)
C1PdfDocument1.Save("..¥..¥ExceltoPdf2.pdf")
System.Diagnostics.Process.Start("..¥..¥ExceltoPdf2.pdf")
◎サンプルコード(C#)
c1XLBook1.Load(@"..¥..¥TestBook.xlsx");
XLSheet sheet = c1XLBook1.Sheets[0];
XLPictureShape shap1 = (XLPictureShape)sheet.Shapes[0];
Font font1 = new Font("MS Gothic", 10, FontStyle.Bold);
Font font2 = new Font("MS Gothic", 10);
c1PdfDocument1.DrawString(sheet[0, 0].Text, font1, Brushes.Black, new Point(10, 20));
c1PdfDocument1.DrawString(sheet[1, 0].Text, font2, Brushes.Black, new Point(10, 40));
c1PdfDocument1.DrawString(sheet[2, 0].Text, font2, Brushes.Black, new Point(10, 60));
c1PdfDocument1.DrawString(sheet[3, 0].Text, font2, Brushes.Black, new Point(10, 80));
c1PdfDocument1.DrawString(sheet[0, 1].Text, font1, Brushes.Black, new Point(180, 20));
c1PdfDocument1.DrawString(sheet[1, 1].Text, font2, Brushes.Black, new Point(180, 40));
c1PdfDocument1.DrawString(sheet[2, 1].Text, font2, Brushes.Black, new Point(180, 60));
c1PdfDocument1.DrawString(sheet[3, 1].Text, font2, Brushes.Black, new Point(180, 80));
c1PdfDocument1.DrawImage(shap1.Image, new RectangleF(400, 20, 150, 100));
Pen pen1 = new Pen(Color.DarkGray);
c1PdfDocument1.DrawLine(pen1, 5, 15, 300, 15);
c1PdfDocument1.DrawLine(pen1, 5, 35, 300, 35);
c1PdfDocument1.DrawLine(pen1, 5, 55, 300, 55);
c1PdfDocument1.DrawLine(pen1, 5, 75, 300, 75);
c1PdfDocument1.DrawLine(pen1, 5, 95, 300, 95);
c1PdfDocument1.DrawLine(pen1, 5, 15, 5, 95);
c1PdfDocument1.DrawLine(pen1, 100, 15, 100, 95);
c1PdfDocument1.DrawLine(pen1, 300, 15, 300, 95);
c1PdfDocument1.Save(@"..¥..¥ExceltoPdf2.pdf");
System.Diagnostics.Process.Start(@"..¥..¥ExceltoPdf2.pdf");
XLSheet sheet = c1XLBook1.Sheets[0];
XLPictureShape shap1 = (XLPictureShape)sheet.Shapes[0];
Font font1 = new Font("MS Gothic", 10, FontStyle.Bold);
Font font2 = new Font("MS Gothic", 10);
c1PdfDocument1.DrawString(sheet[0, 0].Text, font1, Brushes.Black, new Point(10, 20));
c1PdfDocument1.DrawString(sheet[1, 0].Text, font2, Brushes.Black, new Point(10, 40));
c1PdfDocument1.DrawString(sheet[2, 0].Text, font2, Brushes.Black, new Point(10, 60));
c1PdfDocument1.DrawString(sheet[3, 0].Text, font2, Brushes.Black, new Point(10, 80));
c1PdfDocument1.DrawString(sheet[0, 1].Text, font1, Brushes.Black, new Point(180, 20));
c1PdfDocument1.DrawString(sheet[1, 1].Text, font2, Brushes.Black, new Point(180, 40));
c1PdfDocument1.DrawString(sheet[2, 1].Text, font2, Brushes.Black, new Point(180, 60));
c1PdfDocument1.DrawString(sheet[3, 1].Text, font2, Brushes.Black, new Point(180, 80));
c1PdfDocument1.DrawImage(shap1.Image, new RectangleF(400, 20, 150, 100));
Pen pen1 = new Pen(Color.DarkGray);
c1PdfDocument1.DrawLine(pen1, 5, 15, 300, 15);
c1PdfDocument1.DrawLine(pen1, 5, 35, 300, 35);
c1PdfDocument1.DrawLine(pen1, 5, 55, 300, 55);
c1PdfDocument1.DrawLine(pen1, 5, 75, 300, 75);
c1PdfDocument1.DrawLine(pen1, 5, 95, 300, 95);
c1PdfDocument1.DrawLine(pen1, 5, 15, 5, 95);
c1PdfDocument1.DrawLine(pen1, 100, 15, 100, 95);
c1PdfDocument1.DrawLine(pen1, 300, 15, 300, 95);
c1PdfDocument1.Save(@"..¥..¥ExceltoPdf2.pdf");
System.Diagnostics.Process.Start(@"..¥..¥ExceltoPdf2.pdf");
※ご注意
1) C1XLBookのLoadメソッドで既存のExcelファイルをロードするとき、オートシェイプやグラフなど、保持されない設定があります。詳細は、以下のナレッジ:80534を参照してください。
2) PDFへ出力する際、各コンテンツの描画位置、フォントサイズやスタイル、グリッドの枠線など、すべて手動で設定する必要があります。
関連情報
旧文書番号
82582