Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.7.0+

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 Objective-C Downloads

Objective-C
#import <CkoPdf.h>
#import <CkoJsonObject.h>
#import <CkoStringBuilder.h>

BOOL success = NO;

//  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

CkoPdf *pdf = [[CkoPdf alloc] init];
success = [pdf LoadFile: @"qa_data/pdf/invoice_template.pdf"];
if (success == NO) {
    NSLog(@"%@",pdf.LastErrorText);
    return;
}

CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"placeholders.customer" value: @"Acme Corp"];
[json UpdateString: @"placeholders.date" value: @"2026-10-18"];
[json UpdateString: @"placeholders.po" value: @"PO-88213"];
[json UpdateString: @"placeholders.qty1" value: @"12"];
[json UpdateString: @"placeholders.desc1" value: @"Cordless drill, 18 V"];
[json UpdateString: @"placeholders.price1" value: @"$460.00"];
[json UpdateString: @"placeholders.amount1" value: @"$5,520.00"];
[json UpdateString: @"placeholders.qty2" value: @"60"];
[json UpdateString: @"placeholders.desc2" value: @"Galvanized hinge, 4 inch"];
[json UpdateString: @"placeholders.price2" value: @"$12.50"];
[json UpdateString: @"placeholders.amount2" value: @"$750.00"];
[json UpdateString: @"placeholders.subtotal" value: @"$6,270.00"];
[json UpdateString: @"placeholders.taxRate" value: @"7.35%"];
[json UpdateString: @"placeholders.tax" value: @"$460.85"];
[json UpdateString: @"placeholders.total" value: @"$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".
[json UpdateString: @"align" value: @"auto"];

CkoJsonObject *jsonOut = [[CkoJsonObject alloc] init];
success = [pdf TextReplace: @"apply" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
    NSLog(@"%@%@",@"Not applied: ",[jsonOut Emit]);
    if (success == NO) {
        NSLog(@"%@",pdf.LastErrorText);
        return;
    }

}

//  A replacement that runs into neighbouring text or off the page is still made, with a warning.
NSLog(@"%@%d",@"warnings: ",[[jsonOut SizeOfArray: @"warnings"] intValue]);

success = [pdf WriteFile: @"qa_output/invoice_aligned.pdf"];
if (success == NO) {
    NSLog(@"%@",pdf.LastErrorText);
    return;
}

//  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).
CkoJsonObject *jsonText = [[CkoJsonObject alloc] init];
[jsonText UpdateString: @"layout" value: @"grid"];
CkoStringBuilder *sbText = [[CkoStringBuilder alloc] init];
success = [pdf ExtractTextSb: @"text" jsonIn: jsonText sbOut: sbText jsonOut: jsonOut];
if (success == NO) {
    NSLog(@"%@",pdf.LastErrorText);
    return;
}

NSLog(@"%@",[sbText GetAsString]);

//  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