www.pudn.com > FaxConvert.zip > FaxConv.PAS


unit Faxconv; 
 
interface 
 
uses 
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, IniFiles, 
  Forms, Dialogs, StdCtrls, FileCtrl, OoMisc, AdFaxCvt, ExtCtrls, AdMeter, 
  Mask, ComCtrls, RichEdit2,jpeg; 
 
type 
  TFaxConversionForm = class(TForm) 
    btnOK: TButton; 
    ApdFaxConverter1: TApdFaxConverter; 
    btnConvert: TButton; 
    GroupBox2: TGroupBox; 
    cbxEnhTextEnabled: TCheckBox; 
    btnEnhFont: TButton; 
    FontDialog1: TFontDialog; 
    Label3: TLabel; 
    Label4: TLabel; 
    rgpResolution: TRadioGroup; 
    RadioGroup2:TRadioGroup; 
    Label5: TLabel; 
    rgpWidth: TRadioGroup; 
    Label6: TLabel; 
    GroupBox3: TGroupBox; 
    cbxCenterImage: TCheckBox; 
    cbxYield: TCheckBox; 
    cbxYieldOften: TCheckBox; 
    lblFontName: TLabel; 
    lblFontSize: TLabel; 
    edtLeftMargin: TMaskEdit; 
    edtTopMargin: TMaskEdit; 
    edtLinesPerPage: TMaskEdit; 
    edtTabStop: TMaskEdit; 
    GroupBox1: TGroupBox; 
    Label2: TLabel; 
    edtOutFileName: TEdit; 
    Label1: TLabel; 
    edtFileName: TEdit; 
    btnBrowseInFile: TButton; 
    Button2: TButton; 
    OpenDialog1: TOpenDialog; 
    RadioGroup1: TRadioGroup; 
    Label7: TLabel; 
    CheckBox1: TCheckBox; 
    RichEdit981: TRichEdit98; 
    RichEdit982: TRichEdit98; 
    Image2: TImage; 
    Image1: TImage; 
    procedure FormCreate(Sender: TObject); 
    procedure btnConvertClick(Sender: TObject); 
    procedure ApdFaxConverter1Status(F: TObject; Starting, Ending: Boolean; 
      PagesConverted, LinesConverted: Integer; BytesConverted, 
      BytesToConvert: Longint; var Abort: Boolean); 
    procedure btnEnhFontClick(Sender: TObject); 
    procedure FormClose(Sender: TObject; var Action: TCloseAction); 
    procedure cbxCenterImageClick(Sender: TObject); 
    procedure cbxYieldClick(Sender: TObject); 
    procedure cbxYieldOftenClick(Sender: TObject); 
    procedure rgpResolutionClick(Sender: TObject); 
    procedure rgpWidthClick(Sender: TObject); 
    procedure edtLeftMarginExit(Sender: TObject); 
    procedure edtTopMarginExit(Sender: TObject); 
    procedure edtLinesPerPageExit(Sender: TObject); 
    procedure edtTabStopExit(Sender: TObject); 
    procedure btnBrowseInFileClick(Sender: TObject); 
    procedure Button2Click(Sender: TObject); 
    procedure RadioGroup2Click(Sender: TObject); 
    procedure edtFileNameExit(Sender: TObject); 
    procedure btnOKClick(Sender: TObject); 
  private 
    { Private declarations } 
    Meter: TApdMeter; 
  public 
    { Public declarations } 
    Cancelled : Boolean; 
    LastInDir, 
    LastOutDir: String; 
  end; 
 
const 
  IniName = 'TCom3.ini'; 
  crHand = 1; 
var 
  FaxConversionForm: TFaxConversionForm; 
                            //选择内容 
implementation 
 
uses Faxcnvid, RotateUnit,CovertBmp, FDConvrt; 
 
{$R *.DFM} 
 
procedure TFaxConversionForm.FormCreate(Sender: TObject); 
var 
  Ini: TIniFile; 
begin 
  Meter := TApdMeter.Create(Self); 
  Meter.Parent := Self; 
  Meter.Top := btnConvert.Top; 
  Meter.Left := GroupBox1.Left; 
  Meter.Width := btnConvert.Left - GroupBox1.Left - 8; 
  Meter.Height := btnConvert.Height; 
  Meter.Visible := True; 
  Cancelled := False; 
  Ini := TIniFile.Create(IniName); 
  try 
    LastInDir := Ini.ReadString('FaxConvert', 'LastInDir', ExtractFilePath(Application.ExeName)); 
    LastOutDir := Ini.ReadString('FaxConvert', 'LastOutDir', ExtractFilePath(Application.ExeName)); 
    cbxEnhTextEnabled.Checked := Ini.ReadBool('FaxConvert', 'EnhTextEnabled', True); 
    ApdFaxConverter1.EnhFont.Name := Ini.ReadString('FaxConvert', 'EnhFont.Name', Font.Name); 
    ApdFaxConverter1.EnhFont.Size := Ini.ReadInteger('FaxConvert', 'EnhFont.Size', Font.Size); 
    ApdFaxConverter1.LeftMargin := Ini.ReadInteger('FaxConvert', 'LeftMargin', 50); 
    ApdFaxConverter1.TopMargin := Ini.ReadInteger('FaxConvert', 'TopMargin', 0); 
    ApdFaxConverter1.LinesPerPage := Ini.ReadInteger('FaxConvert', 'LinesPerPage', 60); 
    ApdFaxConverter1.TabStop := Ini.ReadInteger('FaxConvert', 'TabStop', 4); 
    ApdFaxConverter1.Options := []; 
    if Ini.ReadBool('FaxConvert', 'Options.DoubleWidth', True) then 
      ApdFaxConverter1.Options := ApdFaxConverter1.Options + [coDoubleWidth]; 
    if Ini.ReadBool('FaxConvert', 'Options.HalfHeight', False) then 
      ApdFaxConverter1.Options := ApdFaxConverter1.Options + [coHalfHeight]; 
    if Ini.ReadBool('FaxConvert', 'Options.CenterImage', True) then 
      ApdFaxConverter1.Options := ApdFaxConverter1.Options + [coCenterImage]; 
    if Ini.ReadBool('FaxConvert', 'Options.Yield', True) then 
      ApdFaxConverter1.Options := ApdFaxConverter1.Options + [coYield]; 
    if Ini.ReadBool('FaxConvert', 'Options.YieldOften', False) then 
      ApdFaxConverter1.Options := ApdFaxConverter1.Options + [coYieldOften]; 
    ApdFaxConverter1.Resolution := TFaxResolution(Ini.ReadInteger('FaxConvert', 'Resolution', Ord(frNormal))); 
    ApdFaxConverter1.Width := TFaxWidth(Ini.ReadInteger('FaxConvert', 'Width', Ord(fwNormal))); 
 
    lblFontName.Caption := ApdFaxConverter1.EnhFont.Name; 
    lblFontSize.Caption := 'Size: ' + IntToStr(ApdFaxConverter1.EnhFont.Size); 
    edtLeftMargin.Text := IntToStr(ApdFaxConverter1.LeftMargin); 
    edtTopMargin.Text := IntToStr(ApdFaxConverter1.TopMargin); 
    edtLinesPerPage.Text := IntToStr(ApdFaxConverter1.LinesPerPage); 
    edtTabStop.Text := IntToStr(ApdFaxConverter1.TabStop); 
    cbxCenterImage.Checked := coCenterImage in ApdFaxConverter1.Options; 
    cbxYield.Checked := coYield in ApdFaxConverter1.Options; 
    cbxYieldOften.Checked := coYieldOften in ApdFaxConverter1.Options; 
    rgpResolution.ItemIndex := Ord(ApdFaxConverter1.Resolution); 
    rgpWidth.ItemIndex := Ord(ApdFaxConverter1.Width); 
    btnEnhFont.Enabled := cbxEnhTextEnabled.Checked; 
  finally 
    Ini.Free; 
  end; 
  RadioGroup1.ItemIndex:=0; 
end; 
 
procedure TFaxConversionForm.btnConvertClick(Sender: TObject); 
var 
  Ext,s: String; 
  RotImage1:TRotImage; 
  i:integer; 
  h:integer; 
begin 
   Ext:=LowerCase(ExtractFileExt(edtFileName.text)); 
  if btnConvert.Caption = '取消' then begin 
    Cancelled := True; 
    btnConvert.Caption := '转换'; 
    Exit; 
  end else begin 
    Cancelled := False; 
    btnConvert.Caption := '取消'; 
  end; 
  if (Ext='.bmp') or (Ext='.jpg') or (Ext='.jpeg') 
       or (Ext='.pcx') or (Ext='.dcx') 
       or (Ext='.tif')  then 
  begin 
    if (CheckBmp(image1,edtfileName.text)=2)or(Ext='.jpg')or(Ext='.jpeg') then 
        edtFileName.text:=ConvertToBmp(Image1,edtFileName.text); 
    if RadioGroup1.ItemIndex=1 then 
    begin 
     RotImage1:=TRotImage.Create(self); 
     RotImage1.Picture.LoadFromFile(edtFileName.text); 
     RotImage1.RotateBitmap(TRotationType(0)); 
     Rotimage1.Picture.SaveToFile(edtFileName.text); 
    end; 
    Ext:=LowerCase(ExtractFileExt(edtFileName.text)); 
  end else if (Ext='.txt') then 
  begin 
    if CheckBox1.Checked then 
    begin 
      RichEdit981.Font.Assign(FontDialog1.font); 
     // RichEdit981.Visible:=false; 
      RichEdit981.WordWrap:=true; 
      RichEdit981.Width:=Trunc(210*GetDeviceCaps(Canvas.Handle, LOGPIXELSX) * 25.4); 
      RichEdit981.Height:=Trunc(297*GetDeviceCaps(Canvas.Handle, LOGPIXELSX) * 25.4); 
 
      RichEdit982.WordWrap:=true; 
      RichEdit982.Width:=Trunc(210*GetDeviceCaps(Canvas.Handle, LOGPIXELSX) * 25.4); 
      RichEdit982.Height:=Trunc(297*GetDeviceCaps(Canvas.Handle, LOGPIXELSX) * 25.4); 
      Try 
        RichEdit981.Lines.LoadFromFile(edtFileName.text); 
        RichEdit982.lines:=RichEdit981.lines; 
      except 
      end; 
       h:=filecreate(edtFileName.text); 
      if h=-1 then exit; 
      For i:=1 to RichEdit982.lines.count do 
      begin 
        Application.ProcessMessages; 
        s:=RichEdit982.lines[i]+#$0d+#$0a; 
        FileWrite(h,s[1],length(s)); 
        s:=''; 
      end; 
      FileClose(h); 
    end; 
  end; 
  if Ext = '.txt' then 
  begin 
    if cbxEnhTextEnabled.Checked then 
      ApdFaxConverter1.InputDocumentType := idTextEx 
    else 
      ApdFaxConverter1.InputDocumentType := idText; 
    ApdFaxConverter1.DocumentFile:=edtFileName.text; 
  end 
  else if Ext = '.bmp' then 
  begin 
    ApdFaxConverter1.InputDocumentType := idBMP; 
    ApdFaxConverter1.DocumentFile := edtFileName.Text; 
  end 
  else if Ext='.fcp' then 
  begin 
     ConvertFCPToAPF(edtFileName.text, 0, 0, '', '', '', '', ApdFaxConverter1Status); 
  end 
  else if Ext = '.tif' then 
  begin 
    ApdFaxConverter1.InputDocumentType := idTIFF; 
    ApdFaxConverter1.DocumentFile := edtFileName.Text; 
  end 
  else if Ext = '.pcx' then 
  begin 
    ApdFaxConverter1.InputDocumentType := idPCX; 
    ApdFaxConverter1.DocumentFile := edtFileName.Text; 
  end 
  else if Ext = '.dcx' then 
  begin 
    ApdFaxConverter1.InputDocumentType := idDCX; 
    ApdFaxConverter1.DocumentFile := edtFileName.Text; 
  end 
  else  ApdFaxConverter1.InputDocumentType := idNone; 
  if ApdFaxConverter1.InputDocumentType = idNone then 
  begin 
     btnConvert.Caption := '转换'; 
     Exit; 
  end; 
  ApdFaxConverter1.OutFileName := edtOutFileName.Text; 
  Try 
    ApdFaxConverter1.ConvertToFile; 
    MessageDlg('转换成功!',mtconfirmation,[mbok],0); 
  except 
  end; 
  btnConvert.Caption := '转换'; 
end; 
 
procedure TFaxConversionForm.ApdFaxConverter1Status(F: TObject; Starting, 
  Ending: Boolean; PagesConverted, LinesConverted: Integer; BytesConverted, 
  BytesToConvert: Longint; var Abort: Boolean); 
begin 
  Abort := Cancelled; 
  if (BytesToConvert <> 0) then 
    Meter.Position := (BytesConverted * 100) div BytesToConvert; 
end; 
 
procedure TFaxConversionForm.btnEnhFontClick(Sender: TObject); 
begin 
  FontDialog1.Font := ApdFaxConverter1.EnhFont; 
  if FontDialog1.Execute then begin 
    ApdFaxConverter1.EnhFont := FontDialog1.Font; 
    lblFontName.Caption := ApdFaxConverter1.EnhFont.Name; 
    lblFontSize.Caption := IntToStr(ApdFaxConverter1.EnhFont.Size); 
  end; 
end; 
 
procedure TFaxConversionForm.FormClose(Sender: TObject; 
  var Action: TCloseAction); 
var 
  Ini: TIniFile; 
begin 
  Ini := TIniFile.Create(IniName); 
  try 
    Ini.WriteString('FaxConvert', 'LastInDir', LastInDir); 
    Ini.WriteString('FaxConvert', 'LastOutDir', LastOutDir); 
    Ini.WriteBool('FaxConvert', 'EnhTextEnabled', cbxEnhTextEnabled.Checked); 
    Ini.WriteString('FaxConvert', 'EnhFont.Name', ApdFaxConverter1.EnhFont.Name); 
    Ini.WriteInteger('FaxConvert', 'EnhFont.Size', ApdFaxConverter1.EnhFont.Size); 
    Ini.WriteInteger('FaxConvert', 'LeftMargin', ApdFaxConverter1.LeftMargin); 
    Ini.WriteInteger('FaxConvert', 'TopMargin', ApdFaxConverter1.TopMargin); 
    Ini.WriteInteger('FaxConvert', 'LinesPerPage', ApdFaxConverter1.LinesPerPage); 
    Ini.WriteInteger('FaxConvert', 'TabStop', ApdFaxConverter1.TabStop); 
    Ini.WriteBool('FaxConvert', 'Options.DoubleWidth', coDoubleWidth in ApdFaxConverter1.Options); 
    Ini.WriteBool('FaxConvert', 'Options.HalfHeight', coHalfHeight in ApdFaxConverter1.Options); 
    Ini.WriteBool('FaxConvert', 'Options.CenterImage', coCenterImage in ApdFaxConverter1.Options); 
    Ini.WriteBool('FaxConvert', 'Options.Yield', coYield in ApdFaxConverter1.Options); 
    Ini.WriteBool('FaxConvert', 'Options.YieldOften', coYieldOften in ApdFaxConverter1.Options); 
    Ini.WriteInteger('FaxConvert', 'Resolution', Ord(ApdFaxConverter1.Resolution)); 
    Ini.WriteInteger('FaxConvert', 'Width', Ord(ApdFaxConverter1.Width)); 
  finally 
    Ini.Free; 
  end; 
end; 
 
procedure TFaxConversionForm.cbxCenterImageClick(Sender: TObject); 
begin 
  if cbxCenterImage.Checked then 
    ApdFaxConverter1.Options := ApdFaxConverter1.Options + [coCenterImage] 
  else 
    ApdFaxConverter1.Options := ApdFaxConverter1.Options - [coCenterImage]; 
end; 
 
procedure TFaxConversionForm.cbxYieldClick(Sender: TObject); 
begin 
  if not cbxYield.Checked then cbxYieldOften.Checked:=false; 
  if cbxYield.Checked then 
    ApdFaxConverter1.Options := ApdFaxConverter1.Options + [coYield] 
  else 
    ApdFaxConverter1.Options := ApdFaxConverter1.Options - [coYield]; 
end; 
 
procedure TFaxConversionForm.cbxYieldOftenClick(Sender: TObject); 
begin 
  if cbxYieldOften.Checked then cbxYield.Checked:=true; 
  if cbxYieldOften.Checked then 
    ApdFaxConverter1.Options := ApdFaxConverter1.Options + [coYieldOften] 
  else 
    ApdFaxConverter1.Options := ApdFaxConverter1.Options - [coYieldOften]; 
end; 
 
procedure TFaxConversionForm.rgpResolutionClick(Sender: TObject); 
begin 
  ApdFaxConverter1.Resolution := TFaxResolution(rgpResolution.ItemIndex); 
end; 
 
procedure TFaxConversionForm.rgpWidthClick(Sender: TObject); 
begin 
  ApdFaxConverter1.Width := TFaxWidth(rgpWidth.ItemIndex); 
end; 
 
procedure TFaxConversionForm.edtLeftMarginExit(Sender: TObject); 
begin 
  ApdFaxConverter1.LeftMargin := StrToInt(edtLeftMargin.Text); 
end; 
 
procedure TFaxConversionForm.edtTopMarginExit(Sender: TObject); 
begin 
  ApdFaxConverter1.TopMargin := StrToInt(edtTopMargin.Text); 
end; 
 
procedure TFaxConversionForm.edtLinesPerPageExit(Sender: TObject); 
begin 
  ApdFaxConverter1.LinesPerPage := StrToInt(edtLinesPerPage.Text); 
end; 
 
procedure TFaxConversionForm.edtTabStopExit(Sender: TObject); 
begin 
  ApdFaxConverter1.TabStop := StrToInt(edtTabStop.Text); 
end; 
 
procedure TFaxConversionForm.btnBrowseInFileClick(Sender: TObject); 
begin 
  OpenDialog1.Filter := '提供的类型(*.txt;*.tif;*.pcx;*.dcx;*.bmp;*.fcp;*.jpg;*.jpeg)|' + 
    '*.txt;*.tif;*.pcx;*.dcx;*.bmp;*.fcp;*.jpg;*.jpeg|文本文件(*.txt)|*.txt|TIFF 文件(*.tif)|' + 
    '*.tif|PCX 文件 (*.pcx)|*.pcx|DCX 文件 (*.dcx)|*.dcx|Bitmap 文件 (*.bmp)|'+ 
    '*.bmp|(*.jpg)|*.jpg|(*.jpeg)|*.jpg|(*.fcp)|*.fcp|全部文件 (*.*)|*.*'; 
  OpenDialog1.InitialDir :=ExtractFilePath(Application.ExeName); 
  OpenDialog1.Title := '浏览要转换的文件'; 
  OpenDialog1.FileName := edtFileName.Text; 
  if OpenDialog1.Execute then begin 
    edtFileName.Text := OpenDialog1.FileName; 
    edtOutFileName.Text := ChangeFileExt(edtFileName.Text, '.apf'); 
    btnConvert.Enabled := FileExists(edtFileName.Text); 
    LastInDir := ExtractFilePath(edtFileName.Text); 
  end; 
end; 
 
procedure TFaxConversionForm.Button2Click(Sender: TObject); 
begin 
  OpenDialog1.Filter := '传真文件 (*.apf)|*.apf|All files (*.*)|*.*'; 
  OpenDialog1.InitialDir := LastOutDir; 
  OpenDialog1.Title := '输出文件'; 
  OpenDialog1.FileName := edtOutFileName.Text; 
  if OpenDialog1.Execute then begin 
    edtOutFileName.Text := OpenDialog1.FileName; 
    LastOutDir := ExtractFilePath(edtOutFileName.Text); 
  end; 
end; 
 
procedure TFaxConversionForm.RadioGroup2Click(Sender: TObject); 
begin 
   if RadioGroup2.ItemIndex=0 then 
     ApdFaxConverter1.Options := ApdFaxConverter1.Options - [coDoubleWidth]-[coHalfHeight] 
   else if RadioGroup2.ItemIndex=1 then 
     ApdFaxConverter1.Options := ApdFaxConverter1.Options +[coDoubleWidth] 
   else if RadioGroup2.ItemIndex=2 then 
      ApdFaxConverter1.Options := ApdFaxConverter1.Options+[coHalfHeight] 
end; 
 
procedure TFaxConversionForm.edtFileNameExit(Sender: TObject); 
begin 
    edtOutFileName.Text := ChangeFileExt(edtFileName.Text, '.apf'); 
    btnConvert.Enabled := FileExists(edtFileName.Text); 
    LastInDir := ExtractFilePath(edtFileName.Text); 
end; 
 
procedure TFaxConversionForm.btnOKClick(Sender: TObject); 
begin 
   close; 
end; 
 
end.