Hi, firends !

     In the developing , I encounter a problem. I want to split the pdf
content into two parts use column(ColumnText). One part show text then
another show other things that they are not realation.

    Could you give me some advice? Thanks a lot!!

this is my source:

package samples.test;

import java.awt.Color;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

public class PdfFileEvent extends PdfPageEventHelper{

        public PdfFileEvent(){
                
        }
        protected Phrase header;
        protected PdfPTable footer;
        protected PdfTemplate total;
        protected BaseFont helv;
        protected PdfGState gstate;
        protected Color color;
        protected Image image;
        
        public void onEndPage(PdfWriter writer, Document document) {
                //footer operation
                PdfContentByte directcontent = writer.getDirectContent();
                directcontent.saveState();
                String text = "第 " + writer.getPageNumber() + " 页/共 ";
                float textBase = document.bottom() - 20;
                float textSize = helv.getWidthPoint(text, 15);
                directcontent.beginText();
                directcontent.setFontAndSize(helv, 15);
                directcontent.setTextMatrix((document.right() - 
document.left()) / 2,
textBase);
                directcontent.showText(text);
                directcontent.endText();
                directcontent.addTemplate(total, (document.right() - 
document.left()) / 2
+ textSize,
                                        textBase);
                directcontent.restoreState();
                //watermark operation
                try {
                        PdfContentByte contentunder = 
writer.getDirectContentUnder();
                        contentunder.saveState();
                        contentunder.setGState(gstate);
                        contentunder.addImage(image, image.getWidth() * 4, 0, 
0, image
                                        .getHeight() * 4, 120, 650);
                        contentunder.setColorFill(color);
                        contentunder.beginText();
                        contentunder.setFontAndSize(helv, 48);
                        contentunder.showTextAligned(Element.ALIGN_CENTER,
                                        "www.citypod.com" , document
                                                        
.getPageSize().getWidth() / 2, document.getPageSize()
                                                        .getHeight() / 2, 45);
                        contentunder.endText();
                        contentunder.restoreState();
                } catch (DocumentException e) {
                        e.printStackTrace();
                }
        }
        
        public void onOpenDocument(PdfWriter writer, Document document) {
                total = writer.getDirectContent().createTemplate(100, 100);
                total.setBoundingBox(new Rectangle(-20, -20, 100, 100));
                try {
                        helv = BaseFont.createFont("STSong-Light", 
"UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
                        image = Image
                                        
.getInstance("e:/workspace/iText/chapter10/resources/iTextLogo.gif");
                } catch (Exception e) {
                        throw new ExceptionConverter(e);
                }
                gstate = new PdfGState();
                gstate.setFillOpacity(0.3f);
                gstate.setStrokeOpacity(0.3f);
        }

        /**
         * @see
com.lowagie.text.pdf.PdfPageEvent#onStartPage(com.lowagie.text.pdf.PdfWriter,
         *      com.lowagie.text.Document)
         */
        public void onStartPage(PdfWriter writer, Document document) {
                PdfContentByte cb = writer.getDirectContent();
                ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, header,
                                        (document.right() - document.left()) / 2
                                                        + 
document.leftMargin(), document.top() + 10, 0);
                if (writer.getPageNumber() % 2 == 1) {
                        color = Color.blue;
                } else {
                        color = Color.red;
                }
        }

        

        /**
         * @see
com.lowagie.text.pdf.PdfPageEvent#onCloseDocument(com.lowagie.text.pdf.PdfWriter,
         *      com.lowagie.text.Document)
         */
        public void onCloseDocument(PdfWriter writer, Document document) {
                total.beginText();
                total.setFontAndSize(helv, 15);
                total.setTextMatrix(0, 0);
                total.showText(" " + (writer.getPageNumber() - 1) + " 页");
                total.endText();
        }
        
        
}


package samples.test;

import java.awt.Color;

import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;

public interface PdfFileFont {
        
        public static final Font FONT9 = FontFactory.getFont(
                        FontFactory.TIMES_ROMAN, 9);

        public static final Font FONT11 = FontFactory.getFont(
                        FontFactory.TIMES_ROMAN, 11);

        public static final Font FONT11B = FontFactory.getFont(
                        FontFactory.TIMES_ROMAN, 11, Font.BOLD);

        public static final Font FONT14B = FontFactory.getFont(
                        FontFactory.TIMES_ROMAN, 14, Font.BOLD);

        public static final Font FONT14BC = FontFactory.getFont(
                        FontFactory.TIMES_ROMAN, 14, Font.BOLD, new Color(255, 
0, 0));

        public static final Font FONT24B = FontFactory.getFont(
                        FontFactory.TIMES_ROMAN, 24, Font.BOLD);
}


main Class

package samples.test;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.List;
import com.lowagie.text.ListItem;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

public class Test extends PdfFileEvent implements PdfFileFont{
        
        public static void main(String args[]){
                createPdf("hello");
        }
        public Test(){
                super();
                header = new Phrase();
                Chunk c;
                c = new Chunk("citypod",FONT14B);
                c.setUnderline(new Color(0x00, 0xFF, 0x00), 1.5f, 0.0f, 0.0f,
                                -0.5f, 
PdfContentByte.LINE_CAP_PROJECTING_SQUARE);
                c.setHorizontalScaling(1.2f);
                header.add(c);
                
        }
        
        public static void createPdf(String fileName){
                Document document = new Document();
                try {
                        PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream("wo.pdf"));
                        
writer.setViewerPreferences(PdfWriter.PageLayoutOneColumn);
                        writer.setPageEvent(new Test());
                        document.open();
                        
                        document.open();
                        float gutter = 20;
                        int numColumns = 2;
                        float fullWidth = document.right() - document.left();
                        float leftColumnWidth  = (fullWidth - (numColumns - 1) 
* gutter) *
numColumns / 3;
                        float rightColumnWidth = (fullWidth - (numColumns - 1) 
* gutter) / 3;
                        float allColumns[] = new float[numColumns];
                        for(int i = 0; i < numColumns; i++){
                                allColumns[i] = document.left() + 
(leftColumnWidth + gutter) * i;
                        }
                        PdfContentByte cb = writer.getDirectContent();
                        ColumnText ct = new ColumnText(cb);
                        ct.setSimpleColumn(document.left(), 0, 
document.right(), document.top());
                        Phrase fullTitle = new Phrase("POJOs in Action");
                        ct.addText(fullTitle);
                        ct.go();
                        Phrase subTitle = new Phrase(
                                        "Developing Enterprise Applications 
with Lightweight Frameworks");
                        ct.addText(subTitle);
                        ct.go();
                        cb.setLineWidth(1);
                        float currentY = ct.getYLine();
                        currentY -= 4;
                        cb.moveTo(document.left(), currentY);
                        cb.lineTo(document.right(), currentY);
                        cb.stroke();
                        currentY = ct.getYLine();
                        currentY -= 15;
                        float topColumn = currentY;
                        for (int k = 1; k < numColumns; ++k) {
                                float x = allColumns[k] - gutter / 2;
                                cb.moveTo(x, topColumn);
                                cb.lineTo(x, document.bottom());
                        }                       
                        cb.stroke();
                        
                        //define a template
                        float x = allColumns[1] - gutter / 2;
                        PdfTemplate t = cb.createTemplate(600, 800);
                        t.moveTo(x, topColumn + 15);
                        t.lineTo(x, document.bottom());
                        t.fillStroke();
                        
                        int currentColumn = 0;
                        ct.setSimpleColumn(allColumns[currentColumn], 
document.bottom(),
                                        allColumns[currentColumn] + 
leftColumnWidth, currentY);
                        
                        Image img = 
Image.getInstance("chapter05/resources/dog.gif");
                        img.setAlignment(Image.LEFT | Image.TEXTWRAP);
                        ct.addElement(img);
                        
                        ct.addElement(newParagraph("Description", FONT14BC, 
15));
                        ct.addElement(newParagraph(
                                                        "In the past, 
developers built enterprise Java applications using EJB
technologies that are excessively complex and difficult to use. Often EJB
introduced more problems than it solved. There is a major trend in the
industry towards using simpler and easier technologies such as Hibernate,
Spring, JDO, iBATIS and others, all of which allow the developer to work
directly with the simpler Plain Old Java Objects or POJOs. Now EJB version 3
solves the problems that gave EJB 2 a black eye--it too works with POJOs.",
                                                        FONT11, 5));
                        Paragraph p = new Paragraph();
                        p.setSpacingBefore(5);
                        p.setAlignment(Element.ALIGN_JUSTIFIED);
                        Chunk anchor = new Chunk("POJOs in Action", FONT11B);
                        
anchor.setAnchor("http://www.manning.com/books/crichardson";);
                        p.add(anchor);
                        p.add(new Phrase(
                                                        " describes the new, 
easier ways to develop enterprise Java
applications. It describes how to make key design decisions when developing
business logic using POJOs, including how to organize and encapsulate the
business logic, access the database, manage transactions, and handle
database concurrency. This book is a new-generation Java applications guide:
it enables readers to successfully build lightweight applications that are
easier to develop, test, and maintain.",
                                                        FONT11));
                        ct.addElement(p);
                        ct.addElement(newParagraph("Inside the Book", FONT14BC, 
15));
                        List list = new List(List.UNORDERED, 15);
                        ListItem li;
                        li = new ListItem("How to develop apps in the post EJB 
2 world",
                                        FONT11);
                        list.add(li);
                        li = new ListItem(
                                        "How to leverage the strengths and work 
around the weaknesses of: JDO,
Hibernate, and EJB 3",
                                        FONT11);
                        list.add(li);
                        li = new ListItem("How to benefit by using aspects", 
FONT11);
                        list.add(li);
                        li = new ListItem(
                                        "How to do test-driven development with 
lightweight frameworks",
                                        FONT11);
                        list.add(li);
                        li = new ListItem("How to accelerate the 
edit-compile-debug cycle",
                                        FONT11);
                        list.add(li);
                        ct.addElement(list);
                        ct.addElement(newParagraph("About the Author...", 
FONT14BC, 15));
                        ct.addElement(newParagraph(
                                                        "Chris Richardson is a 
developer, architect and mentor with over 20
years of experience. He runs a consulting company that jumpstarts new
development projects and helps teams that are frustrated with enterprise
Java become more productive and successful. Chris has been a technical
leader at a variety of companies including Insignia Solutions and BEA
Systems. Chris holds a MA & BA in Computer Science from the University of
Cambridge in England. He lives in Oakland, CA.",
                                                        FONT11, 15));
                        for(int i = 0; i<1000;i++){
                        
ct.addElement(newParagraph("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",FONT14BC,15));
                        }
//                      while (true) {
//                              int status = ct.go();
//                              if (!ColumnText.hasMoreText(status))
//                                      break;
//                              // we run out of column. Let's go to another one
//                              ++currentColumn;
//                              if (currentColumn >= allColumns.length)
//                                      break;
//                              ct.setSimpleColumn(allColumns[currentColumn],
//                                              document.bottom(), 
allColumns[currentColumn]
//                                                              + 
rightColumnWidth, topColumn);
//                      }
                        
                        int status = ColumnText.START_COLUMN;
                        ColumnText ct2 = new ColumnText(cb);
                        for(int i = 0; i<1000;i++){
                                
ct2.addElement(newParagraph("bbbbbbbbbbbbbbbbb",FONT14BC,15));
                        }
                        while (ColumnText.hasMoreText(status)) {
                                if (currentColumn == 0){
                                        
ct.setSimpleColumn(allColumns[currentColumn],
                                                        document.bottom(), 
allColumns[currentColumn]
                                                                        + 
leftColumnWidth, topColumn);
                                        status = ct.go();
                                        currentColumn++;
                                }
                                if (currentColumn == 1){
                                        
ct.setSimpleColumn(allColumns[currentColumn],
                                                        document.bottom(), 
allColumns[currentColumn]
                                                                        + 
rightColumnWidth, topColumn);
                                        currentColumn++;
                                }
                                if (currentColumn > 1) {
                                        currentColumn = 0;
                                        document.newPage();
                                        
ct.setSimpleColumn(allColumns[currentColumn],
                                                        document.bottom(), 
allColumns[currentColumn]
                                                                        + 
leftColumnWidth, topColumn);
                                        cb.addTemplate(t, 0, 0);
                                }                               
                        }
                        
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (DocumentException e) {
                        e.printStackTrace();
                } catch (IOException e){
                        e.printStackTrace();
                }
                document.close();
        }
        
        private static Paragraph newParagraph(String s, Font f, float
spacingBefore) {
                Paragraph p = new Paragraph(s, f);
                p.setAlignment(Element.ALIGN_JUSTIFIED);
                p.setSpacingBefore(spacingBefore);
                return p;
        }
}


and the result :

http://www.nabble.com/file/p12421133/wo.pdf wo.pdf 

I want to add other pictures at the right of the vertical line!

-- 
View this message in context: 
http://www.nabble.com/Problem-%3A-split-the-pdf-content-use-column.-tf4358420.html#a12421133
Sent from the iText - General mailing list archive at Nabble.com.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to