Hi,
I want create a simple pdf-document (a headline and a paragraph), but without a 3rd library like iText or other.
It can also be patched together text-based.
Does anyone have a tip or code-snippet?
Thanks in advance.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi,
I want create a simple pdf-document (a headline and a paragraph), but without a 3rd library like iText or other.
It can also be patched together text-based.
Does anyone have a tip or code-snippet?
Thanks in advance.
a) get to know the PDF file spec very intimately and/or b) use the source code of an open source project (eg PDFBox) as a guide.
Creating a PDF document without any library is almost impossible. I suggest that you try Free Spire.PDF for Java. The following code snippet shows you how to create a simple PDF document containing a headline and several paragraphs. The paragrahs are read from a .txt file.
import com.spire.pdf.graphics.*; import java.awt.*; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.io.*; public class CreatePdfDocument { public static void main(String[] args) throws FileNotFoundException, IOException { //create a PdfDocument object PdfDocument doc = new PdfDocument(); //add a page PdfPageBase page = doc.getPages().add(); //heading text String heading = "Java - Overview"; //create solid brush objects PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.BLUE)); PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.BLACK)); //create true type font objects PdfTrueTypeFont font1= new PdfTrueTypeFont(new Font("Times New Roman",Font.PLAIN,20)); PdfTrueTypeFont font2= new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,12)); //set the text alignment via PdfStringFormat class PdfStringFormat format1 = new PdfStringFormat(); format1.setAlignment(PdfTextAlignment.Center); //draw heading on the center of the page page.getCanvas().drawString(heading, font1, brush1, new Point2D.Float((float)page.getActualBounds(true).getWidth()/2, 0),format1); //get body text from a .txt file String body = readFileToString("C:\\Users\\Administrator\\Desktop\\body.txt"); //create a PdfTextWidget object PdfTextWidget widget = new PdfTextWidget(body, font2, brush2); //create a rectangle where the body text will be placed Rectangle2D.Float rect = new Rectangle2D.Float(0, 30, (float)page.getActualBounds(true).getWidth(),(float)page.getActualBounds(true).getHeight()); //set the PdfLayoutType to Paginate to make the content paginated automatically PdfTextLayout layout = new PdfTextLayout(); layout.setLayout(PdfLayoutType.Paginate); //draw body text on the page widget.draw(page, rect, layout); //save to file doc.saveToFile("output/CreatePdf.pdf"); } //customize a function to read TXT file to String private static String readFileToString(String filepath) throws FileNotFoundException, IOException { StringBuilder sb = new StringBuilder(); String s =""; BufferedReader br = new BufferedReader(new FileReader(filepath)); while( (s = br.readLine()) != null) { sb.append(s + "\n"); } br.close(); String str = sb.toString(); return str; } }
Last edited by jacky; November 10th, 2020 at 03:20 AM.