Am 30.11.2005 um 22:20 schrieb Stefan Pantke:

I need to compose PDF output based on certain input,

For anybody interested, here is a snippet for outputting PDF
using iText - probably not the nicest solution, but for a newbie, this might
be a nice starting point:

>>>
//
// Main.java: Class file for WO Component 'Main'
// Project dprint
//
// Created by sp on 30.11.05
//

import com.webobjects.foundation.*;
import com.webobjects.appserver.*;
import com.webobjects.eocontrol.*;
import com.webobjects.eoaccess.*;
import com.webobjects.foundation.NSData;

// for iText
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.*;
import java.util.Date;
import java.awt.Color;

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

 class TAPDFComponent extends WOComponent {
        private String _contentType = "application/pdf";
        
        public TAPDFComponent(WOContext context) {
                super(context);
        }
        public void setContentType(String aType) {
                _contentType = aType;
        }

        public String contentType() {
                return _contentType;
        }

        public void appendToResponse(WOResponse response, WOContext context) {
                // Voir page suivante …

                // Document document = new Document(PageSize.A4.rotate());
                Document document = new Document( PageSize.A4 );

        ByteArrayOutputStream oBuffer = new ByteArrayOutputStream();
                try {
                        PdfWriter.getInstance( document, oBuffer );

                        try {
                                
                                // step 2:
                                // we create a writer that listens to the 
document
                                // and directs a PDF-stream to a file
                                
                                // step 3: we open the document
                                document.open();
                                
                                // step 4: we add some content
document.add(new Paragraph("To create a document in landscape format, just make the height smaller than the width. For instance by rotating the PageSize Rectangle: PageSize.A4.rotate()"));
                                document.setPageSize(PageSize.A4);
                                document.newPage();
                                document.add(new Paragraph("This is portrait 
again"));
            document.add(new Paragraph("Hello World"));
            document.add(new Paragraph(new Date().toString()));

                        Chunk c;
                        c = new Chunk("background");
                        c.setBackground(new Color(0xC0, 0xC0, 0xC0));
                        document.add(c);
                        

                        // the 14 standard fonts in PDF: do not use this Font 
constructor!
                        // this is for demonstration purposes only, use 
FontFactory!
                        Font[] fonts = new Font[14];
                        fonts[0] = new Font(Font.COURIER, Font.DEFAULTSIZE, 
Font.NORMAL);
                        fonts[1] = new Font(Font.COURIER, Font.DEFAULTSIZE, 
Font.ITALIC);
                        fonts[2] = new Font(Font.COURIER, Font.DEFAULTSIZE, 
Font.BOLD);
fonts[3] = new Font(Font.COURIER, Font.DEFAULTSIZE, Font.BOLD | Font.ITALIC);
                        fonts[4] = new Font(Font.HELVETICA, Font.DEFAULTSIZE, 
Font.NORMAL);
                        fonts[5] = new Font(Font.HELVETICA, Font.DEFAULTSIZE, 
Font.ITALIC);
                        fonts[6] = new Font(Font.HELVETICA, Font.DEFAULTSIZE, 
Font.BOLD);
fonts[7] = new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.BOLDITALIC);
                        fonts[8] = new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, 
Font.NORMAL);
                        fonts[9] = new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, 
Font.ITALIC);
                        fonts[10] = new Font(Font.TIMES_ROMAN, 
Font.DEFAULTSIZE, Font.BOLD);
fonts[11] = new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.BOLDITALIC);
                        fonts[12] = new Font(Font.SYMBOL);
                        fonts[13] = new Font(Font.ZAPFDINGBATS);
                        // add the content
                        for (int i = 0; i < 14; i++) {
document.add(new Paragraph("quick brown fox jumps over the lazy dog", fonts[i]));
                        }


                                
                        }
                        catch(DocumentException de) {
                                System.err.println("Document problem: " + 
de.getMessage());
                        } catch ( Exception ex) {
                                System.err.println("Document problem 2: " + 
ex.getMessage());
                        }
                        
                        // step 5: we close the document
                        document.close();

                        // we need to prepare NSData instead of String, since 
we are going to
                        // output arbitraty data
                        response.setContent( new NSData( oBuffer.toByteArray() 
) );

                        response.setHeader("application/pdf", "Content-Type");
                } catch ( Exception ex) {
                        System.err.println("PDFWriter not inited: " + 
ex.getMessage());
                }


        }
}


public class Main extends TAPDFComponent { // WOComponent {

    public Main(WOContext context) {
        super(context);
    }

    public WOComponent generateTestPDF()
    {

        return null;
    }

}
<<<


Don't forget to install the iText JAR found here

        http://www.lowagie.com/iText/

in

        /Library/Java/Extensions

or another JAVA-well known place.


Cheers,

Stefan

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to