2017-02-26 7 views
1

기존 페이지의 맨 아래에 히브리어로 텍스트를 추가해야합니다. 나는 그럭저럭 할 수 있었다. 그러나 나는 중심에 정렬 될 본문이 필요하다. pdftable에는 하나의 열만 있습니다. 또한 열을 100 % 너비로 지정해야합니다. 코드에서 일어나는 이상한 일이 있습니다itext columntext를 정렬하고 폭을 만드는 방법 100 %

Dim LastPage As Integer = pdfRd.NumberOfPages 'The page number 
Dim cb As iTextSharp.text.pdf.PdfContentByte = Nothing 
Dim ct = New ColumnText(cb) 
Dim rows As Single() = {500} 'create column sizes 
Dim nTbl As PdfPTable = New PdfPTable(1) 'number of columns in our case 1 
Dim cellFont As iTextSharp.text.Font = New iTextSharp.text.Font(BaseFont, 10, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.RED) 

nTbl.RunDirection = PdfWriter.RUN_DIRECTION_RTL 

nTbl.WidthPercentage = 100 'Table size is set to 100% of the page 
nTbl.LockedWidth = True 
nTbl.SetTotalWidth(rows) ' Set the column widths on table 

nTbl.DefaultCell.Border = 0 
nTbl.AddCell(New iTextSharp.text.Phrase(MsgOnClosing, cellFont)) 
nTbl.SetExtendLastRow(1, 1) 
' nTbl.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT 'align table 
ct.Alignment = iTextSharp.text.Element.ALIGN_CENTER 
'ct.SetSimpleColumn(70, 36, iTextSharp.text.PageSize.A4.Width - 36, iTextSharp.text.PageSize.A4.Height - 300) 

nTbl.WriteSelectedRows(0, -1, 0, 20, stamp.GetOverContent(LastPage)) 'coords x=300,y=300      
ct.Go() 

답변

1

:

내 코드입니다.

ColumnText 개체를 만들지 만 아무 것도하지 않습니다. ct에 대한 모든 참조를 제거하고 코드는 이전과 동일 PDF를 생성 :

nTbl.WidthPercentage = 100 'Table size is set to 100% of the page 
nTbl.LockedWidth = True 
nTbl.SetTotalWidth(rows) ' Set the column widths on table 

이하지 않는 : 나는에 대해 나열된 코드를 보면

Dim LastPage As Integer = pdfRd.NumberOfPages 'The page number 
Dim rows As Single() = {500} 'create column sizes 
Dim nTbl As PdfPTable = New PdfPTable(1) 'number of columns in our case 1 
Dim cellFont As iTextSharp.text.Font = New iTextSharp.text.Font(BaseFont, 10, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.RED) 
nTbl.RunDirection = PdfWriter.RUN_DIRECTION_RTL 
nTbl.WidthPercentage = 100 'Table size is set to 100% of the page 
nTbl.LockedWidth = True 
nTbl.SetTotalWidth(rows) ' Set the column widths on table 
nTbl.DefaultCell.Border = 0 
nTbl.AddCell(New iTextSharp.text.Phrase(MsgOnClosing, cellFont)) 
nTbl.SetExtendLastRow(1, 1) 
nTbl.WriteSelectedRows(0, -1, 0, 20, stamp.GetOverContent(LastPage)) 'coords x=300,y=300 

, 나는 좀 더 이상한 것들을 볼 말이 되네. PdfPTable의 너비는 백분율 (WidthPercentage 사용) 또는 절대 너비 (setTotalWidth 사용)로 나타낼 수 있습니다. LockedWidth 값은 백분율 사용 여부 (false) 또는 절대 폭 (true)을 결정합니다. 귀하의 경우, 절대 너비를 사용하도록 선택하므로 폭 비율을 설정하는 것은 의미가 없습니다.

WriteSelectedRows() 메서드를 사용하여 테이블을 추가하기 때문에 의미가 없습니다. WriteSelectedRows을 사용하는 경우 절대 너비를 사용해야합니다.

nTbl.SetTotalWidth(iTextSharp.text.PageSize.A4.Width - 72) 

을하지만 당신은 바로 좌표를 가지고 있는지 확인하십시오 : 귀하의 경우,이 같은 그 폭을 정의 할

nTbl.WriteSelectedRows(0, -1, 36, 20, stamp.GetOverContent(LastPage)) 

당신은 또한 당신이 rows 변수를 정의 라인을 던져해야합니다. 그리고 왜 nTbl.SetExtendLastRow(1, 1)을 추가 했습니까? 이는 WriteSelectedRows() 접근 방식과 함께 사용하면 효과가 없습니다.

대안 # 1 : 당신이 원하는 것을 달성하기 위해 다른 방법도 있습니다

, 당신은 참으로 ColumnText를 사용할 수 있지만이 경우 당신은 칼럼에 PdfPTable을 추가해야합니다. 당신도 이해가되지 않았다 변수 cb와 함께했던 그 무엇을 참고, 내가 그 라인을 멀리 던져 : 다시

Dim LastPage As Integer = pdfRd.NumberOfPages 'The page number 
Dim ct = New ColumnText(stamp.GetOverContent(LastPage)) 
Dim nTbl As PdfPTable = New PdfPTable(1) 'number of columns in our case 1 
Dim cellFont As iTextSharp.text.Font = New iTextSharp.text.Font(BaseFont, 10, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.RED) 
nTbl.RunDirection = PdfWriter.RUN_DIRECTION_RTL 
nTbl.WidthPercentage = 100 'Table size is set to 100% of the page 
nTbl.DefaultCell.Border = 0 
nTbl.AddCell(New iTextSharp.text.Phrase(MsgOnClosing, cellFont)) 
ct.SetSimpleColumn(36, 0, iTextSharp.text.PageSize.A4.Width - 72, 36) 
ct.Go() 

을 좀 줄을 던져했다. 예를 들어, 테이블 너비를 100 %로 설정하면 왜 열 정렬을 설정해야합니까?

대안 # 2 : 코드

주요 문제는 뒤얽힌 점이다. 히브리어 텍스트를 모두 추가하려는 경우 왜 PdfPtable을 사용합니까?

이전 iText 5 대신 iText 7 및 pdfCalligraph 애드온을 사용하면 올바른 방향이 바뀝니다.그러나이 iText 5를 사용하여 주장하는 경우, 당신은 간단한 이런 ColumnText에 히브리어 텍스트를 추가 할 수 있습니다 :

Dim LastPage As Integer = pdfRd.NumberOfPages 'The page number 
Dim ct = New ColumnText(stamp.GetOverContent(LastPage)) 
ct.SetSimpleColumn(36, 0, iTextSharp.text.PageSize.A4.Width - 72, 36) 
ct.RunDirection = PdfWriter.RUN_DIRECTION_RTL 
Dim myFont As iTextSharp.text.Font = New iTextSharp.text.Font(BaseFont, 10, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.RED) 
ct.AddElement(New iTextSharp.text.Paragraph(MsgOnClosing, cellFont)) 
ct.Go() 

당신이 코드의 17 개 애매한 라인을 보낼 것입니다 왜 7 개 라인에서 동일한 결과를 얻을 수 있습니다 때 맑은가요?

+0

감사합니다. Bruno. SetSimpleColumn을 사용하면 pdf 크기가 동적이기 때문에 결과 값이 항상 동일하게 유지됩니까? 새 콘텐츠의 위치가 영향을받지 않습니까? – nkotb

+0

분명히 하드 코딩 된 값을 적용해야합니다. 현재이 코드는 '(0, 0)'([좌표계의 원점] (http://developers.itextpdf.com/question/where-origin-xy-pdf-page))은 왼쪽 아래 구석, 그리고 각 페이지의 너비는 A4 페이지의 너비입니다. 페이지 크기가 예측할 수없는 경우 [각 페이지의 페이지 크기 얻기] (http://developers.itextpdf.com/question/how-get-pdf-page-width-and-height)가 필요합니다. [페이지를 기준으로 텍스트를 배치하는 방법]을 참조하십시오. (http://developers.itextpdf.com/question/how-position-text-relative-page) –

+0

새 텍스트가 다음과 같도록 위치를 수정하려고합니다. 페이지의 맨 아래에 있지만 여전히 SetSimpleColumn 어디에서 텍스트를 페이지의 맨 아래에 나타납니다 값을 설정할 이해하지 않습니다. 나는 당신의 연결 고리를 보았다. – nkotb