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:
Hi Jeremy,
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();
license.setLicense("/var/java/Aspose.Words.lic");
// 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”);
//}
//rtfStream.reset();
// 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.setWrapType(WrapType.NONE);
noteBox.setWidth(550);
noteBox.setHeight(35);
noteBox.setTop(-35); //Sets vertical position; should be equal to or greater than height
noteBox.setHorizontalAlignment(HorizontalAlignment.CENTER);
noteBox.setStrokeColor(Color.WHITE); // make border/stroke blend in with background
// Create paragraph styles for watermark
Style noteParaStyle = rtfDoc.getStyles().add(StyleType.PARAGRAPH, “MyStyle1”);
noteParaStyle.getFont().setName(“Arial”);
noteParaStyle.getFont().setSize(9);
noteParaStyle.getFont().setColor(Color.RED);
// 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();
notePara.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
notePara.getParagraphFormat().setStyle(noteParaStyle);
// Add some text to the paragraph.
Run run = new Run(rtfDoc);
run.setText(note);
notePara.appendChild(run);
// Append the textbox to the first paragraph in the body.
rtfDoc.getFirstSection().getBody().getFirstParagraph().appendChild(noteBox);
}
// Create TOC if it exists
rtfDoc.updateFields();
// 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.
encryptionDetails.setPermissions(PdfPermissions.DISALLOW_ALL);
// Extend permissions to allow editing or modifying annotations.
encryptionDetails.setPermissions(PdfPermissions.PRINTING);
saveOptions.setEncryptionDetails(encryptionDetails);
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;
}
}
END_OF_JAVA_CODE
CLASSPATH => ‘/var/java/Aspose.Words.jdk16.jar:/var/java/commons-codec-1.6.jar’,
SHARED_JVM => 1,
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,
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
pdfDocument1.getPages().add();
main::com::aspose::pdf::internal::ms::System::z87=HASH(0xa3e69f4)
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,
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,
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.