iText : v5.5.13.2
protected void submitPdf_Click(object sender, EventArgs e) {
BarCodeGenerate();
}
///
/// Generates the new page with barcode128.
///
public void BarCodeGenerate() {
Document doc = new Document(PageSize.A4.Rotate(), 50, 50, 20, 20);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(@"c:\temp\BarCodeQRCode.pdf", FileMode.Create));
doc.Open();
PdfContentByte cb = writer.DirectContent;
PdfContentByte cb39 = writer.DirectContent;
Paragraph pa = new Paragraph();
Barcode128 barcode = new Barcode128();
barcode.CodeType = Barcode.CODE128_UCC;
barcode.Code = "hello barcode - code128";
iTextSharp.text.Image barcodeImage = barcode.CreateImageWithBarcode(cb, null, null);
barcodeImage.ScalePercent(100f);
barcodeImage.Alignment = Element.ALIGN_CENTER;
pa.Add(barcodeImage);
BaseFont fontChinese = BaseFont.CreateFont("C:/Windows/Fonts/STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font normalFont = new Font(fontChinese, 12);
//float[] w = { 5f, 15f, 6f, 10f };
float[] w = {
Convert.ToSingle(PageSize.A4.Width * 0.4),
Convert.ToSingle(PageSize.A4.Width * 0.3),
Convert.ToSingle(PageSize.A4.Width * 0.3)
};
PdfPTable table = new PdfPTable(w);
table.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
table.DefaultCell.BorderWidth = 0;
table.DefaultCell.BorderColor = new BaseColor(0, 0, 0);
table.DefaultCell.Padding = 4;
table.DefaultCell.SpaceCharRatio = 4;
table.WidthPercentage = 100;
table.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
PdfPCell cell = new PdfPCell(new Phrase("Create a table with all default settings"));
cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER;
cell.HorizontalAlignment = Rectangle.ALIGN_CENTER;
table.AddCell(cell);
var aa = new Paragraph("aaa", normalFont);
cell = new PdfPCell(new Phrase("Create a table 中文字的測試with all default settings", normalFont));
cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER;
cell.HorizontalAlignment = Rectangle.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Create a table with all default settings"));
cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER;
cell.HorizontalAlignment = Rectangle.ALIGN_CENTER;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Create a table with all default settings"));
cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER;
cell.HorizontalAlignment = Rectangle.ALIGN_CENTER;
table.AddCell(cell);
doc.Add(table);
float[] w2 = {
Convert.ToSingle(PageSize.A4.Width * 0.4),
Convert.ToSingle(PageSize.A4.Width * 0.2),
Convert.ToSingle(PageSize.A4.Width * 0.1),
Convert.ToSingle(PageSize.A4.Width * 0.3)
};
//float[] w2 = { 25f, 15f, 30f, 10f };
table = new PdfPTable(w2);
table.WidthPercentage = 100;
cell = new PdfPCell(new Phrase("Create a table with all default settings"));
cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER;
cell.HorizontalAlignment = Rectangle.ALIGN_CENTER;
//cell.FixedHeight = 10f;
table.AddCell(cell);
cell = new PdfPCell(barcodeImage, true);
cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER;
cell.HorizontalAlignment = Rectangle.ALIGN_CENTER;
cell.Padding = 5f;
//cell.FixedHeight = 10f;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Create a table with all default settings"));
cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER;
cell.HorizontalAlignment = Rectangle.ALIGN_CENTER;
//cell.FixedHeight = 10f;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Create a table with all default settings"));
cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER;
cell.HorizontalAlignment = Rectangle.ALIGN_CENTER;
//cell.FixedHeight = 10f;
table.AddCell(cell);
doc.Add(table);
Barcode39 code39 = new Barcode39();
code39.Code = "314159265";
//code39.CodeType = Barcode.CODE128_UCC;
code39.StartStopText = true;
code39.GenerateChecksum = true;
code39.Extended = true;
iTextSharp.text.Image code39Image = code39.CreateImageWithBarcode(cb39, null, null);
//iTextSharp.text.Image code39Image = code39.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White);
pa.Add(code39Image);
cell = new PdfPCell(new Phrase("Create a table with all default settings"));
cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER;
cell.HorizontalAlignment = Rectangle.ALIGN_CENTER;
//cell.FixedHeight = 10f;
table.AddCell(cell);
cell = new PdfPCell(code39Image, true);
cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER;
cell.HorizontalAlignment = Rectangle.ALIGN_CENTER;
cell.Padding = 5f;
//cell.FixedHeight = 10f;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Create a table with all default settings"));
cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER;
cell.HorizontalAlignment = Rectangle.ALIGN_CENTER;
//cell.FixedHeight = 10f;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Create a table with all default settings"));
cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER;
cell.HorizontalAlignment = Rectangle.ALIGN_CENTER;
//cell.FixedHeight = 10f;
table.AddCell(cell);
table.AddCell(cell);
doc.Add(table);
table.AddCell(cell);
doc.Add(table);
table.AddCell(cell);
doc.Add(table);
code39.StartStopText = true;
code39.GenerateChecksum = false;
code39.Extended = true;
code39Image = code39.CreateImageWithBarcode(cb39, null, null);
pa.Add(code39Image);
table.AddCell(cell);
doc.Add(table);
table.AddCell(cell);
doc.Add(table);
table.AddCell(cell);
doc.Add(table);
BarcodeQRCode qrcode = new BarcodeQRCode("hello barcode - QrCode", 100, 100, null);
iTextSharp.text.Image grImage = qrcode.GetImage();
pa.Add(grImage);
/*
https://dotblogs.com.tw/kevinya/2016/01/13/104010
由於 iTextSharp 內建的 QRCode 類別對於中文內容的支援度較差,
需經過幾道 override 的程序改寫才可存中文,所以在此直接採用
第三方元件 MessagingToolkit.QRCode。
利用此第三方元件的QRCodeEncoder物件即可產生QRCode的點陣圖Bitmap,
再將bitmap轉換成iTextSharp.text.Image即可加入到pdf之中:
QRCodeEncoder chtEncoder = new QRCodeEncoder();
//第三方先產生bitmap
Bitmap qrBitmap1 = chtEncoder.Encode("qrcode裝中文內容"); //將內容轉碼成 QR code
//再將bitmap轉換成iTextSharp.text.Image
iTextSharp.text.Image qrImage1 = iTextSharp.text.Image.GetInstance(qrBitmap1, BaseColor.BLACK);
qrImage1.ScalePercent(50);
qrImage1.Alignment = Element.ALIGN_RIGHT;//透過Image.Alignment來設定靠左靠右
pa.Add(qrImage1);
doc.Add(pa);
doc.Close();
*/
doc.Add(pa);
doc.Close();
}