Hi,

I'm reading in a flat file with some lines starting with several
whitespace characters and then normal text, and also somelines that are
just newlines. PDF output appears to chomp the 1st whitespace character
for the lines that start with whitespace, and for the lines with only
newlines, appears to add a single whitespace character. I'd like to
preserve all leading whitespace, but don't really care too much about
blank lines getting an additional whitespace character. Is there some
setting I'm needing to preserve all the leading whitespace on each line
in the output PDF?

I'm using itext-5.0.4.jar.
I'm viewing the pdf in Adobe reader 9.4, and also Foxit Reader 4.2.0


Sorry if this is too much info, but below is my attempt & notes:

I'm using a copy of the sample exercise source code and just modifying
to see if I can get things to work in java...

I'm trying to read in the file line by line using the java io
BufferedReader
If the line doesn't match a special character (control+L), it will add
the current line to the string buffer variable mybuf. The idea being to
buffer up the file contents until a control+L is found, then create a
PDF paragraph/page.

The system.out.println stuff shows up in Eclipse's console tab, and
appears to show that the leading whitespace is really there, but just
not in the PDF output file?

If I add in the line: mybuf.append(" "); right before the line=
mybuf.append(mycontent), the resulting PDF appears to add a leading
whitespace to some of the lines...

/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */

package part1.chapter01;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.FileUtils; 

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.regex.*;

/**
 * First iText example: Hello World.
 * 
*/
public class HelloWorld_viafile {

    /** Path to the resulting PDF file. */
    public static final String RESULT
        = "results/part1/chapter01/hello_viafile.pdf";
    
    /**
     * Creates a PDF file: hello.pdf
     * @param    args    no arguments needed
     */
    public static void main(String[] args)
        throws DocumentException, IOException {
        new HelloWorld_viafile().createPdf(RESULT);
    }

    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws    DocumentException 
     * @throws    IOException 
     */
    public void createPdf(String filename)
        throws DocumentException, IOException {
        // step -1 read in content text file
        
        File myfile = new File("inputfile.txt");
                
        
        // step 1
        Document document = new Document(PageSize.LETTER);
        document.setMargins(12,12,12,12);
        // step 2
        PdfWriter mywriter = PdfWriter.getInstance(document, new
FileOutputStream(filename));
        mywriter.setStrictImageSequence(true);
        // step 3 - have to put it here early on or else nothing but
blank
        //document.open();
        
        // step 4
        // step 4.1 fonts
        BaseFont bf1 = BaseFont.createFont("c:/windows/fonts/cour.ttf",
                BaseFont.CP1252, BaseFont.EMBEDDED);
        BaseFont bf2 = BaseFont.createFont("c:/windows/fonts/cour.ttf",
                BaseFont.CP1252, BaseFont.EMBEDDED);
        Font font1 = new Font(bf1, 10);
        Font font2 = new Font(bf2, 8);
        
        // loop through file and parse and take actions
        Pattern p = Pattern.compile("\\cL"); //control +L
        String mycontent = "" ;
        BufferedReader br = new BufferedReader(new FileReader(myfile));
        StringBuffer mybuf = new StringBuffer() ;
        
        document.open();
        
        while (( (mycontent=br.readLine())) != null )
        {
                Matcher m = p.matcher(mycontent);
                if (m.find()){
                        // found ^L
                        document.add(new
Paragraph(mybuf.toString(),font1));
                        // clear buffer
                        mybuf.setLength(0);
                        // new page
                        document.newPage();
                } else {
                        //System.out.println("debug: mycontent");
                        //mybuf +=  mycontent +"\n";
                        mybuf.append(mycontent );
                        mybuf.append("\n");
                        //System.out.print(mycontent.length() + " = ");
                        //System.out.println(mycontent);
                        System.out.print(mybuf.length() + " = " );
                        System.out.println(mybuf);
                } //end if
        }
        document.close();
        System.out.println("All
done-------------------------------------------");
    }
    
}

Thanks
------------------------------------------------------------------------------
Download new Adobe(R) Flash(R) Builder(TM) 4
The new Adobe(R) Flex(R) 4 and Flash(R) Builder(TM) 4 (formerly 
Flex(R) Builder(TM)) enable the development of rich applications that run
across multiple browsers and platforms. Download your free trials today!
http://p.sf.net/sfu/adobe-dev2dev
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to