-2

I have two services that each return a pdf file in a byte[]. What I need is to join in the same pdf the two pdf that I generate separately in two services.

I have tried different ways of concatenating the two byte[] but all I get is that one overwrites the other.

Attempts that have not worked:

byte[] result = new byte[a.length + b.length];
// copy a to result
System.arraycopy(a, 0, result, 0, a.length);
// copy b to result
System.arraycopy(b, 0, result, a.length, b.length);

--

byte[] c = Bytes.concat(a, b);

--

    ByteArrayOutputStream output = new ByteArrayOutputStream();

    try {
        output.write(b);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        output.write(a);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    byte[] out = output.toByteArray();

I would appreciate any kind of help.

Thank you very much.

EDIT. POSSIBLE SOLUTION

I have read this solution. It may not be the best, but so far it is working.

https://stackoverflow.com/a/50416159/9850714

Gadium
  • 7
  • 4
  • 3
    You're going to have to do something much, much more complicated. You can't just throw the byte sequences together; you'll have to manipulate the PDF file format. – Louis Wasserman Jun 03 '22 at 17:46
  • 3
    You're going to have to append the page(s) of pdf 2 to pdf 1. You'll need a pdf api to do that – g00se Jun 03 '22 at 17:49
  • There is no way in *Hell o PDF World!* to mish/mash 2 pdfs together. Unless its a proof of concept for dubious use. The construction of PDF means that to add a second file ALL its contents need to totally modified and the first file will need non trivial amendments (unless the minimum case) near its ending. Thus by far the simplest is start file 3 add file 1 body and file 2 body then with millions of years luck, there may be few problems e.g. good library should not show the seams. – K J Jun 03 '22 at 21:04

0 Answers0