Hi there
 
I'm also trying to accomplish concatenation between n -number of PDF-documents...
I'm using XSL-FO and Apache FOP to create my PDF's but sometimes I need to concat multiple PDF documents. I'm no java expert but I've tried to create an other version of com.lowagie.tools.concat_pdf from the iText (version 1.0) project called MergePdfByteArrays. The concat_pdf class works just fine from a command-tool but I'm hold my pdf-documents as byte[] in memory, not as files and my output is either a database or as HTTP response from a servlet... My code don't through any exceptions and the document seems to be generated the same source code the concat_pdf except that some lines at the end of the file i missing ;(...
A buffer/fushing problem???
Here is my code for MergePdfByteArrays and a testing servlet.
 


package mypackage;

import java.io.*;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

/**
* This class concat PDF-files stored in byte[]'s in memory
* @author Erik Gulliksen
*/

public class MergePdfByteArrays 
    {
    private ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    private Document document = null;
    private PdfCopy  writer = null;

    public MergePdfByteArrays()
        { super(); }

    ByteArrayOutputStream getOutputStream()
        { return this.outStream; }

    void setOutputStream(ByteArrayOutputStream outStreamInput)
        { this.outStream = outStreamInput; }

    public byte[] getMergedPdfByteArray()
        {
        if(this.outStream != null )
            { return this.outStream.toByteArray(); }
        else
            { return null;}
        }

    public void add(byte[] pdfByteArray)
        {
        try
            {
            PdfReader reader = new PdfReader(pdfByteArray);
            int numberOfPages = reader.getNumberOfPages();
    
            if(this.document == null)
                {
                this.document = new Document(reader.getPageSizeWithRotation(1));
                this.writer = new PdfCopy( this.document, this.getOutputStream() );
                this.document.open();
                }
            PdfImportedPage page;
            for(int i = 0; i < numberOfPages; )
                {
                ++i;
                page = this.writer.getImportedPage(reader, i);
                this.writer.addPage(page);
                }
            PRAcroForm form = reader.getAcroForm();
            if(form != null)
                { this.writer.copyAcroForm(reader); }
            }
        catch(Exception e)
            { e.printStackTrace(); }
        }

    public void close()
        {
        try
            {
            this.document.close();
            this.outStream.close();
            }
        catch(Exception e)
            { e.printStackTrace(); }
        }

    }



/***********************************************
*** - Test of MergePdfByteArrays, servlet - ***
***********************************************/

package mypackage;

import com.lowagie.tools.*;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.*;
import mypackage.*;

public class TestMerge extends HttpServlet
    {
    private static final String CONTENT_TYPE = "application/pdf";

    public void init(ServletConfig config) throws ServletException
        { super.init(config); }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws
            ServletException, IOException
        { this.doPost( request, response); }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws 
            ServletException, IOException
        {
        response.setContentType(CONTENT_TYPE);
        response.setHeader("Content-disposition", "inline; filename=test.pdf");
        ServletOutputStream out = response.getOutputStream();
        try
            {
            // Testing with the same two file that worked vel with command-line tool 
            File docA = new File("C:\\test\\fileA.pdf");
            File docB = new File("C:\\test\\fileB.pdf");  

            byte[] byteA = new byte[ (int)docA.length() ];
            byte[] byteB = new byte[ (int)docB.length() ];

            FileInputStream fileInStreamA = new FileInputStream(docA);
            FileInputStream fileInStreamB = new FileInputStream(docB);

            fileInStreamA.read(byteA);
            fileInStreamB.read(byteB);

            MergePdfByteArrays pdfMerger = new MergePdfByteArrays();
            pdfMerger.add(byteA);
            pdfMerger.add(byteB);
            ByteArrayOutputStream finalOutStream = pdfMerger.getOutputStream();

            response.setContentLength(finalOutStream.size());
            finalOutStream.writeTo(out);
            out.flush();
            pdfMerger.close();
            }
        catch(IOException ioe)
            { ioe.printStackTrace(); }

        out.close();
        }
    }


Do some know a solution to my problem, or do anyone have a different aproache that works?
Thanks!
 
-Erik
 

Reply via email to