The Fabio
The Fabio

Reputation: 6250

How to merge pdf files with PDFReactor 5.1

I am working with an established application where PDFReactor 5.1 is in use. The company will not upgrade it for a while (maybe another 1-2 years).

I was tasked with building functionality to merge pdf files (a appending all pdfs will do) from a C# based web application.

Reading though the old docs of this tool (which are from 2012) I have found they propose this type of usage:

To merge multiple PDFs use the methods setMergeURLs or setMergeByteArrays

String[] urls = {"http://www.myserver.com/overlaid1.pdf", "http://www.myserver.com/overlaid2.pdf"};
pdfReactor.setMergeURLs(urls);
pdfReactor.setMergeMode(PDFreactor.MERGE_MODE_APPEND);

However, the documentation does not specify what method to call to render the merged pdf and setMergeURLs does not return a value (public void SetMergeURLs(string[] urls))

I can see there are 3 render methods, when I explore the client DLL, but all of them require some input (presumably in addition to the pdf you set with setMergeURLs):

I tried using these 3 render methods but they only result on an empty array with the following error:

org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.

it seems PDF reactor is trying to do a conversion from XML to pdf instead of the merge...

what am I missing??

Upvotes: 0

Views: 623

Answers (1)

The Fabio
The Fabio

Reputation: 6250

I spoke with RealObjects's Support (pdf reactor's creator) and this is their answer:

In general PDFreactor is not intended to just merge existing PDFs. You can only merge one or more existing PDFs to an actual HTML to PDF conversion. See also our FAQ.

This FAQ looks like intended for the current version only, so it does not really help with the usage of version 5.1 (as I require).

I kept trying a little more until I finally got it to merge. This is the main part of it:

var pdfReactor = new PDFreactor();
pdfReactor.SetLicenseKey(myLicenseKey);
pdfReactor.SetMergeMode(PDFreactor.MERGE_MODE_PREPEND); // prepend because it seems to reference the doc to be generated by RenderDocumentFromContent
pdfReactor.SetMergeByteArrays(
    new[]
    {
        File.ReadAllBytes("c:/pdfs/first.pdf"),
        File.ReadAllBytes("c:/pdfs/second.pdf")
    });

var merged = pdfReactor.RenderDocumentFromContent("<html></html>");

File.WriteAllBytes($@"C:\pdfs\merged.pdf", merged);

The merged.pdf file contains the other two files however it is started with a blank page (due to the <html></html> I had it use on the RenderDocumentFromContent call to make it not produce errors).

That is as far as this exercise will go for me, I will leave it at that and consider another tool to do pdf merging, as I wouldn't want this blank page around.

Upvotes: 0

Related Questions