나는 서적 라이브러리가 있으며 그 라이브러리 레이블을 인쇄하려고합니다. 마지막으로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++;
}
}
이 코드는 다음과 같이 표시됩니다.
는하지만이 같은 레이블을 얻으려면 : 내가 옆에 라벨 측을 받고 관리하는 경우
이 때문에, 나는 쉽게 컴퓨터 라벨에 인쇄 할 수 있습니다.가 어떻게 옆 테이블 측을 반복 할 수있다 여기에 내가 그것을 인쇄 할 컴퓨터 라벨은 무엇입니까?
'foreach (book_ids의 var book_id)'루프에서 내부 테이블을 생성하는 동안 어떻게 외부 테이블을 가져올 수 있습니까? –
음 ... 위의 텍스트를 편집하고 코드를 수정하여 게시했습니다. 메모장에서이 코드를 작성 했으므로 상자에서 제대로 작동하지 않을 수 있습니다. 그러나이 구조로 원하는 출력을 얻는 방법에 대한 아이디어를 얻으십시오. – COeDev
감사합니다. 이것은 아주 좋습니다. –