2014-02-28 213 views
1

我正在使用iTextSharp在我的應用程序中創建PDF。不過,我必須重新創建一個表格,我必須爲一個非常小的列設置大小。下面是顯示我要設置列大小的圖片: enter image description herePdfPTable單元格寬度

當表創建所有的休息是很好,我不能設置寬度

代碼:

PdfPTable table = new PdfPTable(2); 
table.WidthPercentage = 82.0f; 
PdfPCell cell = new PdfPCell(new Phrase("Com a assinatura autógrafa, o signatário desta Auto-declaração garante ter cumprido estas condições:", fontetexto)); 
cell.PaddingBottom = 10f; 
cell.PaddingTop = 10f; 
cell.Colspan = 2; 
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right 
table.AddCell(cell); 
table.AddCell("1. "); 
table.AddCell("Os óleos e gorduras vegetais velhos fornecidos são biomassa conforme o Decreto de biomassa."); 

回答

5

嘗試使用此代碼

PdfPTable table = new PdfPTable(new float[] { 30f, 400f }); 
table.HorizontalAlignment = 0; 
table.TotalWidth = 500f; 
table.LockedWidth = true; 
table.SetWidths(widths); 
+1

'該文件已損壞'。我認爲錯誤是'float [] widths' – CesarMiguel

+0

你可以參考這個鏈接http://stackoverflow.com/a/13580935/2911047 –

+1

好吧,像這樣工作:'PdfPTable table = new PdfPTable(new float [] {30f ,400f});' – CesarMiguel

1

從@IntellectWizard答案幫我達成解決方案。

float[] widths = new float[] { 100f, 4000f }; // Code @IntellectWizard

給出了一個損壞的文件,然後我嘗試:

PdfPTable table = new PdfPTable(new float[] { 30f, 400f });

這工作!

2

你可以,如果你使用XmlParser的而不是正常的HTMLParser

var document = new Document(); 
var workStream = new MemoryStream(); // or you can use fileStream also. 
PdfWriter writer = PdfWriter.GetInstance(document, workStream); 
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, stream, Encoding.UTF8); 

入住此的NuGet MvcRazorToPdf用少得多的努力做到這一點,我發現這更好然後RazorPDF

2

下面的代碼爲我工作:

PdfPTable table = new PdfPTable(2); 
table.TotalWidth = 500; 
table.SetTotalWidth(new float[] { 30f, 400f }); 

PdfPCell c1 = new PdfPCell(); 
PdfPCell c2 = new PdfPCell(); 

c1.AddElement(new Phrase("first column")); 
c1.AddElement(new Phrase("second column")); 

table.Rows.Add(new PdfPRow(new PdfPCell[] { c1, c2 }));