PDF Concatenate not working

I am having issues with ASPOSE.PDF for Java getting the concatenate feature to work properly. I have it coded like the sample in your documentation, but it keeps throwing an error when I get to the line for add the pages to the primary document. I am using a inputstream which the PDF file data and I am able to get the page information prior to joining the two files together, but it just fails. Here is the message that I get:

I do get the page info before this so I know it is seeing the data, but as soon as I add the line
pdfDocument1.getPages().add(pdfDocument2.getPages()); it will throw the above error at me. Here is the full code for review

public String doPdfFileCombine (String Content1, String Content2) throws Exception{

// Convert the string into an array of bytes and pass it into a new memory stream
InputStream pdfStream1 = new ByteArrayInputStream(Base64.decodeBase64(Content1));
// Open the first document from InputStream
com.aspose.pdf.Document pdfDocument1 = new com.aspose.pdf.Document(pdfStream1);
// Convert the second string into an array of bytes and pass it into a new memory stream
InputStream pdfStream2 = new ByteArrayInputStream(Base64.decodeBase64(Content2));
// Open the second document from InputStream
com.aspose.pdf.Document pdfDocument2 = new com.aspose.pdf.Document(pdfStream2);

// Create output stream
ByteArrayOutputStream outStream1 = new ByteArrayOutputStream();
// Create output stream
ByteArrayOutputStream outStream2 = new ByteArrayOutputStream();
// Save Files so you can getPages for concating
pdfDocument1.save(outStream1, SaveFormat.Pdf);
pdfDocument2.save(outStream2, SaveFormat.Pdf);
String Test = "Page Count : " + pdfDocument1.getPages().size() + "Page Count : " + pdfDocument2.getPages().size();
// Add pages of second document to the first
// Save concatenated output file
pdfDocument1.save(outStream1, SaveFormat.Pdf);
Test += "\n Page Count2 : " + pdfDocument1.getPages().size() + "Page Count2 : " + pdfDocument2.getPages().size();
// Convert outStream data to Base64 String so we can pass back to Perl
//String outString = new String(Base64.encodeBase64Chunked(outStream1));
return Test;

Side note on this Issue we are currently using the ASPOSE.Words software to convert RTF to Doc and PDF file which is where the Content input is coming from. What we are trying to do is add additional pdf files to the converted RTF->PDF data we are currently generating. The key though is we want all the Table of Content links to still work after concatenating the files together. Is this even going to do that, we are using a different software currently that strip all the hyperlink data when combining multiple PDFs. Any info and assistance would be appreciated.

Hi Jeremy,

Thanks for your inquiry. I have tested your shared code with my sample PDF files using Aspose.Pdf for Java 10.6.1 and unable to notice the exception. If you are using some old version then please download and try latest version of Aspose.Pdf for Java, it will resolve the issue. However If the issue persist then please share your sample PDF documents here as well, so we will look into these and guide you accordingly.

We are sorry for the inconvenience caused.

Best Regards,

I don’t really have an actual PDF file. We use Perl as our base for our sight and access the ASPOSE software via inline Java call. Our documents are starting as RTF code which re run through the ASPOSE.Words outputting as a PDF file. We and then trying to take that returned data and use it in the above function. Here is the code we currently use when converting the RTF string to a PDF.

import java.awt.Color;
import java.io.;
import com.aspose.words.;
import org.apache.commons.codec.binary.*;
public class rtfStringConvert {
public String doRtfStringConvert (String rtfString, String newFormat, String note) throws Exception{
// License stuff
License license = new License();
// Convert the string into an array of bytes and pass it into a new memory stream
InputStream rtfStream = new ByteArrayInputStream(Base64.decodeBase64(rtfString));
// This may be useful in the future but disabling for now
//FileFormatInfo info = FileFormatUtil.detectFileFormat(rtfStream);
//if (info.getLoadFormat() != LoadFormat.RTF) {
//throw new Exception(“The RTF string passed is invalid.”);
//System.err.println(“invalid format”);
// Create new Document from inputStream
Document rtfDoc = new Document(rtfStream);
// Set Fonts
FontSettings.setFontsFolder("/var/java/fonts/", false);
// Create output stream
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//Adds watermark for blank forms
if ( !note.equals(“none”) ) {
// Create TEXT_BOX to hold text
Shape noteBox = new Shape(rtfDoc, ShapeType.TEXT_BOX);
noteBox.setTop(-35); //Sets vertical position; should be equal to or greater than height
noteBox.setStrokeColor(Color.WHITE); // make border/stroke blend in with background
// Create paragraph styles for watermark
Style noteParaStyle = rtfDoc.getStyles().add(StyleType.PARAGRAPH, “MyStyle1”);
// Let’s create a new paragraph for the textbox manually and align it in the center. Make sure we add the new nodes to the textbox as well.
noteBox.appendChild(new Paragraph(rtfDoc));
Paragraph notePara = noteBox.getFirstParagraph();
// Add some text to the paragraph.
Run run = new Run(rtfDoc);
// Append the textbox to the first paragraph in the body.
// Create TOC if it exists
// Convert to the desired format
if ( newFormat.equals(“pdf”) ) {
//If file is water marked then we want to set permissions so they can only print. JD - 7-15-2014
if ( !note.equals(“none”) ) {
PdfSaveOptions saveOptions = new PdfSaveOptions();
// Create encryption details and set owner password.
PdfEncryptionDetails encryptionDetails = new PdfEncryptionDetails("", “Timbob@1”, PdfEncryptionAlgorithm.RC_4_128);
// Start by disallowing all permissions.
// Extend permissions to allow editing or modifying annotations.
rtfDoc.save(outStream, saveOptions);
} else {
rtfDoc.save(outStream, SaveFormat.PDF);
} else if ( newFormat.equals(“doc”) ) {
rtfDoc.save(outStream, SaveFormat.DOC);
} else if ( newFormat.equals(“docx”) ) {
rtfDoc.save(outStream, SaveFormat.DOCX);
} else if ( newFormat.equals(“html”) ) {
rtfDoc.save(outStream, SaveFormat.HTML);
} else if ( newFormat.equals(“png”) ) {
rtfDoc.save(outStream, SaveFormat.PNG);
// Convert outStream data to Base64 String so we can pass back to Perl
String outString = new String(Base64.encodeBase64Chunked(outStream.toByteArray()));
return outString;
CLASSPATH => ‘/var/java/Aspose.Words.jdk16.jar:/var/java/commons-codec-1.6.jar’,
DIRECTORY => ‘/var/www/html/TempDocfjyrnnvfwsms/’,
package AsposeWordsInline;

sub convertRtfString {

my ($rtfString, $format, $note) = @;
my $inlineClass = new rtfStringConvert();
if (!$note) {
$note = ‘none’;
#my $newFile = $inlineClass->doRtfStringConvert($rtfString, $format);
my $newFile = $inlineClass->doRtfStringConvert(MIME::Base64::encode_base64($rtfString), $format, $note);
return MIME::Base64::decode_base64($newFile);

Then we are taking that returned value in $newFile for two different documents and passing them into the function I provide above through similar process. Some the $newFile is being returned to my script and then instantly being passed into the below function that is calling the code I posted in my initial post.

package AsposePDFInline;
sub combinePDFs {
my ($Document1, $Document2) = @;
my $inlineClass = new pdfFileCombine();
my $newFile = $inlineClass->doPdfFileCombine(MIME::Base64::encode_base64($Document1), MIME::Base64::encode_base64($Document2));
return $newFile;

Which seems to be working from the passing of data point since the getPages().size() is returning the same number of pages that I get when I output the converted RTF returned data to the html page. I have attached those files, though I am not accessing them from the saved files included.

Ultimately I need to be able to take multiple generated files mixed with some previously generated PDF files and combine them together. And the biggest question that I need answered is if the ASPOSE.PDF software will combine them together while keeping their Table of Content links intact. As I said we already have a license for the Words software and will being adding the PDF software if we can get this to work for the situations I described.

Hi Jeremy,

Thanks for sharing the details.

The above details and code snippet show that you are using Aspose.Words to convert RTF file to PDF format. Please note that Aspose.Pdf for Java can Concatenate PDF Files. However in resultant document, the TOC needs to be updated because if multiple PDF documents containing TOC are concatenated, there will be multiple TOC entries. So you need to recreate TOC so that all its entries are pointing to correct page. For further details, please visit Add TOC to Existing PDF

Thank you for the insight on the TOC issue, I will look into that further. As far as the original issue I am having your response unfortunately did not address it. I am aware that a majority of the code in my second post was Apose.Words RTF to PDF. If you look at the bottom of that and the explanation I provided with the code you see that I am taking RTF and converting to a PDF file via Aspose.Words. I am then taking that returned data stream and passing it into the function doPdfFileCombine() which was the function in my original post that is throwing the exception with the pdfDocument1.getPages().add(pdfDocument2.getPages()); line.

I have attached all of the applicable code in the word file. I have highlighted in purple the RTF code going into the Aspose.Words function call and the return of that back into the Perl code. In yellow highlight is the first pdf file data returned and its path. In blue is the second pdf file data returned and its path. In red is where the code is throwing the main::com::aspose::pdf::internal::ms::System::z87=HASH(0xbeb1a7c) exception error on the concatenate code.

If I comment out or remove the concatenate code the function runs fine and I get the saved TestSave.pdf file properly. I have also tried replacing the InputStream data with the Input1.pdf and Input2.pdf that I attached in my previous post. I have them open directly in the function and that too will give the same error at the concatenate line of code, but will work once that line is removed. So it appears that there is something wrong with the output from Aspose.Words and the concatenate feature of Aspose.Pdf. If you could please give me insight to how to address this that would be appreciated.

As an additional note I have tried just using doc.getPages().add() and I get the same results

// Insert a empty page at the end of PDF


I have also tried with a pdf file that was not RTF generated through Aspose.Words which I attached and I am getting the same problem.

Hi Jeremy,

Thanks for your inquiry. I have tested the scenario with your recent shared PDF document and still unable to notice the issue. I have again gone through your code and I’m unable to notice license implementation for Aspose.Pdf for Java. Please note when you are using more than one JAR in code you have to implement license for each jar separately. So please implement license for Aspose.Pdf for java as well, hopefully it will resolve the issue.

However if the issue persist then please try your code in a simple java project and share the results.

We are sorry for the inconvenience caused.

Best Regards,

We do not currently have a license for the Aspose.pdf software, we are trying it in evaluation mode to see if it will do what it needs us to do. So I would need a temp/trial license to be able to try your suggestion.

If you could send me the pdf that you were able to create from the Input1 and Input2 files I sent you then I could at least evaluate if the product will do what we need it to do as far as maintaining the TOC links once the two documents are joined. That would let me know if it is even worth any more time trying to get this to work on our side if the software can’t do what we need it to do. With Adobe we are able to join them together manually and the TOCs stay intact and working, we need that functionality.

In the meantime I will attempt to create the simple Java project and see if I can get that to work.

Thanks for the assistance.

Hi Jeremy,

Thanks for your feedback. You are getting issue due to evaluation mode. Aspose.Pdf evaluation
version has two limitations, evaluation watermark and at most four
elements of any collection can be viewed. Please make a
request for 30 days temporary license and apply it to evaluate our product without
any limitation. it will resolve the issue.

As requested, please find attached concatenated output of Input1.pdf and Input2.pdf for your reference.

Please feel free to contact us for any further assistance.

Best Regards,