快点帮帮忙,我还是先把它顶上去再说!!!

解决方案 »

  1.   

    springfreamwork对pdf操作有这样一个类叫AbstractPdfView,这个东西对pdf的操作很容易的,如果你不会使用的话,可以看看源包里的juint测试,测试的类是PdfTestSuite.测试代码如下:
    /*
     * Copyright 2002-2004 the original author or authors.
     * 
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     * 
     *      http://www.apache.org/licenses/LICENSE-2.0
     * 
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */ 
    package org.springframework.web.servlet.view.document;import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    import java.util.HashMap;
    import java.util.Map;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import junit.framework.TestCase;import org.pdfbox.cos.COSDocument;
    import org.pdfbox.pdfparser.PDFParser;
    import org.pdfbox.util.PDFTextStripper;
    import org.springframework.mock.web.MockHttpServletRequest;
    import org.springframework.mock.web.MockHttpServletResponse;import com.lowagie.text.Document;
    import com.lowagie.text.Paragraph;
    import com.lowagie.text.pdf.PdfWriter;/**
     * @author Alef Arendsen
     */
    public class PdfTestSuite extends TestCase {

    public void testPdf() 
    throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();

    AbstractPdfView pdfView = new AbstractPdfView() {
    protected void buildPdfDocument(Map model, Document pdfDoc,
    PdfWriter writer, HttpServletRequest request,
    HttpServletResponse response) throws Exception {
    pdfDoc.add(new Paragraph("this should be in the PDF"));
    }
    };

    pdfView.render(new HashMap(), request, response);

    // get the content
    byte[] pdf = response.getContentAsByteArray();
    String text = parsePdf(pdf);
    if (text.indexOf("this should be in the PDF") == -1) {
    fail("The text we put in the PDF wasn't in there when we looked at it!");
    }
    }    public static final String DEFAULT_ENCODING =
            "ISO-8859-1";
        
        public static String parsePdf(byte[] pdf) throws Exception
        {
            PDFTextStripper stripper = new PDFTextStripper();
            // parse all of it!
            int startPage = 1;
            int endPage = Integer.MAX_VALUE;
            
            InputStream input = null;
            Writer output = null;
            COSDocument document = null;
            try {
                input = new ByteArrayInputStream(pdf);
                document = parseDocument( input );            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                output = new OutputStreamWriter(baos);            stripper.setStartPage( startPage );
                stripper.setEndPage( endPage );
                
                stripper.writeText( document, output );
                return new String(baos.toByteArray());                        
            }
            finally {
                input.close();
                output.close();
                document.close();
            }
        }    /** parses a PDF document resulting in a COSDocument */ 
        private static COSDocument parseDocument( InputStream input )throws IOException {
            PDFParser parser = new PDFParser( input );
            parser.parse();
            return parser.getDocument();
        }   
    }相信你能看明白的......