印刷処理( 文字列 )

  VB + Framework



prtDialog.ShowDialog() で印刷ダイアログが表示されます
AddHandler ステートメントでイベントを登録しています

  
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Windows.Forms

Module App

' ********************************************************
' * 変数
' ********************************************************
Private printFont As Font
Private prtDialog As new PrintDialog
Private printPageMax As Integer = 2
Private rowCount As Integer = 0

' ********************************************************
' * 実行
' ********************************************************
Sub Main()

	Dim pd As New PrintDocument

	prtDialog.PrinterSettings = New PrinterSettings
	prtDialog.Document = pd
	If prtDialog.ShowDialog() = DialogResult.OK Then
		printFont = New Font("HG創英角ポップ体", 12)
		AddHandler pd.PrintPage, AddressOf pd_PrintPage
		pd.Print()
	End If
	prtDialog.Dispose()

End Sub

Sub pd_PrintPage(sender As Object, ev As PrintPageEventArgs)

	Dim linesPerPage As Integer = 0
	Dim yPos As Integer = 0
	Dim leftMargin As Integer = ev.MarginBounds.Left
	Dim topMargin As Integer = ev.MarginBounds.Top
	Dim I as Integer

	linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)

	For I = 1 to linesPerPage
		rowCount += 1
		yPos = topMargin + (I-1) * printFont.GetHeight(ev.Graphics)
		ev.Graphics.DrawString("行" & rowCount, printFont, _
			Brushes.Black, leftMargin, yPos, New StringFormat())
	Next

	printPageMax -= 1

	if printPageMax = 0 then
		' 次ページ無し
		ev.HasMorePages = False
	else
		' 次ページ有り
		ev.HasMorePages = True
	end if

End Sub

End Module
  
↓参考
PrintDocument クラス
http://www.microsoft.com/japan/msdn/vbasic/migration/tips/Print/






  C#



PrintPageEventHandler デリゲートのインスタンスをイベントに追加しています

  
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

// ********************************************************
// 実行
// ********************************************************
public class App
{

// ********************************************************
// 変数
// ********************************************************
private Font printFont;
private PrintDialog prtDialog = new PrintDialog();
private int printPageMax = 2;
private int rowCount = 0;

	public static void Main() {

		new App().printTest();

	}

	private void printTest() {
		
		PrintDocument pd = new PrintDocument();

		prtDialog.PrinterSettings = new PrinterSettings();
		prtDialog.Document = pd;

		if ( prtDialog.ShowDialog() == DialogResult.OK ) {
			printFont = new Font("HG創英角ポップ体", 12);
			pd.PrintPage += 
				new PrintPageEventHandler(pd_PrintPage);
			pd.Print();
		}
		prtDialog.Dispose();
	}

	private void pd_PrintPage(object sender, PrintPageEventArgs ev) {

		int linesPerPage = 0;
		int yPos = 0;
		int leftMargin = ev.MarginBounds.Left;
		int topMargin = ev.MarginBounds.Top;

		linesPerPage = 
			(int)(ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics));

		for( int i = 1; i <= linesPerPage; i++ ) {
			rowCount += 1;
			yPos = (int)(topMargin + (i-1) * printFont.GetHeight(ev.Graphics));
			ev.Graphics.DrawString(
				String.Format("{0}{1}","行",rowCount),
				printFont,
				Brushes.Black, 
				leftMargin,
				yPos,
				new StringFormat());
		}
	
		printPageMax -= 1;
	
		if ( printPageMax == 0 ) {
			// 次ページ無し
			ev.HasMorePages = false;
		}
		else {
			// 次ページ有り
			ev.HasMorePages = true;
		}
	}
}
  




  Java

print メソッドが、複数回同じページで呼ばれるので注意。
集計処理等は、一回だけ実行するようにする。
( 印刷処理は、何故か全て実行しないと印字されない )

  
import java.awt.*;
import java.awt.print.*; 

public class print implements Printable
{
	
	public static void main(String[] args) {

		PrinterJob job = PrinterJob.getPrinterJob();

		// インスタンス作成 
		job.setPrintable(new print());
		// 印刷ダイアログ表示      
		if (job.printDialog()) { 	 
			try {
				// 印刷処理の呼び出し
				job.print();
			} 
			catch (Exception e) {
			} 
		}
	}

	public int print(Graphics g, PageFormat pf, int pageIndex) 
		throws PrinterException  {

		if ( pageIndex == 2 ) {
			// 次ページ無し
			return Printable.NO_SUCH_PAGE;
		}

		int linesPerPage = 0;
		int yPos = 0;
		int leftMargin = (int)((pf.getWidth()-pf.getImageableWidth())/2);
		int topMargin = (int)((pf.getHeight()-pf.getImageableHeight())/2) + 20;

		int ch = g.getFontMetrics(
			new Font( "HG創英角ポップ体", Font.PLAIN, 12) ).getHeight();
		linesPerPage = 
			(int)(pf.getImageableHeight() / ch);

		for( int i = 1; i <= linesPerPage; i++ ) {
			yPos = (int)((i-1) * ch);
			g.setColor(Color.BLACK);
			g.setFont(new Font( "HG創英角ポップ体", Font.PLAIN, 12) );
			g.drawString("行" + (pageIndex*linesPerPage+i),
				leftMargin, topMargin+yPos);
		}

		return Printable.PAGE_EXISTS;

	}

}
  

↓参考
http://java.sun.com/j2se/1.5.0/ja/docs/ja/guide/2d/spec/j2d-print.html
http://java.sun.com/j2se/1.5.0/ja/docs/ja/api/java/awt/print/Printable.html




  C++ API

  
#include <windows.h>
#include <stdio.h>

HDC hDC = NULL;
HANDLE hDevMode = NULL;
HANDLE hDevNames = NULL;

LONG nOrgPageWidth;
LONG nOrgPageLength;
LONG nPageWidth;
LONG nPageLength;
LONG nPageOffsetWidth;
LONG nPageOffset;
LONG nCharWidth;
LONG nLinePitch;
LONG nLines;
HFONT hFont = NULL;
HFONT hFontOld = NULL;

inline void CLEAR_HANDLE() {
	if ( hDevMode != NULL ) {
		GlobalFree( hDevMode );
	}
	if ( hDevNames != NULL ) {
		GlobalFree( hDevNames );
	}
	if ( hDC != NULL ) {
		DeleteDC( hDC );
	}
	if ( hFontOld != NULL ) {
		SelectObject(hDC, hFontOld);
		DeleteObject(hFont);
	}
};

int main() {

	PRINTDLG pd;
 	TEXTMETRIC tm;
	DOCINFO doc;
	BOOL bRet;

	// 印刷ダイアログを表示する為の準備
	ZeroMemory( &pd, sizeof( PRINTDLG ) );
	pd.lStructSize = sizeof( PRINTDLG );
	pd.Flags = PD_RETURNDC;
	pd.hwndOwner = GetDesktopWindow();

	// 印刷ダイアログを表示
	bRet = PrintDlg( &pd );
	if ( !bRet ) {
		return false;
	}

	// デバイスコンテキスト
	hDC = pd.hDC;

	// その他ハンドル( ここでは使わない )
	hDevMode = pd.hDevMode;
	hDevNames = pd.hDevNames;

	// ページフォントを決定
	int cHeight;

	cHeight = -MulDiv(18, GetDeviceCaps(hDC, LOGPIXELSY), 72);
	hFont = CreateFont(
		cHeight,
		0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0x01 | 0x30,
		"HG創英角ポップ体");
	hFontOld = (HFONT)SelectObject(hDC, hFont);


	// 物理幅
	nOrgPageWidth
		= GetDeviceCaps(pd.hDC, PHYSICALWIDTH);
	// 物理高さ
	nOrgPageLength
		= GetDeviceCaps(pd.hDC, PHYSICALHEIGHT);
	// 印字可能領域までの位置
	nPageOffsetWidth
		= GetDeviceCaps(pd.hDC, PHYSICALOFFSETX);
	nPageOffset
		= GetDeviceCaps(pd.hDC, PHYSICALOFFSETY);
	// 印字可能ページ長
	nPageLength 
		= nOrgPageLength
		- nPageOffsetWidth * 2;

	GetTextMetrics(pd.hDC, &tm);
	// 平均文字幅
	nCharWidth = tm.tmAveCharWidth;
	// 文字高さ
	nLinePitch = tm.tmHeight;
	// 印字可能幅
	nPageWidth
		= nOrgPageWidth
		- nPageOffsetWidth * 2
		- nCharWidth;

	// ページあたりの行数
	nLines = nPageLength / nLinePitch - 1;
	// 実際のページ長
	nPageLength = nLinePitch * nLines;

	// 印刷開始準備
	ZeroMemory( &doc, sizeof( &doc ) );
	doc.cbSize = sizeof( &doc );
	doc.lpszDocName = "lightbox ドキュメント";
	doc.lpszOutput = NULL;
	doc.lpszDatatype = NULL;

	// 印刷開始
	int nRet;

	nRet = StartDoc( hDC, &doc );
	if ( nRet <= 0 ) {
		CLEAR_HANDLE();
		return 0;
	}

	int i,page,yPos;
	int rowCount;
	char buff[80];
	i = 0;
	page = 0;
	rowCount = 0;

	// ページ印刷
	for( page = 0; page < 2; page++ ) {
		StartPage( hDC );
		for( i = 1; i <= nLines; i++ ) {
			rowCount += 1;
			yPos = (int)(nPageOffset + (i-1) * nLinePitch);

			wsprintf( buff, "%d 行", i );

			TextOut( 
				hDC,
				0,
				yPos,
				buff,
				lstrlen(buff)
			);

		}
		EndPage( hDC );
	}

	// 印刷終了
	EndDoc( hDC );

	CLEAR_HANDLE();

	return 0;
}
  

↓build.bat
  
cmd.exe /c vc8.bat print
  

↓vc8.bat ( Visual C++ 2005 Express Edition with SDK )
  
Set AddPath=C:\Program Files\Microsoft Visual Studio 8\Common7\IDE
Set PATH=%AddPath%;%PATH%
Set CL8="C:\Program Files\Microsoft Visual Studio 8\VC\bin\cl.exe"
Set LINK8="C:\Program Files\Microsoft Visual Studio 8\VC\bin\link.exe"
Set INC81="C:\Program Files\Microsoft Visual Studio 8\VC\include"
Set INC82="C:\Program Files\Microsoft Platform SDK\Include"
Set INC83="C:\Program Files\Microsoft Platform SDK\Include\atl"
Set LIB81="C:\Program Files\Microsoft Visual Studio 8\VC\lib"
Set LIB82="C:\Program Files\Microsoft Platform SDK\Lib"
Set LIBS1=kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib
Set LIBS2=shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
%CL8% %1.cpp /c /I%INC81% /I%INC82% /I%INC83%
%LINK8% /LIBPATH:%LIB81% /LIBPATH:%LIB82% %1.obj %LIBS1% %LIBS2%
  



  PHP で PDF 出力

実行
  
php.exe print_pdf.php
  

print_pdf.php
  
<?php
# **********************************************************
# FPDF を使用しています
# **********************************************************
require("http://homepage2.nifty.com/lightbox/phppdf/japanese.php");

$GLOBALS['margin'] = 5;

# ポートレイト、単位(ミリメートル)、サイズA4
$pdf = new PDF_Japanese( 'P', 'mm', 'A4' );

# 使えそうなマシンにインストール済みのフォント名
# ( GIMP で調べました )
$pdf->AddSJISFont("HGSoeiKakupoptai");

# 2ページぶんの処理
for( $page = 0; $page < 2; $page++ ) {

	# ページ追加
	$pdf->AddPage();
	# 塗りつぶす為の色
	$pdf->SetFillColor( 200, 230, 185 );

	# タイトル印字
	$pdf->SetFont('SJIS','B',20);
	$text = 'PHP 印字テスト';
	$pdf->Text( $GLOBALS['margin'], 13.5, $text );

	# 通常印字フォント
	$pdf->SetFont('SJIS','',10);

	for( $i = 1; $i <= 20; $i++ ) {

		$pdf->SetX( $GLOBALS['margin'] );
		$pdf->SetY( ( $i-1 ) * 8 + 40 );
		$pdf->SetTextColor( 0, 0, 0 );
		# 幅、高さ、印刷文字列、罫線あり、不要、センタリング、色を塗る
		$pdf->Cell( 100, 6, "$i 行", 1, 0, 'C', 1 );

	}
}

# 書き込み
$pdf->Output("print.pdf");

# **********************************************************
# Windows 経由の 外部実行
# **********************************************************
$WshShell = new COM("WScript.Shell");
$command = "RunDLL32.EXE shell32.dll,ShellExec_RunDLL ";
$command .= "\"print.pdf\"";
$WshShell->Run( $command, 1, TRUE );

print "処理が終了しました\n";

?>
  



  PHP + PECL

↓モジュールダウンロード
http://pecl.php.net/package/printer

↓実行
  
php print.php
  

↓print.php

  
<?php
# 仮想プリンタドライバ( PDF 化 )
$handle = printer_open("PrimoPDF");

# 1ページに40行として設定
$mm = printer_get_option($handle, PRINTER_PAPER_LENGTH);
$inch = 0.0393 * $mm;
$dot = printer_get_option($handle, PRINTER_RESOLUTION_Y) * $inch;
$dotperline = $dot / 40;

# フォント作成
$font = printer_create_font(
	"HG創英角ポップ体", 
	$dotperline, 
	$dotperline/2, 
	PRINTER_FW_NORMAL, false, false, false, 0);

# フォント選択
printer_select_font($handle, $font);

# 印刷開始
printer_start_doc($handle, "lightbox ドキュメント");

# 2ページ印字
for( $page = 1; $page <= 2; $page++ ) {

	# ページ開始
	printer_start_page($handle);

	# 文字列印字
	for( $i = 1; $i <= 40; $i++ ) {
		printer_draw_text($handle, "$i 行", 10, ($i-1) * $dotperline);
	}

	# ページ終了
	printer_end_page($handle);

}

printer_end_doc($handle);

printer_delete_font($font);
printer_close($handle);
?>
  



  Jscript.NET

Jscript.NET は、中間結果が整数でない場合は常にparseInt しておかないとエラーになります

↓はビルド時にエラー
  
var a : int;

a = 1/3;
  

継承して、イベント用メソッドをオーバーライドします ( PrintDocument )

  
import System;
import System.Drawing.Printing;
import System.Windows.Forms;

var printFont : System.Drawing.Font;
var prtDialog : PrintDialog = new PrintDialog();
var printPageMax : int = 2;
var rowCount : int = 0;

var pd : myPrint = new myPrint();

prtDialog.PrinterSettings = new PrinterSettings();
prtDialog.Document = pd;
if ( prtDialog.ShowDialog() == DialogResult.OK ) {
	printFont = new System.Drawing.Font("HG創英角ポップ体", 12);
	pd.Print();
}
prtDialog.Dispose();


class myPrint extends PrintDocument {

	protected function OnPrintPage( ev : PrintPageEventArgs ) {
	
		var linesPerPage : int = 0;
		var yPos : int = 0;
		var leftMargin : int = ev.MarginBounds.Left;
		var topMargin : int  = ev.MarginBounds.Top;
		var I : int;

		linesPerPage =
			parseInt(ev.MarginBounds.Height
			/ parseInt(printFont.GetHeight(ev.Graphics).ToString()));

		for ( I = 1; I <= linesPerPage; I++ ) {
			rowCount++;
			yPos = topMargin + (I-1) * 
				parseInt(printFont.GetHeight(ev.Graphics));
			ev.Graphics.DrawString("行" + rowCount, printFont,
				System.Drawing.Brushes.Black,
				leftMargin,
				yPos,
				new System.Drawing.StringFormat());
		}
	
		printPageMax -= 1;
	
		if ( printPageMax == 0 ) {
			// 次ページ無し
			ev.HasMorePages = false;
		}
		else {
			// 次ページ有り
			ev.HasMorePages = true;
		}
	
	}

}
  











  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ