PDF(Portable Document Format) ได้กลายเป็นหนึ่งในรูปแบบที่ใช้กันอย่างแพร่หลายในโลกของเอกสารดิจิทัล อุตสาหกรรมต่างๆ ใช้รูปแบบ PDF เพื่อสร้างรายงาน ใบแจ้งหนี้ ใบแจ้งหนี้ และเอกสารทางธุรกิจประเภทอื่นๆ ในทางกลับกัน บาร์โค้ดได้กลายเป็นส่วนสำคัญของธุรกิจที่มีข้อมูลในรูปแบบที่เครื่องอ่านได้ ทุกวันนี้ คุณอาจพบบาร์โค้ดบนใบเรียกเก็บเงินและใบแจ้งหนี้ของคุณด้วย ในบทความนี้ ฉันจะนำเสนอกรณีการใช้งานที่คุณต้องสร้างไฟล์ PDF และฝังบาร์โค้ดไว้บนไฟล์ เช่น เมื่อสร้างใบแจ้งหนี้ สำหรับการสาธิต ฉันจะสร้าง ASP.NET PDF Editor ซึ่งคุณสามารถสร้างไฟล์ PDF และบาร์โค้ดโดยใช้ C# ภายในแอปพลิเคชันเว็บ ASP.NET Core
เว็บแอปพลิเคชัน ASP.NET นี้จะมีคุณสมบัติดังต่อไปนี้:
- โปรแกรมแก้ไขแบบ WYSIWYG เพื่อเขียนเนื้อหาของเอกสาร PDF
- ตัวเลือกในการสร้างบาร์โค้ดตามข้อความที่ให้ไว้
- ตัวเลือกในการตั้งค่าสัญลักษณ์บาร์โค้ดที่ต้องการ
ข้อกำหนดเบื้องต้นสำหรับการสร้าง PDF ที่มีบาร์โค้ดใน ASP.NET
ต่อไปนี้คือเครื่องมือและ API ที่คุณต้องการเพื่อสร้าง ASP.NET PDF Editor ที่มีคุณสมบัติบาร์โค้ด
- Visual Studio 2017 หรือใหม่กว่า
- Aspose.PDF for .NET – A .NET PDF API.
- Aspose.BarCode for .NET – .NET Barcode Generator and Scanner API.
สร้าง ASP.NET PDF Editor พร้อมคุณสมบัติบาร์โค้ด
มาเริ่มต้นการเดินทางของเราและดูวิธีสร้าง ASP.NET PDF Editor ของเราที่จะช่วยให้คุณสร้าง PDF และฝังบาร์โค้ดได้ในคลิกเดียว
- สร้างแอปพลิเคชันเว็บ ASP.NET Core ใหม่ใน Visual Studio

- เลือกแอปพลิเคชันเว็บ (Model-View-Controller) จากเทมเพลต

- ติดตั้งแพ็คเกจของ Aspose.PDF, Aspose.BarCode และ CKEditor

ดาวน์โหลด แพ็คเกจ ของ CKEditor แตกไฟล์แล้วคัดลอก/วางโฟลเดอร์ในไดเร็กทอรี wwwroot คุณยังสามารถรวมโปรแกรมแก้ไข HTML แบบ WYSIWYG ที่คุณชื่นชอบได้ตามความต้องการของคุณ
เพิ่มสคริปต์ต่อไปนี้ในมุมมอง Views/Home/index.cshtml
@{
ViewData["Title"] = "PDF Creator";
}
<script src="~/ckeditor/ckeditor.js"></script>
<br />
<form method="post">
<div class="row">
<div class="col-md-12">
<textarea name="editor1" id="editor1" rows="80" cols="80">
Start creating your PDF document.
</textarea>
<br />
<script>
// Replace the <textarea id="editor1"> with a CKEditor
// instance, using default configuration.
CKEDITOR.replace('editor1');
</script>
</div>
<hr />
</div>
<div class="row">
<div class="col-md-12">
<h3>Create a Barcode</h3>
</div>
</div>
<hr />
<div class="row">
<div class="col-md-9 form-horizontal" align="center">
<div class="form-group">
<label class="control-label col-sm-2" for="CodeText">Code Text</label>
<div class="col-sm-10">
<input class="form-control" type="text" name="codeText" id="codeText" placeholder="Code text" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="barcodeType">Symbology</label>
<div class="col-sm-10">
<select name="barcodeType" class="form-control">
<option value="Code128">Code128</option>
<option value="Code11">Code11</option>
<option value="QR">QR</option>
<option value="Pdf417">Pdf417</option>
<option value="Datamatrix">Datamatrix</option>
</select>
</div>
</div>
</div>
</div>
<hr />
<div class="row">
<div class="col-md-12" align="center">
<input type="submit" class="btn btn-lg btn-success" value="Generate PDF" />
</div>
</div>
</form>
- เพิ่มวิธีการต่อไปนี้ใน Controllers/HomeController.cs
[HttpPost]
public FileResult Index(string editor1, string codeText, string barcodeType)
{
// generate a barcode
string barcodeImagePath = Path.Combine("wwwroot/barcodes/", Guid.NewGuid() + ".png");
SymbologyEncodeType type = GetBarcodeSymbology(barcodeType);
BarcodeGenerator generator = new BarcodeGenerator(type, codeText);
generator.Parameters.BackColor = System.Drawing.Color.Transparent;
// set resolution of the barcode image
generator.Parameters.Resolution = 200;
// generate barcode
generator.Save(barcodeImagePath, BarCodeImageFormat.Png);
// create a unique file name for PDF
string fileName = Guid.NewGuid() + ".pdf";
// convert HTML text to stream
byte[] byteArray = Encoding.UTF8.GetBytes(editor1);
// generate PDF from the HTML
MemoryStream stream = new MemoryStream(byteArray);
HtmlLoadOptions options = new HtmlLoadOptions();
Document pdfDocument = new Document(stream, options);
// add barcode image to the generated PDF
pdfDocument = InsertImage(pdfDocument, barcodeImagePath);
// create memory stream for the PDF file
Stream outputStream = new MemoryStream();
// save PDF to output stream
pdfDocument.Save(outputStream);
// return generated PDF file
return File(outputStream, System.Net.Mime.MediaTypeNames.Application.Pdf, fileName);
}
private SymbologyEncodeType GetBarcodeSymbology(string symbology)
{
if (symbology.ToLower() == "qr")
return EncodeTypes.QR;
else if (symbology.ToLower() == "code128")
return EncodeTypes.Code128;
else if (symbology.ToLower() == "code11")
return EncodeTypes.Code11;
else if (symbology.ToLower() == "pdf417")
return EncodeTypes.Pdf417;
else if (symbology.ToLower() == "datamatrix")
return EncodeTypes.DataMatrix;
else
return EncodeTypes.Code128; // default barcode type
}
private Document InsertImage(Document document, string barcodeImagePath)
{
// get page from Pages collection of PDF file
Aspose.Pdf.Page page = document.Pages[1];
// create an image instance
Aspose.Pdf.Image img = new Aspose.Pdf.Image();
img.IsInLineParagraph = true;
// set Image Width and Height in Points
img.FixWidth = 100;
img.FixHeight = 100;
img.HorizontalAlignment = HorizontalAlignment.Right;
img.VerticalAlignment = VerticalAlignment.Top;
// set image type as SVG
img.FileType = Aspose.Pdf.ImageFileType.Unknown;
// path for source barcode image file
img.File = barcodeImagePath;
page.Paragraphs.Add(img);
// return updated PDF document
return document;
}
- สร้างแอปพลิเคชันและรันในเบราว์เซอร์ที่คุณชื่นชอบ

การสร้าง PDF ด้วย ASP.NET PDF Editor
ต่อไปนี้เป็นขั้นตอนรวมถึงการสาธิตวิธีสร้างไฟล์ PDF และการฝังบาร์โค้ดด้วยการคลิกเพียงครั้งเดียว
- เขียนหรือคัดลอก/วางเนื้อหาของเอกสาร PDF ในพื้นที่ตัวแก้ไข
- ตั้งค่าข้อความโค้ดเพื่อสร้างบาร์โค้ด
- เลือกสัญลักษณ์บาร์โค้ดที่คุณต้องการ (ดู สัญลักษณ์บาร์โค้ดที่รองรับทั้งหมด)
- คลิกปุ่มสร้าง PDF เพื่อสร้าง PDF ที่มีบาร์โค้ดอยู่
ดาวน์โหลดซอร์สโค้ด
คุณสามารถดาวน์โหลดซอร์สโค้ดทั้งหมดของ ASP.NET PDF Editor ได้จาก ที่นี่
ลองใช้ Aspose API ฟรี
รับ [ใบอนุญาตชั่วคราว] ของคุณ7 และทดลองใช้ API ของเราฟรีหนึ่งเดือน