0

I have 2 PDF files.

I read both files as byte arrays, file1 and file2, and concatenate then to a third array file3.

When I write this third array to the file system, the created file size is the sum of file1 and file2, but it shows only the content of file1.

Here is how I write the array to the file system:

try (FileOutputStream stream = new FileOutputStream(path)) {
    stream.write(file3);
}

How can i see all pages of merged file ?

jhamon
  • 3,603
  • 4
  • 26
  • 37
Singh123
  • 216
  • 3
  • 10
  • 3
    Possible duplicate of [How to merge two PDF files into one in Java?](https://stackoverflow.com/questions/3585329/how-to-merge-two-pdf-files-into-one-in-java) – jhamon Oct 21 '19 at 08:12
  • 2
    Like many file format, pdf is not just a text content, it has a defined structured with headers and footers. Simply concatenating both binary doesn't modify this structure. So the reader app doesn't know it should keep reading the file after it reaches the first footer. There are dedicated libraries to merge PDF, check the linked question. – jhamon Oct 21 '19 at 08:16
  • It's actually even worse than described by @jhamon. Pdfs have to be read from the end where there is an offset from the start of file to certain core structures. As the value of the second file is now applied to the concatenation of both files, it points to some arbitrary position. You are lucky that something sensible is displayed at all. – mkl Oct 21 '19 at 09:44
  • [How to merge two PDF files into one in Java?](https://stackoverflow.com/questions/3585329/how-to-merge-two-pdf-files-into-one-in-java) is not an exact duplicate: It is more specialized as it focuses on merging using a specific PDF library, PDFBox. Nonetheless it's a good pointer. – mkl Oct 21 '19 at 10:08
  • 1
    Thanks for your input . @mkl Is there anyway to merge two PDF byte Array into one PDF without using any third party library? – Singh123 Oct 21 '19 at 13:22
  • *"Is there anyway to merge two PDF byte Array into one PDF without using any third party library?"* - As there is no explicit PDF support in the JRE: Only by effectively implementing a (specialized) PDF library yourself. Not an easy task. Plan for some months of programming... (I assume "calling external third party programs or web services" would for you be equivalent to "using third party libraries".) – mkl Oct 21 '19 at 14:08
  • Thanks for your suggestions – Singh123 Oct 21 '19 at 16:36

0 Answers0