2012-11-27 10 views
15

में PdfPCell के लिए चौड़ाई सेट करने के मैं तालिका में PdfpCell के लिए चौड़ाई सेट करना चाहते हैं, मैं चाहता हूँ इसकैसे iTextSharp

enter image description here
मैं लिखें इस कोड

PdfPCell cell; 
      PdfGrid tableHeader; 
      PdfGrid tmpTable; 
      PdfGrid table = new PdfGrid(numColumns: 1) { WidthPercentage = 100, RunDirection = PdfWriter.RUN_DIRECTION_LTR, ExtendLastRow = false }; 

      string imagepath2 = HttpRuntime.AppDomainAppPath + "Header.JPG"; 
      cell = new PdfPCell() { Border = 0, RunDirection = PdfWriter.RUN_DIRECTION_RTL }; 
      cell.Image = iTextSharp.text.Image.GetInstance(imagepath2); 
      table.AddCell(cell); 


      tableHeader = new PdfGrid(numColumns: 10); 
      tableHeader.RunDirection = PdfWriter.RUN_DIRECTION_LTR; 

      tmpTable = new PdfGrid(numColumns: 1); 
      tmpTable.TotalWidth = 10f; 
      tmpTable.LockedWidth = true; 
      cell = new PdfPCell() {Rotation =-90,VerticalAlignment =Element.ALIGN_MIDDLE, HorizontalAlignment =Element.ALIGN_CENTER, BorderWidth = 1}; 
      cell.Phrase = fontSelector.Process("SER. No"); 

      tmpTable.AddCell(cell); 

      cell = new PdfPCell(tmpTable) { BorderWidth = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
      tableHeader.AddCell(cell); 

      //////////////////////////////////////////////////////////// 
      tmpTable = new PdfGrid(numColumns: 1); 
      cell = new PdfPCell() { Border = 0, BorderWidthBottom = 1,VerticalAlignment =Element.ALIGN_MIDDLE, HorizontalAlignment =Element.ALIGN_CENTER }; 
      cell.Phrase = fontSelector.Process("TYPE OF SHIPPING"); 
      // 
      tmpTable.AddCell(cell); 


      cell = new PdfPCell() { Border = 0, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
      cell.Phrase = fontSelector.Process("AWB/BL NO."); 
      tmpTable.AddCell(cell); 

      cell = new PdfPCell(tmpTable) { BorderWidth = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
      tableHeader.AddCell(cell); 
      /////////////////////////////////////////////////////////////// 

      tmpTable = new PdfGrid(numColumns: 1); 
      cell = new PdfPCell() { Border = 0, BorderWidthBottom = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
      cell.Phrase = fontSelector.Process("ORDER NO."); 
      tmpTable.AddCell(cell); 


      cell = new PdfPCell() { Border = 0, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
      cell.Phrase = fontSelector.Process("COMPLEX NAME"); 
      tmpTable.AddCell(cell); 

      cell = new PdfPCell(tmpTable) { BorderWidth = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
      tableHeader.AddCell(cell); 
      /////////////////////////////////////////////////////////////// 

      tmpTable = new PdfGrid(numColumns: 1); 
      cell = new PdfPCell() { Border = 0, BorderWidthBottom = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
      cell.Phrase = fontSelector.Process("QTY."); 
      tmpTable.AddCell(cell); 


      cell = new PdfPCell() { Border = 0, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
      cell.Phrase = fontSelector.Process("G.W Kgs."); 
      tmpTable.AddCell(cell); 

      cell = new PdfPCell(tmpTable) { BorderWidth = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
      tableHeader.AddCell(cell); 
      /////////////////////////////////////////////////////////////// 
      tmpTable = new PdfGrid(numColumns: 1); 
      cell = new PdfPCell() { Border = 0, BorderWidthBottom = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
      cell.Phrase = fontSelector.Process("DISCHARGE PPORT"); 
      tmpTable.AddCell(cell); 


      cell = new PdfPCell() { Border = 0, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
      cell.Phrase = fontSelector.Process("DESTINATON"); 
      tmpTable.AddCell(cell); 

      cell = new PdfPCell(tmpTable) { BorderWidth = 1 }; 
      tableHeader.AddCell(cell); 
      /////////////////////////////////////////////////////////////// 
      cell = new PdfPCell() { BorderWidth = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
      cell.Phrase = fontSelector.Process("DESCRIPTION OF GOODS"); 

      tableHeader.AddCell(cell); 

      ///////////////////////////////////////////////////////////// 
      tmpTable = new PdfGrid(numColumns: 1); 
      cell = new PdfPCell() { Border = 0, BorderWidthBottom = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
      cell.Phrase = fontSelector.Process("LINE DOC. RECI. DATE"); 
      tmpTable.AddCell(cell); 


      cell = new PdfPCell(new Phrase("")) { Border = 0, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
      cell.Phrase = fontSelector.Process("OWNER DOC. RECI. DATE"); 
      tmpTable.AddCell(cell); 

      cell = new PdfPCell(tmpTable) { BorderWidth = 1 }; 
      tableHeader.AddCell(cell); 
      /////////////////////////////////////////////////////////////// 
      cell = new PdfPCell() { BorderWidth = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
      cell.Phrase = fontSelector.Process("CLEARANCE DATE"); 

      tableHeader.AddCell(cell); 

      /////////////////////////////////////////////////////////// 

      cell = new PdfPCell() { BorderWidth = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
      cell.Phrase = fontSelector.Process("CUSTOM PERMIT NO."); 

      tableHeader.AddCell(cell); 

      /////////////////////////////////////////////////////////// 
      cell = new PdfPCell() { BorderWidth = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
      cell.Phrase = fontSelector.Process("DISPATCH DATE"); 
      tableHeader.AddCell(cell); 

      /////////////////////////////////////////////////////////// 
      cell = new PdfPCell(tableHeader) { BorderWidth = 1, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 

      table.AddCell(cell); 
      doc.Add(table); 
      /////////////////////////////////////////////////////////// 
      doc.Close(); 

लेकिन इस कोड को इस तालिका बनाने के लिए डिजाइन enter image description here

मैं आकार बदलना चाहता हूं Serno कॉलम लेकिन

लेकिन मुझे नहीं पता कि सेल की चौड़ाई का आकार कैसे बदलें, कृपया मेरी मदद करें।

+0

तुम क्यों PdfGrid प्रयोग करते हैं और PdfPTable नहीं करने के लिए तालिका की शीर्ष दोहराने के लिए प्रयोग किया जाता है? यह स्पष्ट नहीं है कि आप वास्तव में क्या करना चाहते हैं। एक टेबल है तो एक टेबल हैडर और उनके पास कोई लिंक नहीं है। (???) –

+0

@ निक: तालिका शीर्षलेख नेस्टेड टेबल है, इस सेल में केवल रोवनो और मैं चाहता हूं कि मैं छोटा हूं और चौड़ाई – Pouya

उत्तर

33

इस के लिए PdfPTable ऑब्जेक्ट का उपयोग क्यों नहीं करें? एक निश्चित चौड़ाई तालिका बनाएं और स्तंभों

PdfPTable table = new PdfPTable(10); 
table.HorizontalAlignment = 0; 
table.TotalWidth = 500f; 
table.LockedWidth = true; 
float[] widths = new float[] { 20f, 60f, 60f, 30f, 50f, 80f, 50f, 50f, 50f, 50f }; 
table.SetWidths(widths); 

addCell(table, "SER.\nNO.", 2); 

addCell(table, "TYPE OF SHIPPING", 1); 
addCell(table, "ORDER NO.", 1); 
addCell(table, "QTY.", 1); 
addCell(table, "DISCHARGE PPORT", 1); 

addCell(table, "DESCRIPTION OF GOODS", 2); 

addCell(table, "LINE DOC. RECL DATE", 1); 

addCell(table, "CLEARANCE DATE", 2); 
addCell(table, "CUSTOM PERMIT NO.", 2); 
addCell(table, "DISPATCH DATE", 2); 

addCell(table, "AWB/BL NO.", 1); 
addCell(table, "COMPLEX NAME", 1); 
addCell(table, "G. W. Kgs.", 1); 
addCell(table, "DESTINATION", 1); 
addCell(table, "OWNER DOC. RECL DATE", 1); 

.... 

private static void addCell(PdfPTable table, string text, int rowspan) 
{ 
    BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false); 
    iTextSharp.text.Font times = new iTextSharp.text.Font(bfTimes, 6, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK); 

    PdfPCell cell = new PdfPCell(new Phrase(text, times)); 
    cell.Rowspan = rowspan; 
    cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER; 
    cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; 
    table.AddCell(cell); 
} 

this tutorial भी पर एक नजर है की चौड़ाई निर्धारित करने के लिए एक नाव सरणी का उपयोग करें ...

+0

मैंने नमूना प्रदान किए गए मेरे प्रश्न को अपडेट किया है। यह तालिका –

+0

पर चौड़ाई निर्धारित करने के बारे में है, मुझे यह पसंद है, यह क्लीनर है। आप आगे बढ़ सकते हैं और एक विधि में टेम्पलेट पंक्ति का एक प्रकार बना सकते हैं। आप विधि को 'AddRow (PdfPTable _t, DataRow _dr, bool _head)' के साथ कॉल करेंगे। वहां से सब आसान है। –

+0

@ निक।: वास्तव में भी बेहतर, मैंने अपना कोड नमूना अपडेट किया है, लेकिन केवल प्रत्येक कक्ष के लिए –

3

इस

PdfPCell cell; 
PdfPTable tableHeader; 
PdfPTable tmpTable; 
PdfPTable table = new PdfPTable(10) { WidthPercentage = 100, RunDirection = PdfWriter.RUN_DIRECTION_LTR, ExtendLastRow = false }; 

// row 1/cell 1 (merge) 
PdfPCell _c = new PdfPCell(new Phrase("SER. No")) { Rotation = -90, VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER, BorderWidth = 1 }; 
_c.Rowspan = 2; 

table.AddCell(_c); 

// row 1/cell 2 
_c = new PdfPCell(new Phrase("TYPE OF SHIPPING")) { VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
table.AddCell(_c); 

// row 1/cell 3 
_c = new PdfPCell(new Phrase("ORDER NO.")) { VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
table.AddCell(_c); 

// row 1/cell 4 
_c = new PdfPCell(new Phrase("QTY.")) { VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
table.AddCell(_c); 

// row 1/cell 5 
_c = new PdfPCell(new Phrase("DISCHARGE PPORT")) { VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
table.AddCell(_c); 

// row 1/cell 6 (merge) 
_c = new PdfPCell(new Phrase("DESCRIPTION OF GOODS")) { VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
_c.Rowspan = 2; 
table.AddCell(_c); 

// row 1/cell 7 
_c = new PdfPCell(new Phrase("LINE DOC. RECI. DATE")) { VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
table.AddCell(_c); 

// row 1/cell 8 (merge) 
_c = new PdfPCell(new Phrase("OWNER DOC. RECI. DATE")) { VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
_c.Rowspan = 2; 
table.AddCell(_c); 

// row 1/cell 9 (merge) 
_c = new PdfPCell(new Phrase("CLEARANCE DATE")) { VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
_c.Rowspan = 2; 
table.AddCell(_c); 

// row 1/cell 10 (merge) 
_c = new PdfPCell(new Phrase("CUSTOM PERMIT NO.")) { VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
_c.Rowspan = 2; 
table.AddCell(_c); 


// row 2/cell 2 
_c = new PdfPCell(new Phrase("AWB/BL NO.")) { VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
table.AddCell(_c); 

// row 2/cell 3 
_c = new PdfPCell(new Phrase("COMPLEX NAME")) { VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
table.AddCell(_c); 

// row 2/cell 4 
_c = new PdfPCell(new Phrase("G.W Kgs.")) { VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
table.AddCell(_c); 

// row 2/cell 5 
_c = new PdfPCell(new Phrase("DESTINATON")) { VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
table.AddCell(_c); 

// row 2/cell 7 
_c = new PdfPCell(new Phrase("OWNER DOC. RECI. DATE")) { VerticalAlignment = Element.ALIGN_MIDDLE, HorizontalAlignment = Element.ALIGN_CENTER }; 
table.AddCell(_c); 

_doc.Add(table); 
/////////////////////////////////////////////////////////// 
_doc.Close(); 

आप की तरह कुछ की कोशिश करो चौड़ाई और सीमाओं पर थोड़ा समायोजित करने की आवश्यकता हो सकती है लेकिन यह एक शॉट है।

1

aca definis लॉस anchos

float[] anchoDeColumnas= new float[] {10f, 20f, 30f, 10f}; 

aca से लॉस insertas एक ला तबला कुए टिएने लास Columnas

table.setWidths(anchoDeColumnas); 
2

इस कोड को मुझे लगता है कि इसे और अधिक इष्टतम है की कोशिश करो।

HeaderRow प्रत्येक नए पेज स्वचालित

 BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false); 
     iTextSharp.text.Font times = new iTextSharp.text.Font(bfTimes, 6, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK); 

     PdfPTable table = new PdfPTable(10) { HorizontalAlignment = Element.ALIGN_CENTER, WidthPercentage = 100, HeaderRows = 2 }; 
     table.SetWidths(new float[] { 2f, 6f, 6f, 3f, 5f, 8f, 5f, 5f, 5f, 5f }); 
     table.AddCell(new PdfPCell(new Phrase("SER.\nNO.", times)) { Rowspan = 2, GrayFill = 0.95f }); 
     table.AddCell(new PdfPCell(new Phrase("TYPE OF SHIPPING", times)) { GrayFill = 0.95f }); 
     table.AddCell(new PdfPCell(new Phrase("ORDER NO.", times)) { GrayFill = 0.95f }); 
     table.AddCell(new PdfPCell(new Phrase("QTY.", times)) { GrayFill = 0.95f }); 
     table.AddCell(new PdfPCell(new Phrase("DISCHARGE PPORT", times)) { GrayFill = 0.95f }); 
     table.AddCell(new PdfPCell(new Phrase("DESCRIPTION OF GOODS", times)) { Rowspan = 2, GrayFill = 0.95f }); 
     table.AddCell(new PdfPCell(new Phrase("LINE DOC. RECL DATE", times)) { GrayFill = 0.95f }); 
     table.AddCell(new PdfPCell(new Phrase("CLEARANCE DATE", times)) { Rowspan = 2, GrayFill = 0.95f }); 
     table.AddCell(new PdfPCell(new Phrase("CUSTOM PERMIT NO.", times)) { Rowspan = 2, GrayFill = 0.95f }); 
     table.AddCell(new PdfPCell(new Phrase("DISPATCH DATE", times)) { Rowspan = 2, GrayFill = 0.95f }); 
     table.AddCell(new PdfPCell(new Phrase("AWB/BL NO.", times)) { GrayFill = 0.95f }); 
     table.AddCell(new PdfPCell(new Phrase("COMPLEX NAME", times)) { GrayFill = 0.95f }); 
     table.AddCell(new PdfPCell(new Phrase("G. W. Kgs.", times)) { GrayFill = 0.95f }); 
     table.AddCell(new PdfPCell(new Phrase("DESTINATION", times)) { GrayFill = 0.95f }); 
     table.AddCell(new PdfPCell(new Phrase("OWNER DOC. RECL DATE", times)) { GrayFill = 0.95f });