簡體   English   中英

使用PDF小丑寫pdf文件時如何跳過行?

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

使用PDF小丑寫pdf文件時如何跳過行? 我在IntelliJ中使用它。

我將其用於將文本非常基本地寫入pdf文件中,很難想象沒有關於如何跳過行的說明。

任何幫助將不勝感激。

// 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: ");
    }

使用BlockComposer處理類似的高級文本內容。 單擊此處,獲取使用帶有BlockComposer段落的復雜示例。 在線上還有許多其他示例資源介紹了如何使用它。

編輯:根據您的代碼示例

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();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM