Search code examples
javapdfclown

How can I skip lines when writing to a pdf file using PDF Clown?


How can I skip lines when writing to a pdf file using PDF Clown? I'm using it in IntelliJ.

I'm using it for very basic writing of text to a pdf file, it is hard to imagine that there is no instruction on how to skip lines.

Any help would be greatly appreciated.

// Create a document and add a page to it
    try {

        // 1. Instantiate a new PDF document!
        Document document = new File().getDocument();

        // 2. Add a page to the document!
        Page page = new Page(document); // Instantiates the page inside the document context.
        document.getPages().add(page); // Puts the page in the pages collection (you may choose an arbitrary position).

        // 3. Create a content composer for the page!
        PrimitiveComposer composer = new PrimitiveComposer(page);

        // 4. Add contents through the composer!
        composer.setFont(new StandardType1Font(document, StandardType1Font.FamilyEnum.Courier, true, false), 32);
        composer.showText("Hello World!", new Point2D.Double(32, 48));

        // 5. Flush the contents into the page!
        composer.flush();

        // 6. Save the document!
        document.getFile().save(job.getUpdateFileLocalDir() + "/pdfclown.pdf" , SerializationModeEnum.Standard);

    } catch (Exception e) {
        System.out.println("Exception is: ");
    }

Solution

  • Use BlockComposer for more advanced text stuff like that. Click here for a complex example utilizing paragraphs with BlockComposer. There are plenty of other examples and resources online on how to use it.

    Edit: Sample based on your code

    import java.awt.Dimension;
    import java.awt.geom.Dimension2D;
    import java.awt.geom.Point2D;
    import java.awt.geom.Rectangle2D;
    
    import org.pdfclown.documents.Document;
    import org.pdfclown.documents.Page;
    import org.pdfclown.documents.contents.composition.BlockComposer;
    import org.pdfclown.documents.contents.composition.PrimitiveComposer;
    import org.pdfclown.documents.contents.composition.XAlignmentEnum;
    import org.pdfclown.documents.contents.composition.YAlignmentEnum;
    import org.pdfclown.documents.contents.fonts.Font;
    import org.pdfclown.documents.contents.fonts.StandardType1Font;
    import org.pdfclown.files.File;
    import org.pdfclown.files.SerializationModeEnum;
    
    public class Main {
    
        private static final int Margin_X = 50;
        private static final int Margin_Y = 50;
    
        public static void main(String args[])
        {
            try {
    
                Document document = new File().getDocument();
    
                Page page = new Page(document);
                document.getPages().add(page); 
    
                Dimension2D pageSize = page.getSize();
    
                PrimitiveComposer composer = new PrimitiveComposer(page);
    
                BlockComposer blockComposer = new BlockComposer(composer);
    
                composer.beginLocalState();
    
                Rectangle2D frame = new Rectangle2D.Double(Margin_X, Margin_Y, pageSize.getWidth() - Margin_X * 2, pageSize.getHeight() - Margin_Y * 2);
    
                blockComposer.begin(frame,XAlignmentEnum.Left,YAlignmentEnum.Top);
    
                Dimension breakSize = new Dimension(24, 8); // Indentation (24pt)
                                                            // and top margin (8pt).
    
                composer.setFont(new StandardType1Font(document, StandardType1Font.FamilyEnum.Courier, true, false), 14);
    
                blockComposer.begin(frame, XAlignmentEnum.Left, YAlignmentEnum.Top);
    
                blockComposer.showBreak(breakSize);
                blockComposer.showText("There was nothing so VERY remarkable in that; nor did Alice think it so VERY much out of the way to hear the Rabbit say to itself, 'Oh dear! Oh dear! I shall be late!' (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); but when the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT- POCKET, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge.");
                blockComposer.showBreak(breakSize);
                blockComposer.showText("Wassup");
    
                blockComposer.end();
    
                composer.flush();
    
                document.getFile().save(job.getUpdateFileLocalDir() + "/pdfclown.pdf" , SerializationModeEnum.Standard);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }