Search Docs
Loading...
Skip to content

Folded Cards

Build a bi-fold greeting card and a tri-fold leaflet, and keep each block on its panel. Then change the paper format of the card and export the flat sheets for print.

8 mins
estimated time
GitHub

We build an A5 greeting card and an A4 tri-fold leaflet, each with an outside and an inside sheet. We clip the outside panels and place one photo across the inside fold of the card. Then we change the card to half US Letter and export the four sheets. This guide covers:

  • How a page with page slices makes a folded card
  • Building a bi-fold card and a tri-fold leaflet
  • Clipping the panels at the folds
  • Keeping each block on its panel while users edit
  • Changing the paper format of a card
  • Exporting the flat sheets for a print shop

How a Folded Card Is Built#

A folded card is one flat sheet of paper with folds. CE.SDK builds each printed side of the sheet as one page. Each panel of that side is one page slice of the page, and the folds lie between the page slices. This guide calls a page a sheet and a page slice a panel, except next to an API name.

  • A bi-fold card has two panels on each side, so it needs two sheets: the outside and the inside.
  • A tri-fold leaflet has three panels on each side. One panel folds inside, so it is 3 mm narrower than the other two.
  • The other panels have the size of the folded card. Along the fold axis, a sheet is as long as all its panels together.
  • The print shop sets the bleed. This guide uses 3 mm on each side.
  • A tent card folds from top to bottom. For it, set page/pageSliceAxis to Vertical before you append the page slices.

Folded Products and Spreads explains the model, the size rules and the ownership rules of page slices.

Create the Scene#

Create a design scene and set its design unit to millimeters. All sizes in this guide are then in millimeters.

try engine.scene.create(sceneLayout: .verticalStack)
try engine.scene.setDesignUnit(.mm)
let stack = try engine.block.find(byType: .stack)[0]

Create a Sheet#

A sheet is a page that holds one page slice per panel. To build one:

  1. Create a page and add it to the stack.
  2. Turn on its margin, which CE.SDK uses as the bleed, and set 3 mm on each side.
  3. Add one page slice per panel, each with its own width, height and name.

The helper below puts these steps together:

private struct CardPanel {
let name: String
let width: Float
let height: Float
}
@MainActor
private func createSheet(engine: Engine, stack: DesignBlockID, panels: [CardPanel]) throws -> DesignBlockID {
let page = try engine.block.create(.page)
try engine.block.setName(page, name: panels.map(\.name).joined(separator: " | "))
try engine.block.appendChild(to: stack, child: page)
// A 3 mm bleed on every side of the sheet
try engine.block.setBool(page, property: "page/marginEnabled", value: true)
for side in ["top", "bottom", "left", "right"] {
try engine.block.setFloat(page, property: "page/margin/\(side)", value: 3)
}
for panel in panels {
let pageSlice = try engine.block.create(.pageSlice)
// Size the page slice before it joins the page.
try engine.block.setWidth(pageSlice, value: panel.width)
try engine.block.setHeight(pageSlice, value: panel.height)
try engine.block.setName(pageSlice, name: panel.name)
try engine.block.appendChild(to: page, child: pageSlice)
}
return page
}

The sheet takes its size from its panels, so we never size the page. The engine lays out the sheet on its next update. Until then, getWidth on the page returns its earlier width, such as the default 100 mm. getOwningPageSlice, resizeContentAware and export lay out the sheet themselves.

Build a Bi-Fold Card#

The outside of a bi-fold card reads from left to right: the back, then the front. The front of the folded card is the right half of the outside sheet.

For an A5 card that folds to A6, each panel is 105 × 148 mm. Each sheet is then A5 in landscape.

// An A5 card folds to A6: two panels of 105 × 148 mm on each side.
let outside = try createSheet(engine: engine, stack: stack, panels: [
CardPanel(name: "Back", width: 105, height: 148),
CardPanel(name: "Front", width: 105, height: 148),
])
let inside = try createSheet(engine: engine, stack: stack, panels: [
CardPanel(name: "Inside Left", width: 105, height: 148),
CardPanel(name: "Inside Right", width: 105, height: 148),
])
let outsidePanels = try engine.block.getPageSlices(outside)
let back = outsidePanels[0]
let front = outsidePanels[1]

Build a Tri-Fold Leaflet#

In a tri-fold leaflet, the inside flap folds in first. It is 3 mm narrower than the other two panels, so it fits inside without buckling. For an A4 leaflet of 297 × 210 mm in landscape:

  • The outside holds the inside flap, the back and the cover: 97, 100 and 100 mm.
  • The print shop turns the sheet over. So the inside holds the same widths in the opposite order: 100, 100 and 97 mm.
// The inside flap is 3 mm narrower, so it fits inside without buckling.
// The inside sheet holds the same widths in the opposite order.
let leafletOutside = try createSheet(engine: engine, stack: stack, panels: [
CardPanel(name: "Inside Flap", width: 97, height: 210),
CardPanel(name: "Back", width: 100, height: 210),
CardPanel(name: "Cover", width: 100, height: 210),
])
let leafletInside = try createSheet(engine: engine, stack: stack, panels: [
CardPanel(name: "Inside Left", width: 100, height: 210),
CardPanel(name: "Inside Middle", width: 100, height: 210),
CardPanel(name: "Inside Right", width: 97, height: 210),
])

Add Content to the Panels#

We give the front a color fill and a title. We place one photo across the inside of the card, and a blue band on the leaflet cover. setFill on a page slice also turns its fill on.

let frontFill = try engine.block.createFill(.color)
try engine.block.setColor(frontFill, property: "fill/color/value", color: .rgba(r: 0.55, g: 0.12, b: 0.2, a: 1))
try engine.block.setFill(front, fill: frontFill)
// Content is a child of the page, in the coordinates of the sheet.
let title = try engine.block.create(.text)
try engine.block.appendChild(to: outside, child: title)
try engine.block.replaceText(title, text: "Happy Birthday")
try engine.block.setTextFontSize(title, fontSize: 24)
try engine.block.setTextColor(title, color: .rgba(r: 1, g: 1, b: 1, a: 1))
try engine.block.setWidth(title, value: 85)
try engine.block.setHeightMode(title, mode: .auto)
try engine.block.setPositionX(title, value: 115)
try engine.block.setPositionY(title, value: 60)
// One photo across the inside, bleed included, centered on the fold
let photo = try engine.block.create(.graphic)
try engine.block.setShape(photo, shape: engine.block.createShape(.rect))
let photoFill = try engine.block.createFill(.image)
try engine.block.setURL(
photoFill,
property: "fill/image/imageFileURI",
value: baseURL.appendingPathComponent("ly.img.image/images/sample_1.jpg"),
)
try engine.block.setFill(photo, fill: photoFill)
try engine.block.appendChild(to: inside, child: photo)
try engine.block.setPositionX(photo, value: -3)
try engine.block.setPositionY(photo, value: -3)
try engine.block.setWidth(photo, value: 216)
try engine.block.setHeight(photo, value: 154)
// A band on the leaflet cover that reaches 20 mm onto the back and 3 mm
// into the bleed
let band = try engine.block.create(.graphic)
try engine.block.setShape(band, shape: engine.block.createShape(.rect))
let bandFill = try engine.block.createFill(.color)
try engine.block.setColor(bandFill, property: "fill/color/value", color: .rgba(r: 0.13, g: 0.36, b: 0.6, a: 1))
try engine.block.setFill(band, fill: bandFill)
try engine.block.appendChild(to: leafletOutside, child: band)
try engine.block.setPositionX(band, value: 177)
try engine.block.setPositionY(band, value: 20)
try engine.block.setWidth(band, value: 123)
try engine.block.setHeight(band, value: 40)

Place each block in the coordinates of the whole sheet. The origin is the top-left corner of the trim. The bleed starts at x = -3 mm and y = -3 mm.

  • The front starts at x = 105 mm. The title starts 10 mm inside it, at x = 115 mm.
  • The photo of the inside starts at x = -3 mm and y = -3 mm. It is 6 mm larger than the sheet, so it covers both panels and the bleed.
  • The center of the photo is on the fold, so Inside Left owns it. A clip on Inside Left would cut the photo at the fold.
  • The blue band of the leaflet has its center on the cover. It reaches 20 mm onto the back and 3 mm into the bleed.

Clip the Panels at the Folds#

setClipped on a panel cuts the blocks that the panel owns at its folds and at the edges of the sheet with their bleed. The panel under the center of a block owns the block. On a fold, the earlier panel owns it.

  • The cover of the leaflet owns the blue band. The clipped cover cuts the band at the fold to the back.
  • At an outer edge of the sheet, a clipped panel cuts at the edge of the bleed. The band reaches 3 mm into the bleed and stops there.
  • An unclipped panel lets a block cross the fold, such as the photo across the inside of the card.
  • Each panel has its own clip setting.
// A clipped panel cuts the blocks that it owns at its folds, so the band
// stops at the fold between the back and the cover of the leaflet.
for sheet in [outside, leafletOutside] {
for panel in try engine.block.getPageSlices(sheet) {
try engine.block.setClipped(panel, clipped: true)
}
}
// The inside panels stay open, so the photo crosses the fold.
if let photoOwner = try engine.block.getOwningPageSlice(photo) {
print("The photo belongs to:", try engine.block.getName(photoOwner))
}

Keep Content on Its Panel While Users Edit#

A movement constraint on a page slice holds the blocks that it owns inside that page slice. It acts while users move or resize a block on the canvas. The edges to the other page slices act as page edges.

// Users cannot drag the blocks of an outside panel past its edges.
try engine.editor.setMovementConstraint([
MovementConstraintRule(overshoot: 0, scope: .block(back)),
MovementConstraintRule(overshoot: 0, scope: .block(front)),
])
if let titleConstraint = try engine.editor.getMovementConstraint(title) {
print("Overshoot on the title:", titleConstraint.overshoot)
}
  • overshoot: 0 keeps a block fully on its panel. 0.3 lets 30% of the block reach past the edge.
  • A block wider than its panel stays inside the page on that axis, not inside the panel.
  • During a drag, the panel where the drag started applies until the block is fully on another panel.
  • removeMovementConstraint(.block(panel)) removes the rule.
  • Calls from your code are not limited by the constraint.
  • getMovementConstraint returns the rule that applies to a block, with its overshoot, or nil.

Change the Paper Format#

resizeContentAware on a page gives the whole sheet a new size. Each block stays on its panel.

  • Pass the size of the whole sheet, not the size of one panel. Half US Letter in landscape is 215.9 × 139.7 mm.
  • The panels keep the ratio of their widths, not the difference. So the flap of a leaflet is no longer exactly 3 mm narrower.
  • To keep the flap 3 mm narrower on a sheet of width W, set each other panel to (W + 3) / 3 mm. Set the flap to 3 mm less. Each block moves with its panel.
  • The resize also scales and moves the blocks, so a font size can change.
  • A page slice refuses the call with BLOCK.PAGE_SLICE_RESIZE_CONTENT_AWARE. Resize its page instead.
  • To offer paper formats, keep a list of sheet sizes in your app and pass the size that the user chooses.
// Pass the size of the whole sheet: half US Letter, landscape.
try engine.block.resizeContentAware([outside, inside], width: 215.9, height: 139.7)
let titleOwner = try engine.block.getOwningPageSlice(title)
print("The title stays on the front:", titleOwner == front)

Export the Sheets for Print#

A print shop wants each side of a card as one flat sheet, with the bleed, crop marks and fold marks. Export each sheet as a PDF with exportPdfWithCropMarks and exportPdfWithFoldMarks. A pair of fold marks shows each fold.

  • printMarkOffset is the distance from the trim to the marks, in design units. The default is 6 pt, so the marks reach into a 3 mm bleed. Set the offset to the bleed, here 3 mm.
  • A scene export gives one PDF page per page. To send both sides of a card in one PDF, keep each product in its own scene. Then export the scene.
  • Without a license, each export carries a watermark.

Export to PDF describes the marks, the TrimBox and the BleedBox, and the option exportPdfPageSlices that gives one PDF page per panel.

// One flat sheet per printed side, with crop and fold marks
let printMarks = ExportOptions(
exportPdfWithCropMarks: true,
exportPdfWithFoldMarks: true,
// The marks start at the edge of the 3 mm bleed.
printMarkOffset: 3,
)
var sheetPDFs: [Data] = []
for sheet in [outside, inside, leafletOutside, leafletInside] {
let pdf = try await engine.block.export(sheet, mimeType: .pdf, options: printMarks)
// Save the PDF to a file or share it in your app.
print(try engine.block.getName(sheet), "PDF bytes:", pdf.count)
sheetPDFs.append(pdf)
}

Each export returns the PDF as Data. Write it to a file in your app container, or hand it to a share sheet.

Troubleshooting#

  • A photo shows on the next panel. The panel that owns the photo is not clipped. Call setClipped on that page slice.
  • A photo is cut off at the fold. The panel that owns the photo is clipped. Turn the clip off, or move the center of the photo onto the panel where it belongs.
  • The sheet has the wrong size after a format change. resizeContentAware got the size of one panel. Pass the size of the whole sheet.
  • A format change fails with BLOCK.PAGE_SLICE_RESIZE_CONTENT_AWARE. The call got a page slice. Pass its page.
  • A tent card has panels with width and height swapped. The axis changed after the page slices were appended. Set page/pageSliceAxis before you append them.
  • The marks print over the bleed. The default mark offset is 6 pt. Set printMarkOffset to the bleed.
  • Users drag a block onto the next panel. Set a movement constraint on the page slice.

API Reference#

API Purpose
engine.block.setBool(_:property:value:) Turn on the bleed of a sheet with page/marginEnabled.
engine.block.setFloat(_:property:value:) Set the bleed on one side of a sheet with page/margin/top and the other sides.
engine.block.create(.pageSlice) Create a panel. Size it, then append it to a page.
engine.block.getPageSlices(_:) Get the panels of a sheet in reading order.
engine.block.setClipped(_:clipped:) Cut the blocks of a panel at its folds and at the page edges with their bleed.
engine.block.getOwningPageSlice(_:) Get the panel that owns a block, or nil.
engine.block.setEnum(_:property:value:) Put the panels in a row or a column with page/pageSliceAxis.
engine.editor.setMovementConstraint(_:) Keep the blocks of a panel on that panel while users edit.
engine.editor.getMovementConstraint(_:) Get the rule that applies to a block, or nil.
engine.editor.removeMovementConstraint(_:) Remove the constraint of a panel.
engine.block.resizeContentAware(_:width:height:) Give the sheets a new size, and keep each block on its panel.
engine.block.export(_:mimeType:options:) Export a sheet as a PDF.
ExportOptions(exportPdfWithCropMarks:exportPdfWithFoldMarks:printMarkOffset:) Ask for crop and fold marks, and set their distance from the trim.

Next Steps#