SQL Server Requires Chilkat v11.7.0+
SQL Server
Alignment of Replaced PDF Text: Right-Aligned Amounts
See more PDF Text Replacement Examples
When the new text is wider or narrower than the text it replaces, the "align" option decides where it sits. The default, "auto", keeps a placeholder in place when it is right-aligned with other text on the page or centered, and otherwise keeps its left edge; a label's value is always left-aligned after its label. The invoice template's amounts are right-aligned, so the new amounts keep their right edge, which the "grid" text layout (characters placed by position) shows.Chilkat SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- The PDF used in this example is available at invoice_template.pdf
DECLARE @pdf int
EXEC @hr = sp_OACreate 'Chilkat.Pdf', @pdf OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @pdf, 'LoadFile', @success OUT, 'qa_data/pdf/invoice_template.pdf'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pdf
RETURN
END
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.customer', 'Acme Corp'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.date', '2026-10-18'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.po', 'PO-88213'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.qty1', '12'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.desc1', 'Cordless drill, 18 V'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.price1', '$460.00'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.amount1', '$5,520.00'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.qty2', '60'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.desc2', 'Galvanized hinge, 4 inch'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.price2', '$12.50'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.amount2', '$750.00'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.subtotal', '$6,270.00'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.taxRate', '7.35%'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.tax', '$460.85'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'placeholders.total', '$6,730.85'
-- "auto" is the default: a placeholder that is right-aligned with other text keeps its right edge.
-- The other choices are "left", "right" and "center".
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'align', 'auto'
DECLARE @jsonOut int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonOut OUT
EXEC sp_OAMethod @pdf, 'TextReplace', @success OUT, 'apply', @json, @jsonOut
IF @success = 0
BEGIN
EXEC sp_OAMethod @jsonOut, 'Emit', @sTmp0 OUT
PRINT 'Not applied: ' + @sTmp0
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
RETURN
END
END
-- A replacement that runs into neighbouring text or off the page is still made, with a warning.
EXEC sp_OAMethod @jsonOut, 'SizeOfArray', @iTmp0 OUT, 'warnings'
PRINT 'warnings: ' + @iTmp0
EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/invoice_aligned.pdf'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
RETURN
END
-- The "grid" layout places each character by its position on the page, so the right-aligned
-- amounts can be seen to line up (the wider amounts grew to the left).
DECLARE @jsonText int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonText OUT
EXEC sp_OAMethod @jsonText, 'UpdateString', @success OUT, 'layout', 'grid'
DECLARE @sbText int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbText OUT
EXEC sp_OAMethod @pdf, 'ExtractTextSb', @success OUT, 'text', @jsonText, @sbText, @jsonOut
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
EXEC @hr = sp_OADestroy @jsonText
EXEC @hr = sp_OADestroy @sbText
RETURN
END
EXEC sp_OAMethod @sbText, 'GetAsString', @sTmp0 OUT
PRINT @sTmp0
-- Sample output:
-- warnings: 0
-- INVOICE
-- Chilkat Hardware Supply
-- 1200 Harbor Road, Suite 4
-- Portside, WA 98000
--
--
-- Bill To: Acme Corp
-- Invoice #: INV-0000
-- Date: 2026-10-18
-- PO Number: PO-88213
-- Ref: A-1
--
--
--
-- Qty Description Unit Price Amount
-- 12 Cordless drill, 18 V $460.00 $5,520.00
-- 60 Galvanized hinge, 4 inch $12.50 $750.00
--
-- Subtotal: $6,270.00
-- Tax (7.35%): $460.85
-- Total Due: $6,730.85
--
-- Ref: A-2
--
-- Payment is due within 30 days. Please include the invoice number with your payment.
-- Thank you for your business, Acme Corp!
-- View this invoice online
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
EXEC @hr = sp_OADestroy @jsonText
EXEC @hr = sp_OADestroy @sbText
END
GO