⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainunit.cpp

📁 生成PDF文档的控件
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "MainUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "VPDFDoc"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------

__fastcall TForm1::PreparePage()
{
	   VPDF->CurrentPage->SetFont( "Arial", TFontStyles() << fsItalic, 10 , 0, false);
	   VPDF->CurrentPage->PrintText( 50, VertPos, 0, TableName + "  Page"+ IntToStr(PageNum) );
	   VPDF->CurrentPage->PrintText( 480, VertPos, 0, DateTimeToStr(Now()) );
	   VPDF->CurrentPage->MoveTo( 50, VertPos+15 );
	   VPDF->CurrentPage->LineTo( 570, VertPos+15 );
	   VPDF->CurrentPage->MoveTo( 50, VertPos+45 );
	   VPDF->CurrentPage->LineTo( 570, VertPos+45 );
	   VPDF->CurrentPage->Stroke();
	   VPDF->CurrentPage->SetFont("Times New Roman", TFontStyles() << fsItalic << fsBold, 12, 0, false);
	   VPDF->CurrentPage->SetRGBFillColor(clNavy);
	   PrintRow( VertPos + 25, "No", "Company", "Addr", "City", false);
	   VPDF->CurrentPage->SetRGBFillColor(clBlack);
	   VertPos += 20;
}

__fastcall TForm1::PrintRow( int Position, AnsiString No, AnsiString Company, AnsiString Addr, AnsiString City, bool ShowBackground )
{
	   if ( ShowBackground )
	   {
		   VPDF->CurrentPage->SetRGBColor(0xFFF3DD);
		   VPDF->CurrentPage->Rectangle(50, Position, 520, 20);
		   VPDF->CurrentPage->Fill();
		   VPDF->CurrentPage->SetRGBColor(clBlack);
	   }
	   VPDF->CurrentPage->PrintText(  70, Position, 0, No );
	   VPDF->CurrentPage->PrintText( 110, Position, 0, Company );
	   VPDF->CurrentPage->PrintText( 300, Position, 0, Addr );
	   VPDF->CurrentPage->PrintText( 480, Position, 0, City );
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
	  TableName = "Customer.DB";

	  CustomerTable->DatabaseName = "DBDEMOS";   //  Open demo customer
	  CustomerTable->TableName = TableName;      //   database table
	  CustomerTable->Active = true;              //
	  CustomerTable->First();

	  VPDF->AutoLaunch = true;                                      // PDF file will be shown automatically
	  VPDF->FileName = "TableDemo.pdf";                           // Set PDF filename
	  VPDF->PageLayout = plOneColumn;
	  VPDF->BeginDoc (false);                                                // Create PDF file
	  VPDF->CurrentPage->SetFont ("Arial", TFontStyles() << fsBold, 24 , 0, false);             // Set current font
	  VPDF->CurrentPage->PrintText (200, 20, 0, "Customer report"); // Print PDF header
	  PageNum = 1;
	  VertPos = 60;                                                // Draw and print
	  PreparePage();                                                  //  table header
	  bool Back = true;
	  while ( !CustomerTable->Eof )
	  {
			Back = !Back;
			if ( VertPos > 700 )                                // If end of page then
			{
				VPDF->AddPage();                                         // Add page, draw and print
				PageNum++;                                         // table header and set
				VertPos = 60;                                        // new print position
				PreparePage();
			}
			PrintRow( VertPos + 25,                                   // print  row from the current position
					  CustomerTable->FieldValues["CustNo"],
					  CustomerTable->FieldValues["Company"],
					  CustomerTable->FieldValues["Addr1"],
					  CustomerTable->FieldValues["City"],
					  Back
					  );
			VertPos += 20;
			CustomerTable->Next();                                       // Go to next record
	  }
	   VPDF->EndDoc();                                                 // Close PDF file

}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -