- 最近孩子每天要学习一个小故事,做了个pdf转换图片,方便打印,学习
unit MainFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, PdfiumCore, ExtCtrls, StdCtrls, PdfiumCtrl, jpeg, PdfiumLib, Spin,
ComCtrls,ShellAPI;
type
TfrmMain = class(TForm)
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
btnStart: TButton;
Label1: TLabel;
edtFile: TEdit;
btnOpen: TButton;
GroupBox1: TGroupBox;
rbAll: TRadioButton;
rbJS: TRadioButton;
rbOS: TRadioButton;
rbZDY: TRadioButton;
seStart: TSpinEdit;
seEnd: TSpinEdit;
Label2: TLabel;
GroupBox2: TGroupBox;
RichEdit1: TRichEdit;
ProgressBar1: TProgressBar;
Label3: TLabel;
procedure FormCreate(Sender: TObject);
procedure btnStartClick(Sender: TObject);
procedure btnOpenClick(Sender: TObject);
private
FDocument: TPdfDocument;
public
{ Public-Deklarationen }
end;
var
frmMain: TfrmMain;
implementation
uses
TypInfo, Printers;
{$R *.dfm}
procedure TfrmMain.FormCreate(Sender: TObject);
begin
PDFiumDllDir := ExtractFilePath(ParamStr(0));
FDocument := TPdfDocument.Create;
seStart.MinValue := 1;
seEnd.MinValue := 1;
seStart.MaxValue := 1;
seEnd.MaxValue := 1;
ForceDirectories(PDFiumDllDir + '图片');
end;
function ColorToARGB(Color: TColor; Alpha: Byte = $FF): FPDF_DWORD;
const
clSystemColor = $FF000000;
var
RGBColor: DWORD;
begin
if (DWORD(Color) and clSystemColor) = clSystemColor then
RGBColor := GetSysColor(DWORD(Color) and $FFFFFF)
else
RGBColor := DWORD(Color);
Result := FPDF_ARGB(Alpha, RGBColor and $FF, (RGBColor and $FF00) shr 8, (RGBColor and $FF0000) shr 16);
end;
procedure ToBitmap(Bitmap: FPDF_BITMAP; Result: TBitmap);
var
Format, Width, Height, Stride, BytesPerPixel, Y: Integer;
Buffer: Pointer;
begin
Format := FPDFBitmap_GetFormat(Bitmap);
Width := FPDFBitmap_GetWidth(Bitmap);
Height := FPDFBitmap_GetHeight(Bitmap);
Stride := FPDFBitmap_GetStride(Bitmap);
Buffer := FPDFBitmap_GetBuffer(Bitmap);
case Format of
FPDFBitmap_Unknown:
Exit;
FPDFBitmap_Gray:
begin
Result.PixelFormat := pf8bit;
BytesPerPixel := 1;
end;
FPDFBitmap_BGR:
begin
Result.PixelFormat := pf24bit;
BytesPerPixel := 3;
end;
else
begin
Result.PixelFormat := pf32bit;
BytesPerPixel := 4;
end;
end;
Result.Width := Width;
Result.Height := Height;
for Y := 0 to Height - 1 do
begin
Move(Buffer^, Result.ScanLine[Y]^, BytesPerPixel * Width);
Inc(PByte(Buffer), Stride);
end;
end;
procedure TfrmMain.btnOpenClick(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
edtFile.Text := OpenDialog1.FileName;
FDocument.LoadFromFile(edtFile.Text);
seStart.MaxValue := FDocument.PageCount;
seEnd.MaxValue := FDocument.PageCount;
seEnd.Value := seEnd.MaxValue;
end;
end;
procedure TfrmMain.btnStartClick(Sender: TObject);
var
i: Integer;
var
JpegImage: TJpegImage;
pdfBitmap: TPdfBitmap;
Bitmap: TBitmap;
w, h: integer;
begin
RichEdit1.Clear;
ProgressBar1.Max := FDocument.PageCount;
for i := 0 to FDocument.PageCount - 1 do
begin
ProgressBar1.Position := i + 1;
if rbJS.Checked then
begin
if (i + 1) mod 2 = 0 then
begin
Continue;
end;
end;
if rbOS.Checked then
begin
if (i + 1) mod 2 <> 0 then
begin
Continue;
end;
end;
if rbZDY.Checked then
begin
if not ((i + 1) in [seStart.Value..seEnd.Value]) then
begin
Continue;
end;
end;
try
JpegImage := TJpegImage.Create;
Bitmap := TBitmap.Create;
JpegImage.CompressionQuality := 100;
w := Round(150 * FDocument.Pages[i].Width / 72.0);
h := Round(150 * FDocument.Pages[i].Height / 72.0);
w := Round(100 * FDocument.Pages[i].Width / 72.0);
h := Round(100 * FDocument.Pages[i].Height / 72.0);
pdfBitmap := TPdfBitmap.Create(w, h, True);
pdfBitmap.FillRect(0, 0, w, h, ColorToARGB(clWhite));
FDocument.Pages[i].DrawToPdfBitmap(pdfBitmap, 0, 0, w, h, prNormal, [proAnnotations, proPrinting]);
ToBitmap(pdfBitmap.Bitmap, Bitmap);
JpegImage.Assign(Bitmap);
JpegImage.SaveToFile(ExtractFilePath(ParamStr(0)) +'图片\' + IntToStr(i + 1) + '.jpg');
RichEdit1.Lines.Add(IntToStr(i + 1) + '.jpg 转换成功!');
PostMessage(RichEdit1.Handle, WM_VSCROLL, SB_BOTTOM, 0);
finally
JpegImage.Free;
Bitmap.Free;
pdfBitmap.free;
end;
end;
ShellExecute(0,'open','Explorer.exe',PWideChar(ExtractFilePath(ParamStr(0)) + '图片'),nil,SW_NORMAL);
RichEdit1.Lines.Add('转换完成!');
end;
end.