📄 updfwriter.pas
字号:
FDestNameTreeNumberInNodes: Integer;
//number of the PDF object of the catalog of the PDF file
FCatalogObjNum: TPDFObjectNumber;
//number of the PDF object of the root of the outline of the file
FOutlineObjNum: TPDFObjectNumber;
//number of the PDF object of the root of the name tree of destionation
FDestNamesObjNum: TPDFObjectNumber;
//Sets the group to add pages to.
procedure SetPageGroupInsertPos(Value :Integer);
//Creates all fonts into ~[link FFonts].
procedure CreateFonts;
//Returns the representation of a rect in the PDF file.
function GetBox(Rect: TPDFRect): String;
//Write the resources of the page(s).
procedure WriteResources(Images: TStrings = nil);
protected
//Gets a font object by its styles.
function GetFont(Font: TPdfFontType; Style: TPdfFontStyles): TType1Font;
//Returns a string as a text in the PDF file.
function StrToPDFStr(const S: String): String;
//Returns a number of characters as a text in the PDF file.
function CharactersToPDFStr(Characters: PChar; Count: Integer): String;
//Returns a string as a text in the PDF file in a binary representation.
function StrToBinaryPDFStr(const S: String): String;
//Returns a buffer of bytes as a text in the PDF file in a binary
//representation.
function BufferToBinaryPDFStr(Buffer: Pointer; Count: Integer): String;
//Begins a new PDF object at the current position and returns its number.
function GetAddObjPosNumber: TPDFObjectNumber;
//Writes some text into the stream.
procedure Write(const Str: String; NewLine: Boolean = True);
//Writes some formatted text into the stream.
procedure WriteFormatted(const Fmt: String; const Args: array of const;
NewLine: Boolean = True);
//Writes some text into the page.
procedure WritePage(const S: String; NewLine: Boolean = True);
//Writes a PDF number value as part of a command into the page.
procedure WritePageCommandNumber(const Number: TPDFValue);
//Ends the current page.
procedure EndPage;
//Registers a page number and returns parent object in the tree of pages.
function RegisterPageNumber(var Page: TPDFObjectNumber): TPDFObjectNumber;
//Write the references to the page objects.
procedure WritePages;
//Writes the name tree of the destinations.
procedure WriteDestinationNameTree;
//Write the catalog of the PDF file.
procedure WriteCatalog;
public
//Creates the object and starts the PDF file.
constructor Create(Stream: TStream);
//Frees the object and all fields.
destructor Destroy; override;
//Writes the object with the information of the document.
procedure WriteDocumentInfo(const Creator, Title,
Subject, Keywords: String);
//Writes the fonts to the PDF file.
procedure WriteFonts;
//Starts the first page.
procedure FirstPage;
//Starts a new page.
procedure NewPage;
//Adds an annotation to the current page.
procedure AddAnnotation(AnnType: TLinkType; const Dest: String;
const Rect: TPDFRect);
//Adds a destination on the current page.
procedure AddDestination(X, Y: TPDFValue; const DestLabel: String);
//Adds an alias of the most recently defined destination on the current
//page.
procedure AddDestinationAlias(const DestAlias: String);
//Returns the number that the next PDF object would have.
function NextObject: TPDFObjectNumber;
//Ends the part of the shown data (pages) of the document.
procedure EndShownDocument;
//Write the root of the outline of the PDF document.
function WriteOutlineRoot(First, Last: TPDFObjectNumber;
Count: Integer): TPDFObjectNumber;
//Write an entry of the outline of the PDF document.
function WriteOutlineEntry(const Title, Dest: String;
Parent, Prev, Next,
First, Last: TPDFObjectNumber;
Count: Integer): TPDFObjectNumber;
//Writes the final data and ends the PDF document.
procedure EndDocument;
//Ends the current group of pages.
procedure EndPageGroup;
//Writes raw PDF code into the current page; just for texting!
// procedure WriteRawInPage(const Data: String);
//Returns the width of a symbol in the current font size.
function GetSymbolWidth(Symbol: Char): TPDFValue;
//Writes a symbol into the current page.
procedure WriteSymbol(Symbol: Char; Color: TColor; DefaultColor: TColor;
UseBorder: Boolean);
//Sets a new font.
procedure SetFont(Font: TPdfFontType; Size: TPDFValue;
Style: TPdfFontStyles = []);
//Begins the output of text on the page.
procedure BeginText; // BT
//Ends the output of text on the page.
procedure EndText; // ET
//Moves the position where the next text would be shown.
procedure MoveTextPoint(tx, ty: TPDFValue); // Td
//Sets the position where the next text would be shown.
procedure SetTextPoint(tx, ty: TPDFValue); // Tm
//Shows some text at the current position.
procedure ShowText(const S: String); // Tj
//Shows some characters at the current position.
procedure ShowCharacters(Characters: PChar; Count: Integer); // Tj
//Sets the rendering mode of the text.
procedure SetTextRenderingMode(Mode: TPDFTextRenderingMode); // Tr
//Sets the additional word spacing of the text.
procedure SetWordSpacing(Spacing: TPDFValue); // Tw
//Sets the additional character spacing of the text.
procedure SetCharacterSpacing(Spacing: TPDFValue); // Tc
//Sets the color of the text.
procedure SetColor(Value: TPDFColor); // rg
//Sets the color of lines and borders.
procedure SetStrokeColor(Value: TPDFColor); // RG
//Adds an image to the PDF file to be drawn later.
procedure AddImage(ImageData: TStream; const ImageSize: TPoint;
const Name: String;
Interpolate: Boolean = False; IsPNG: Boolean = False);
//Returns the size of a previously added image.
function GetImageSize(const ImageName: String): TPoint;
//Draws an image on the page.
procedure DrawImage(const Name: String; // Do
Width, Height, X, Y: TPDFValue);
//Saves the position in the ~[link FPageStream] to insert text afterwards.
//~deprecated
procedure SaveInsertionPoint;
//Inserts text in the ~[link FPageStream] at the saves position and unsets
//~[link FInsertionPoint].
//~deprecated
procedure InsertTextCommand(const PDFCommand: String);
//Returns the width of the text in the current font.
function TextWidth(const Text: String): TPDFValue;
//Returns the total width of the characters in the current font.
function TextWidthCharacters(Characters: PChar; Count: Integer): TPDFValue;
//Returns the number of fitting characters of the text in the given length
//in the current font.
function FittingText(const Text: String; AWidth: TPDFValue): Integer;
//Calculates the distance to the top of the line.
function ToTopOfLine: TPDFValue;
//Gets the width of the specified text in the specified font.
function TextWidthInFont(Font: TPdfFontType; Style: TPdfFontStyles;
Size: TPDFValue; const Text: String): TPDFValue;
//Gets the widths of the specified characters in the specified font.
procedure GetCharacterWidths(Font: TPdfFontType;
Style: TPdfFontStyles;
Size: TPDFValue;
const Text: String;
var Widths: array of TPDFValue;
StartIndex: Integer);
//Draws a line
procedure DrawLine(FromX, FromY, ToX, ToY: TPDFValue); // m l S
//Sets the width of lines to be drawn.
// procedure SetLineWidth(Width: TPDFValue); // w
//Adds additional commands to be drawn (by calling ~[link DoDrawCommands]
//before ~[link NewPage ending a page]) to the current page.
procedure AddLowLevelDrawingCommandsToPage(const Commands: String);
//Draws the lines drawn by ~[link DrawLine].
procedure DoDrawCommands;
//Adds an low level drawing command to the page, it will be added directly
//and not at the end of the page.
procedure DoLowLevelDrawingCommand(const Command: String);
property Stream: TStream read FStream;
property Compression: Boolean read FCompression write FCompression;
property PageWidth: Single read FPageWidth write FPageWidth;
property PageHeight: Single read FPageHeight write FPageHeight;
property Font: TType1Font read FFont;
property Size: TPDFValue read FSize;
property FontType: TPDFFontType read FFontType;
property FontStyle: TPDFFontStyles read FFontStyle;
property Color: TColor read FFontColor;
property PageGroupInsertPos: Integer read FPageGroupInsertPos
write SetPageGroupInsertPos;
end;
//Converts a PDF number value into its string representation.
function PDFNumberToStr(Number: TPDFValue): String;
implementation
uses SysUtils,
ZLib, //if you don't have this file installed, search on your Delphi CD
General,
UImages;
{Converts a PDF number value into its string representation. The value of the
number has to be below 20 million and is converted with two digits after the
decimal point.
~param Number the value to be written
~result the string representation of the number }
function PDFNumberToStr(Number: TPDFValue): String;
const BufferSize = 15; //size of the buffer to convert the number
var S :String[BufferSize]; //used to convert the number
TheValue :Integer; //number * 100 as an integer
pS :PChar; //runner through the buffer
AbsValue :Cardinal; //positiv number * 100 as an integer
begin
TheValue := Round(Number * 100); //get the number to be written
pS := PChar(@S[BufferSize]); //initialize buffer
if TheValue = 0 then //number is zero?
ps^ := '0' //write the zero
else
begin
AbsValue := Abs(TheValue); //first write positive number
ps^ := Char(Ord('0') + AbsValue mod 10); //add last (hundredth) digit
AbsValue := AbsValue div 10;
if (AbsValue = 0) or (pS^ <> '0') then //is relevant?
Dec(pS);
ps^ := Char(Ord('0') + AbsValue mod 10); //add "tenth"-digit
AbsValue := AbsValue div 10;
if (pS^ <> '0') or (pS <> PChar(@S[BufferSize])) then //is relevant?
begin
Dec(pS);
pS^ := '.'; //add decimal point
Dec(pS);
end;
repeat //add all other digits
ps^ := Char(Ord('0') + AbsValue mod 10);
Dec(pS);
AbsValue := AbsValue div 10;
until AbsValue = 0;
if TheValue < 0 then //value was negative?
pS^ := '-' //prepend signum
else
Inc(pS);
end;
SetString(Result, pS, @S[BufferSize] - pS + 1); //return the number
end;
{ * * * *** * * * *** TType1Font *** * * * *** * * * }
{Creates the font object and sets its values.
~param Widths width of the characters in the font
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -