techprimate / TPPDF

TPPDF is a simple-to-use PDF builder for iOS and macOS written in Swift
https://techprimate.github.io/TPPDF/
MIT License
743 stars 125 forks source link

Is there a way to keep a section as whole and not split between 2 pages? #362

Open icarus31 opened 9 months ago

icarus31 commented 9 months ago

Hi,

First of all, great job for that tool!

I create a section and wonder if it is possible to make sure it does not split between two pages?

For that case, should I use a group instead?

Thanks

philprime commented 6 months ago

Hi there, sorry for the late response. I am not entirely sure if I understand your case correctly.

Do I understand correctly, that you are using multiple columns in a section, but they should not extend to the next page?

icarus31 commented 6 months ago

No worries, I understand that you are not 100% dedicated to this superb project! :-)

So, what I meant is I have a section (see function below) that I want to make sure everything stick together if a new page is needed. Right now, it just cut the section in half between 2 pages. I would like to have similar behavior, like you do with a regular table. The new table row will move to the next page and not be truncated between 2 pages. Also, I don't want to see any lines for my section.

Is it something doable with a section. If not, you have an alternative to that?

pdfDocument!.add(section: myNewSection(with: item))

func myNewSection(with item: Item) -> PDFSection {

    let section = PDFSection(columnWidths: [0.5, 0.5])
    let itemImage = PDFImage(image: image, size: CGSize(width: 125, height: 125), sizeFit: .height)
        section.columns[0].add(.center, image: nodeImage)
    section.columns[1].add(.left, textObject: PDFSimpleText(text: "Info", style: self.h2Style))
    section.columns[1].add(.left, space: 2.0)
    section.columns[1].add(attributedText: item.description)
    section.columns[1].add(attributedText: item.number)
    section.columns[1].add(attributedText: item.name)
    section.columns[1].add(attributedText:  item.shortName)

    return section
}

Thanks

philprime commented 6 months ago

PDFGroup would probably be the correct approach to keep the elements together.

Is there a reason why you are creating two columns but only adding content to the second one?

icarus31 commented 6 months ago

I will see what I can do with PDFGroup.

For the first column, I forgot a line in the example! :-)

section.columns[0].add(.center, image: itemImage)

philprime commented 6 months ago

Ah now it makes more sense :)

Yes please test it and let me know if it worked out.