2013-02-04 13 views
11

मैं PDFBox बुद्धि hthe उपयोग कर रहा हूँ कोड निम्नलिखित:pdfbox रैप पाठ

doc = new PDDocument(); 
page = new PDPage(); 

doc.addPage(page); 
PDFont font = PDType1Font.COURIER; 

pdftitle = new PDPageContentStream(doc, page); 
pdftitle.beginText(); 
pdftitle.setFont(font, 12); 
pdftitle.moveTextPositionByAmount(40, 740); 
pdftitle.drawString("Here I insert a lot of text"); 
pdftitle.endText(); 
pdftitle.close(); 

क्या किसी को पता है कि मैं पाठ कैसे लपेट कर सकते हैं, जिससे कि वह एक और लाइन के लिए चला जाता है? आपका बहुत बहुत धन्यवाद!

उत्तर

0

मैं pdfBOX

सामान्य में में LINEBREAK समस्या के लिए एक समाधान पाया देखें, तो आप तीन चरणों की जरूरत है अपने पाठ रैप करने के लिए:

1) स्ट्रिंग है कि लिपटे होने के लिए प्रत्येक शब्द विभाजित और उन्हें स्ट्रिंग की सरणी में डाल दें, उदाहरण के लिए स्ट्रिंग [] भागों

2) स्ट्रिंगबफर के साथ एक सरणी बनाएं (टेक्स्टलेथेंथ/(रेखा में वर्णों की संख्या), उदा। 280/70 = 5 >> हमें 5 लाइनब्रेक चाहिए!

3), StringBuffer [i] में भागों डाल एक पंक्ति में वर्णों की अधिकतम संख्या की सीमा की अनुमति दी है जब तक,

4) stringbuffer.length जब तक पाश < लाइनब्रेक

विधि splitString वह तरीका है जो यह करता है। विधि writeText सिर्फ पीडीएफ

यहाँ करने के लिए लिपटे पाठ ड्रॉ एक उदाहरण

import java.io.IOException; 
import java.util.ArrayList; 

import org.apache.pdfbox.exceptions.COSVisitorException; 
import org.apache.pdfbox.pdmodel.PDDocument; 
import org.apache.pdfbox.pdmodel.PDPage; 
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; 
import org.apache.pdfbox.pdmodel.font.PDFont; 
import org.apache.pdfbox.pdmodel.font.PDType1Font; 

public class pdfTest{ 
private ArrayList<String> arrayList; 
private PDDocument document; 
private PDFont font = PDType1Font.HELVETICA; 

pdfTest(PDDocument document, ArrayList arrayList, PDFont font) throws COSVisitorException, IOException { 
    this.document = document; 
    this.arrayList = arrayList; 
    this.font = font; 
    writeText(document, arrayList, 30, 750, font); //method for easily drawing a text into a pdf 
} //constructor 


public void writeText(PDDocument document, ArrayList arrayList, int positionX, int positionY, PDFont font) throws IOException, COSVisitorException { 
    PDPage page = new PDPage(); 
    document.addPage(page); 

    // Start a new content stream 
    PDPageContentStream contentStream = new PDPageContentStream(document, page); 

    // Define a text content stream using the selected font, moving the cursor and drawing the text in arrayList 
    for(int i=0;i<arrayList.size();i++) { 
     String text=(String) arrayList.get(i); 
     String [] tmpText = splitString(text); 
     for(int k=0;k<tmpText.length;k++) { 
      contentStream.beginText(); 
      contentStream.setFont(font, 12); 
      contentStream.moveTextPositionByAmount(positionX, positionY); 
      contentStream.drawString(tmpText[k]);   
      contentStream.endText(); 
      positionY=positionY-20; 
     }   
     contentStream.setLineWidth((float) 0.25); 
    } 

    // Make sure that the content stream is closed: 
    contentStream.close();  
    document.save("Test.pdf"); 
    document.close(); 
} //main 

public static void main(String[] args) throws COSVisitorException, IOException { 
    ArrayList arrayList = new ArrayList<String>(); 

    PDDocument document = new PDDocument(); 
    PDFont font = PDType1Font.HELVETICA; 
    PDPage page = new PDPage(); 

    arrayList.add(  "12345 56789 0 aaa bbbew wel kwäer kweork merkweporkm roer wer wer e er" 
         + "df sdmfkl slkdfm sdkfdof sopdkfp osdkfo sädölf söldm,f sdkfpoekr re, ä" 
         + " sdfk msdlkfmsdlk fsdlkfnsdlk fnlkdn flksdnfkl sdnlkfn kln df sdmfn sn END"); 
    arrayList.add("this is an example"); 
    arrayList.add("java pdfbox stackoverflow");   

    new pdfTest(document,arrayList,font); 
    System.out.println("pdf created!"); 
} 

public String [] splitString(String text) { 
    /* pdfBox doesnt support linebreaks. Therefore, following steps are requierd to automatically put linebreaks in the pdf 
     * 1) split each word in string that has to be linefeded and put them into an array of string, e.g. String [] parts 
     * 2) create an array of stringbuffer with (textlength/(number of characters in a line)), e.g. 280/70=5 >> we need 5 linebreaks! 
     * 3) put the parts into the stringbuffer[i], until the limit of maximum number of characters in a line is allowed, 
     * 4) loop until stringbuffer.length < linebreaks 
     * 
     */ 
    int linebreaks=text.length()/80; //how many linebreaks do I need? 
    String [] newText = new String[linebreaks+1];  
    String tmpText = text; 
    String [] parts = tmpText.split(" "); //save each word into an array-element 

    //split each word in String into a an array of String text. 
    StringBuffer [] stringBuffer = new StringBuffer[linebreaks+1]; //StringBuffer is necessary because of manipulating text 
    int i=0; //initialize counter 
    int totalTextLength=0; 
    for(int k=0; k<linebreaks+1;k++) { 
     stringBuffer[k] = new StringBuffer(); 
     while(true) {    
      if (i>=parts.length) break; //avoid NullPointerException 
      totalTextLength=totalTextLength+parts[i].length(); //count each word in String    
      if (totalTextLength>80) break; //put each word in a stringbuffer until string length is >80 
      stringBuffer[k].append(parts[i]); 
      stringBuffer[k].append(" "); 
      i++; 
     } 
     //reset counter, save linebreaked text into the array, finally convert it to a string 
     totalTextLength=0; 
     newText[k] = stringBuffer[k].toString(); 
    } 
    return newText; 
} 

} 
+5

की चौड़ाई वर्णों की संख्या पर एक सीमा का उपयोग करते हुए प्रत्येक पंक्ति में लगता है अधिकांश फोंट के लिए अपर्याप्त। इसके बजाय आप पीडीएफ में खींची गई रेखा की वास्तविक लंबाई को सीमित करना चाहेंगे। [यह उत्तर] (http://stackoverflow.com/questions/19635275/how-to-generate-multiple-lines-in-pdf-using-apache-pdfbox/19683618#19683618) उस सम्मान में आपकी सहायता कर सकता है – mkl

4

यह मेरे लिए काम किया है। WordUtils का एक संयोजन और

String[] wrT = null; 
String s = null; 
text = "Job Description: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque hendrerit lectus nec ipsum gravida placerat. Fusce eu erat orci. Nunc eget augue neque. Fusce arcu risus, pulvinar eu blandit ac, congue non tellus. Sed eu neque vitae dui placerat ultricies vel vitae mi. Vivamus vulputate nullam."; 
    wrT = WordUtils.wrap(text, 100).split("\\r?\\n"); 
     for (int i=0; i< wrT.length; i++) { 
       contents.beginText(); 
       contents.setFont(PDType1Font.HELVETICA, 10); 
       contents.newLineAtOffset(50,600-i*15); 
       s = wrT[i]; 
       contents.showText(s); 
       contents.endText(); 
      } 
+0

कहां किया गया शब्द उपयोग से आते हैं? – jrahhali

+0

@jrahhali org.apache.commons.text.WordUtils तरह लग रहा है – Wodin

0

PdfBox और Boxable दोनों ऑटो सेल चौड़ाई से अधिक समय पाठ का हिस्सा लपेटता विभाजित है, तो इसका मतलब है कि अगर सेल चौड़ाई = 80 वाक्य चौड़ाई = 100 के शेष भाग चौड़ाई 20 के पाठ अगली पंक्ति से शुरू कर देंगे (नोट:। मैं चौड़ाई (वास्तविक अंतरिक्ष की सजा से भस्म) और नहीं लंबाई (उल्लेख किया है कोई पात्रों में से))

तो वाक्य चौड़ाई = 60, चौड़ाई 20 की पाठ सेल की चौड़ाई को भरने की आवश्यकता होगी, और इसके बाद कोई भी पाठ अगली पंक्तिपर जाएगासमाधान: भरने के इस चौड़ाई रिक्त स्थान

सेल के न भरे अंतरिक्ष = cellWidth के साथ 20 - sentenceWidth, numberOfSpaces = सेल के न भरे अंतरिक्ष/एक भी अंतरिक्ष

private String autoWrappedHeaderText(String text, float cellWidth) { 
    List<String> splitStrings = Arrays.asList(text.split("\n")); 
    String wholeString = ""; 
    for (String sentence : splitStrings) { 
     float sentenceWidth = FontUtils.getStringWidth(headerCellTemplate.getFont(), " " + sentence + " ", 
       headerCellTemplate.getFontSize()); 
     if (sentenceWidth < cellWidth) { 
      float spaceWidth = FontUtils.getStringWidth(headerCellTemplate.getFont(), " ", 
        headerCellTemplate.getFontSize()); 
      int numberOfSpacesReq = (int) ((cellWidth - sentenceWidth)/spaceWidth); 
      wholeString += sentence; 
      for (int counter = 0; counter < numberOfSpacesReq; counter++) { 
       wholeString += " "; 
      } 
     } 
    } 

    return wholeString; 
} 
cell = headerRow.createCell(cellWidth * 100/table.getWidth(), headerText, HorizontalAlignment.LEFT, VerticalAlignment.TOP); 
संबंधित मुद्दे