2017-03-18 4 views
1

나는 서적 라이브러리가 있으며 그 라이브러리 레이블을 인쇄하려고합니다. 마지막으로iTextSharp - C# winforms에서 테이블을 나란히 반복 할 수 있습니까?

public void CreateLabels(List<int> books_ids, PanelControl p) 
{ 
    var doc = new Document(PageSize.A4, 10, 10, 10, 10); 
    var m = new SaveFileDialog 
    { 
     Filter = @"PDF File Format|*.pdf", 
     FileName = "Labels.pdf" 
    }; 

    if (m.ShowDialog() != DialogResult.OK) return; 

    var file = new FileStream(m.FileName, FileMode.Create); 
    wr = PdfWriter.GetInstance(doc, file); 

    doc.Open(); 
    cb = wr.DirectContent; 
    wr.PageEvent = this; 
    // Labels are created from this method. 
    Labels(doc, books_ids); 
    doc.Close(); 
} 

, 내용 : CreateLabels 방법의 내용을

private void btnQRPrintLabels_Click(object sender, EventArgs e) 
{ 
    // These are books' ids. Normally, these are retrieved from GridControl element. 
    var books_ids = new List<int>() {1, 2, 5, 6, 7, 12}; 
    // I don't use PanelControl. But in the future, maybe I can use it later. 
    CreateLabels(books_ids, panelControl1); 
} 

을 그리고 여기에 있습니다 : 당신이 GridControl에서 책을 선택하고 Print Labels 버튼을 클릭하면, 프로그램은이 같은 pdf 파일에 라벨을 생성 Labels 방법 :

protected void Labels(Document doc, List<int> books_ids) 
{ 
    var i = 1; 
    foreach (var book_id in books_ids) 
    { 
     var alignment = (i % 2 != 0) ? Element.ALIGN_LEFT : Element.ALIGN_RIGHT; 

     // Book's informations are retrieved from Linq Connect Model. 
     var _connection = new LinqtoSQLiteDataContext(); 
     var book = _connection.books.SingleOrDefault(b_no => b_no.id == book_id); 
     // Outer table to get rounded corner... 
     var table = new PdfPTable(1) { WidthPercentage = 48, HorizontalAlignment = alignment }; 
     // Inner table: First cell for QR code and second cell for book's informations. 
     var inner_table = new PdfPTable(2) { WidthPercentage = 100 }; 
     inner_table.DefaultCell.Border = Rectangle.NO_BORDER; 

     var inner_measurements = new[] { 40f, 60f }; 
     inner_table.SetWidths(inner_measurements); 
     // Generate QR code in the `Tools` class, `GenerateQR` method. 
     System.Drawing.Image image = Tools.GenerateQR(100, 100, book?.isbn); 
     var pdfImage = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg); 
     var qr = iTextSharp.text.Image.GetInstance(pdfImage); 

     var a = new PdfPCell(qr) { Border = Rectangle.NO_BORDER }; 
     var b = new PdfPCell(new Phrase(book?.name, font_normal)) { Border = Rectangle.NO_BORDER }; 

     inner_table.AddCell(a); 
     inner_table.AddCell(b); 

     var s = new PdfPCell(inner_table) 
     { 
      CellEvent = new RoundRectangle(), 
      Border = Rectangle.NO_BORDER, 
      Padding = 2, 
      HorizontalAlignment = Element.ALIGN_LEFT 
     }; 
     table.AddCell(s); 
     doc.Add(table); 
     i++; 
    } 
} 

이 코드는 다음과 같이 표시됩니다.

enter image description here

는하지만이 같은 레이블을 얻으려면 : 내가 옆에 라벨 측을 받고 관리하는 경우

enter image description here

이 때문에, 나는 쉽게 컴퓨터 라벨에 인쇄 할 수 있습니다.

enter image description here

가 어떻게 옆 테이블 측을 반복 할 수있다 여기에 내가 그것을 인쇄 할 컴퓨터 라벨은 무엇입니까?

답변

1

하나를 사용하여 외부 표는이 일에 모든 내부 테이블을 추가하고 마지막에 문서에 외부 테이블을 추가합니다. 이렇게하면 왼쪽/오른쪽 정렬을 사용하지 않아도됩니다.

: 편집 : 나는이 작업을 수행하는 방법을

var table = new PdfPTable(2) { WidthPercentage = 100 }; 

foreach (var book_id in books_ids) 
{ 
    // Book's informations are retrieved from Linq Connect Model. 
    var _connection = new LinqtoSQLiteDataContext(); 
    var book = _connection.books.SingleOrDefault(b_no => b_no.id == book_id); 
    // Outer table to get rounded corner... 

    // Inner table: First cell for QR code and second cell for book's informations. 
    var inner_table = new PdfPTable(2) { WidthPercentage = 100 }; 
    inner_table.DefaultCell.Border = Rectangle.NO_BORDER; 

    var inner_measurements = new[] { 40f, 60f }; 
    inner_table.SetWidths(inner_measurements); 
    // Generate QR code in the `Tools` class, `GenerateQR` method. 
    System.Drawing.Image image = Tools.GenerateQR(100, 100, book?.isbn); 
    var pdfImage = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg); 
    var qr = iTextSharp.text.Image.GetInstance(pdfImage); 

    var a = new PdfPCell(qr) { Border = Rectangle.NO_BORDER }; 
    var b = new PdfPCell(new Phrase(book?.name, font_normal)) { Border = Rectangle.NO_BORDER }; 

    inner_table.AddCell(a); 
    inner_table.AddCell(b); 

    PdfPCell labelCell = new PdfPCell(inner_table) 
    { 
     CellEvent = new RoundRectangle(), 
     Border = Rectangle.NO_BORDER, 
     Padding = 2, 
     HorizontalAlignment = Element.ALIGN_LEFT 
    }; 

    table.AddCell(labelCell);    
} 

doc.Add(table); 
+0

'foreach (book_ids의 var book_id)'루프에서 내부 테이블을 생성하는 동안 어떻게 외부 테이블을 가져올 수 있습니까? –

+0

음 ... 위의 텍스트를 편집하고 코드를 수정하여 게시했습니다. 메모장에서이 코드를 작성 했으므로 상자에서 제대로 작동하지 않을 수 있습니다. 그러나이 구조로 원하는 출력을 얻는 방법에 대한 아이디어를 얻으십시오. – COeDev

+0

감사합니다. 이것은 아주 좋습니다. –

-1

페이지 수준 테이블 (2 열 6 행)을 추가하고 각 레이블을이 페이지 수준 테이블의 개별 셀에 배치하기 만하면됩니다.

new PdfPTable(2) { WidthPercentage = 100 } 

+0

미안 해요, 난 이해가 안 돼요? 나는 다음과 같이 인터넷 검색을했다. https://www.google.com.tr/?gws_rd=ssl#q=itextsharp+page-level+table&* 그러나 나는 이것에 관해 아무것도 발견하지 못했다. –

+0

라벨 레이아웃에 대해서는, 각 라벨 및 다음의 테이블에 테이블을 사용하고 있기 때문에, 전체 페이지에있는 테이블을 의미합니다. – Kordik

+0

네, 생각하기에 둥근 코더에 테이블을 사용해야합니다. –