2015-12-14 2 views
0

PdfClown кажется очень хорошим, но как я могу пропустить строки при записи в pdf-файл? Я использую его в IntelliJ.PdfClown кажется очень хорошим, но как я могу пропустить строки при записи в pdf-файл?

Я использую его для простой записи текста в 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: "); 
    } 
+0

Что вы имеете в виду под "пропустить линии"? Вы запрашиваете текстовую упаковку? –

+0

Я просто хочу написать несколько пустых строк для разделения текста, как завершена новая линия или возврат каретки? Или в этом отношении обертывание текста? Благодаря! – 65535

ответ

1

Используйте BlockComposer для получения более совершенного текстового материала. Click here для сложного примера, используя абзацы с BlockComposer. Есть много других examples и resources онлайн о том, как его использовать.

Edit: образца на основе кода

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

Спасибо за ответ. Используя предоставленное вами «абзацы», существует проблема с «blockComposer.showBreak (breakSize)», «IntelliJ дает ошибку:« Не удается разрешить символ «breakSize»; – 65535

+0

'breakSize' определяется ранее в коде как' Dimension breakSize = new Dimension (0,20); ' –

+0

Почему бы вам не попробовать весь пример, который они предоставляют? Второй, который я связал, проще. –

Смежные вопросы