You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Rajagopalan Srinivasan <ra...@gmail.com> on 2010/04/30 12:09:26 UTC
linux, windows difference
all
I have a simple program using pdfbox reproduced below. When I run it under
Ubuntu it creates the expected pdf file. On windows however I get a 3 page
empty pdf file. when I step thru code, it appears to be working ok.
Any ideas?
if it matters, I do have Adobe Acrobat installed on my windows machine.
package lister;
import java.io.*;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.font.*;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.edit.*;
import org.apache.fontbox.util.*;
public class Lister {
private PDDocument doc ;
private PDFont font ;
private void Create()
{
try
{
doc = new PDDocument() ;
}
catch (Exception exc)
{
}
font = PDType1Font.HELVETICA;
}
private void ListFile(String filename)
{
PDPage page = new PDPage();
PDRectangle pageFormat = new PDRectangle(843f,596f);
page.setMediaBox(pageFormat);
try
{
PDPageContentStream contentStream = new PDPageContentStream(doc,
page);
contentStream.beginText();
contentStream.setFont( font, 12 );
int linesperpage = (596 - 72) / 12 ;
contentStream.moveTextPositionByAmount( 72 , 596 - 36);
FileInputStream inp = new FileInputStream(filename);
DataInputStream dinp = new DataInputStream(inp);
BufferedReader reader = new BufferedReader(new
InputStreamReader(dinp));
String inpline ;
int lines= 0;
while ((inpline = reader.readLine()) != null)
{
contentStream.moveTextPositionByAmount(0 , -12);
contentStream.drawString(inpline);
lines++;
if (lines > linesperpage)
{
doc.addPage(page);
page = new PDPage() ;
page.setMediaBox(pageFormat);
contentStream.close();
contentStream = new PDPageContentStream(doc, page);
contentStream.beginText();
contentStream.setFont( font, 12 );
contentStream.moveTextPositionByAmount(72 , 596 - 36);
lines=0;
}
}
inp.close();
contentStream.close();
doc.addPage(page);
}
catch (Exception exc)
{
}
}
private void Save(String filename)
{
try
{
doc.save(filename);
}
catch (Exception exc)
{
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Lister pgmlister = new Lister();
pgmlister.Create();
for (int i=0; i<args.length; i++)
{
System.out.print("Adding File: ");
System.out.println(args[i]);
pgmlister.ListFile(args[i]);
}
pgmlister.Save("EmptyHello.pdf");
}
}