L
L
LittleBob2022-03-13 11:40:08
Java
LittleBob, 2022-03-13 11:40:08

How to create and save PDF file with special text formatting?

Good afternoon.
Let's say there is an array int[13, 24, 24, 65, 3, 32, 27, 77, 88, 25, 74, 14]
How can I create and save a PDF (maybe txt, or in another file, if easier) like this so that the numbers are arranged in this format 4 vertically in a row?:
13 3 88
24 32 25
24 27 74
65 77 14

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2022-03-13
@LittleBob

- "bite off" the required number of elements from the array
- print what you "bite off"
- go to a new line
- repeat until the array is over
UPD .:
Well, since Sergey Gornostaev was generous with the code, I will add my own version:

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

class Scratch {

    public static void main(String[] args) {
        int[] ints = new int[]{13, 24, 24, 65, 3, 32, 27, 77, 88, 25, 74, 14};

        batches(Arrays.stream(ints).boxed().toList(), 3)
                .map(list -> list.stream().map(String::valueOf).collect(Collectors.joining(" ")))
                .reduce((s1, s2) -> s1 + System.lineSeparator() + s2)
                .ifPresent(System.out::println);
    }

    private static <T> Stream<List<T>> batches(List<T> source, int length) {
        var size = source.size();
        if (length <= 0 || size <= 0) {
            return Stream.empty();
        }
        var fullChunks = (size - 1) / length;
        return IntStream.range(0, fullChunks + 1).mapToObj(
                n -> source.subList(n * length, n == fullChunks ? size : (n + 1) * length));
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question