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.

Results 1 to 3 of 3

Thread: Creating a pdf without 3rd library

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Creating a pdf without 3rd library

    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.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Creating a pdf without 3rd library

    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.

  3. #3
    Junior Member
    Join Date
    Dec 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a pdf without 3rd library

    Quote Originally Posted by Pischti View Post
    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.
    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.

Similar Threads

  1. problem with creating .pdf
    By deepanshu in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 18th, 2013, 05:16 AM
  2. Creating a Library, abstract class created issues. Also need GUI help.
    By rplee18 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 10th, 2013, 03:36 PM
  3. Creating Random unused ID number for every library member.
    By clarky2006 in forum Java Theory & Questions
    Replies: 95
    Last Post: March 20th, 2013, 06:33 AM
  4. creating a shared library
    By navinbecse in forum Threads
    Replies: 1
    Last Post: April 9th, 2010, 07:54 AM