Hello all,

At the risk of reinventing the wheel I thought I should contribute a workaround 
I've come up with for getting my page breaks into the RTF output by a servlet 
running FOP.  Our usage runs FOP on the back end of a transform for which the 
input is xml pulled out of Fedora Repository based upon the users choice.

Instead of passing the output of the fop factory to the servlet response 
directly, I pass it to a byte array.  Searching the byte array for occurrences 
of a page break proxy string I can swap that string out for the string the RTF 
relies upon to signal the page break.  The altered byte array is then passed on 
to the response.

Because we pass the output of the transform to FOP, we need only alter our 
transform to output the desired page break proxy string in those spots where we 
want a page break.  A partial listing is as follows:

ByteArrayOutputStream RTFresByteArray = new ByteArrayOutputStream();
StreamResult RTFres = new StreamResult(RTFresByteArray);

Fop fop = fopFactory.newFop(MimeType, RTFresByteArray);

//after the first transform

                        FOPtransformer.transform(FOsrc, res);
                        
                        //put the result in a byte array
                        byte[] RTFbyteArr = RTFresByteArray.toByteArray();
                        String findStr = "xKDARTFpagebreakencodingx";  //
                        byte[] findStrByte = findStr.getBytes( "8859_1" /* 
encoding */ );
                        String breakStr = "\\pard \\insrsid \\page \\par";    
//25 chars
                        byte[] breakStrByte = breakStr.getBytes( "8859_1" /* 
encoding */ );
                        //now do the find and replace
                        for (int i = 0; i < (RTFbyteArr.length-26); ++i) {
                            if (RTFbyteArr[i] == findStrByte[0] &&
                                    RTFbyteArr[i+1] == findStrByte[1] &&
                                        RTFbyteArr[i+2] == findStrByte[2]){
                                if (RTFbyteArr[i+3] == findStrByte[3] &&
                                        RTFbyteArr[i+4] == findStrByte[4] &&
                                            RTFbyteArr[i+5] == findStrByte[5]){
                                    for (int j = 0; j <25; j++){
                                        RTFbyteArr[i + j] = breakStrByte[j];
                                    }
                                }
                                
                            }
                        }
                        //put the byte array in the outputstream
                        out.write(RTFbyteArr);

This works fine in the context of a servlet.  If however I were running FOP in 
a local environment I would alternately put a backend in batch to find and 
replace the proxy string to get the page breaks.

Cheers,
Keith



Reply via email to