PDF(Định dạng tài liệu di động) đã trở thành một trong những định dạng được sử dụng rộng rãi trong thế giới tài liệu kỹ thuật số. Nhiều ngành khác nhau đang sử dụng định dạng PDF để tạo báo cáo, hóa đơn, hóa đơn và các loại tài liệu kinh doanh khác. Mặt khác, mã vạch đã trở thành một phần thiết yếu của hoạt động kinh doanh chứa thông tin ở dạng máy có thể đọc được. Ngày nay, bạn thậm chí có thể tìm thấy mã vạch trên hóa đơn và hóa đơn của mình. Trong bài viết này, tôi sẽ trình bày một trường hợp sử dụng trong đó bạn cần tạo tệp PDF và nhúng mã vạch vào đó, chẳng hạn như khi tạo hóa đơn. Để minh họa, tôi sẽ tạo một ASP.NET PDF Editor bằng cách sử dụng nó để bạn có thể tạo các tệp PDF và mã vạch bằng C# trong ứng dụng web ASP.NET Core.

Ứng dụng web ASP.NET này sẽ có các tính năng sau:

  • Trình soạn thảo WYSIWYG để viết nội dung của tài liệu PDF.
  • Tùy chọn tạo mã vạch dựa trên văn bản được cung cấp.
  • Tùy chọn để đặt ký hiệu mong muốn của mã vạch.

Điều kiện tiên quyết để tạo tệp PDF có mã vạch trong ASP.NET

Sau đây là các công cụ và API mà bạn cần để tạo ASP.NET PDF Editor có tính năng mã vạch.

Tạo ASP.NET PDF Editor với các tính năng mã vạch

Hãy bắt đầu hành trình của chúng tôi và xem cách tạo ASP.NET PDF Editor cho phép tạo PDF và nhúng mã vạch chỉ bằng một cú nhấp chuột.

  • Tạo một ứng dụng web ASP.NET Core mới trong Visual Studio.
Tạo ứng dụng web ASP.NET Core
  • Chọn Ứng dụng web (Model-View-Controller) từ các mẫu.
ASP.NET MVC
  • Cài đặt các gói Aspose.PDF, Aspose.BarCode và CKEditor.
Thêm API Aspose .NET PDF và mã vạch
  • Tải xuống gói của CKEditor, giải nén nó và sao chép/dán thư mục trong thư mục wwwroot. Bạn cũng có thể tích hợp bất kỳ trình soạn thảo HTML WYSIWYG yêu thích nào dựa trên yêu cầu của bạn.

  • Thêm tập lệnh sau vào chế độ xem 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>
  • Thêm các phương thức sau vào 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;
}
  • Xây dựng ứng dụng và chạy nó trong trình duyệt yêu thích của bạn.
Trình soạn thảo ASP.NET PDF với các tính năng mã vạch

Tạo tệp PDF bằng ASP.NET PDF Editor

Sau đây là các bước cũng như phần trình bày cách tạo tệp PDF và nhúng mã vạch chỉ bằng một cú nhấp chuột.

  • Viết hoặc sao chép/dán nội dung của tài liệu PDF vào khu vực soạn thảo.
  • Đặt văn bản mã để tạo mã vạch.
  • Chọn ký hiệu mã vạch mong muốn của bạn (xem tất cả ký hiệu mã vạch được hỗ trợ).
  • Nhấp vào nút Tạo PDF để tạo tệp PDF có mã vạch trên đó.

Tải xuống mã nguồn

Bạn có thể tải xuống mã nguồn hoàn chỉnh của ASP.NET PDF Editor này từ đây.

Dùng thử miễn phí API Aspose

Nhận giấy phép tạm thời của bạn và dùng thử API của chúng tôi miễn phí trong một tháng.

Xem thêm