2013-03-14 38 views
10

我有一個網站,用戶上傳照片和創建相冊。而且,他們可以在絕對位置,旋轉和對齊中添加文本。文本可以有新的行。旋轉段落或單元格一些任意度 - Itext

我一直在使用Itext庫自動化後期印刷的Photobooks高質量PDF的創建。

將用戶上傳的圖像添加到PDF中非常簡單,當我嘗試添加文本時出現問題。

從理論上講,我需要做的是定義一些定義寬度和高度的段落,設置用戶文本,字體,字體樣式,對齊方式(居中,左右,對齊),最後設置迴轉。

對於我讀過的Itext,我可以創建一個設置用戶屬性的段落,並使用ColumnText對象設置絕對位置,寬度和高度。但是,不可能設置大於單行的任何東西的旋轉。

我不能使用表格單元格要麼,因爲旋轉法只允許是90

倍數度是否有辦法增加一個段落與一些旋轉(例如20度),而無需添加一行一行地使用ColumnText.showTextAligned()方法和所有涉及到的數學方法?

----編輯:2008年以前,2013 ----

如果有幫助的人,這是我用來解決這個問題的代碼(感謝布魯諾):

//Create the template that will contain the text 
PdfContentByte canvas = pdfWriter.getDirectContent(); 
PdfTemplate textTemplate = canvas.createTemplate(imgWidth, imgHeight); //The width and height of the text to be inserted 

ColumnText columnText = new ColumnText(textTemplate); 

columnText.setSimpleColumn(0, 0, imgWidth, imgHeight); 
columnText.addElement(paragraph); 

columnText.go(); 

//Create de image wraper for the template 
Image textImg = Image.getInstance(textTemplate); 

//Asign the dimentions of the image, in this case, the text 
textImg.setInterpolation(true); 
textImg.scaleAbsolute(imgWidth, imgHeight); 
textImg.setRotationDegrees((float) -textComp.getRotation()); //Arbitrary number of degress 
textImg.setAbsolutePosition(imgXPos, imgYPos); 

//Add the text to the pdf 
pdfDocument.add(textImg); 
+1

編輯與我使用的代碼的問題。 – BernalCarlos 2013-08-08 15:03:19

回答

10
  • 創建一個PdfTemplate對象;只是一個矩形。
  • 在此PdfTemplate上繪製您的ColumnText;不要擔心旋轉,只需在矩形中填入想要添加到列中的任何內容即可。
  • PdfTemplate包裝在Image對象內;這只是爲了方便,避免數學。這並不意味着您的文本將被光柵化。
  • 現在將旋轉和絕對位置應用到Image並將其添加到文檔中。

你的問題現在已經解決了;-)

PS:我是iText的在行動的書籍。

+0

你是對的!問題解決了。非常感謝。 – BernalCarlos 2013-03-15 16:57:55

+0

@BrunoLowagie,按照iText in Action(2nd Ed。)的3.3.1章節的說法,這也是在使用'ColumnText.setSimpleColumn()'和'.setText(myPhrase)'時旋轉文本的最佳方式嗎? – Ben 2015-01-15 17:01:55

+0

看看如何在[StackOverflow上的最佳iText問題]中回答這個問題(https://leanpub.com/itext_so)。其實,你的問題是在編輯回答這個問題。唯一的區別是BernalCarlos在複合模式下使用'ColumnText',而您使用的是文本模式。免費電子書解釋了不同之處。 – 2015-01-16 08:20:58

1

多虧了我們的朋友(布魯諾& BernalCarlos) 我最後的代碼,在他們的項目中使用「RTL」用戶是在這裏:

// step 1 
Document document = new Document(); 
document.setPageSize(PageSize.A4); 

// step 2 
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(destination_file)); 
CreateBorder event = new CreateBorder(); 
writer.setPageEvent(event); 

// step 3 
document.open(); 

// step 4 
int imgWidth=400; 
int imgHeight=50; 
//Create the template that will contain the text 
PdfContentByte canvas = writer.getDirectContent(); 
PdfTemplate textTemplate = canvas.createTemplate(imgWidth, imgHeight); 
//The width and height of the text to be inserted 

ColumnText columnText = new ColumnText(textTemplate); 
columnText.setSimpleColumn(0, 0, imgWidth, imgHeight); 
columnText.setRunDirection(PdfWriter.RUN_DIRECTION_RTL); 
columnText.addElement(new Paragraph("محاسبه بار غیر متعادل", font_IranSemiBold)); 
columnText.go(); 

//Create de image wraper for the template 
Image textImg = Image.getInstance(textTemplate); 

//Asign the dimentions of the image, in this case, the text 
textImg.setInterpolation(true); 
textImg.scaleAbsolute(imgWidth, imgHeight); 
textImg.setRotationDegrees(90); //Arbitrary number of degress 
textImg.setAbsolutePosition(50, 200); 

//Add the text to the pdf 
document.add(textImg); 

// step 5 
document.close();