บทความสั้นๆ นี้จะแนะนำคุณเกี่ยวกับวิธีการ แปลง PDF เป็น QR Code โดยใช้ C# โดยมีรายละเอียดเกี่ยวกับการตั้งค่า IDE สำหรับการพัฒนาและรายการขั้นตอนสำหรับการแปลง PDF เป็นบาร์โค้ดโดยใช้ C# คุณจะได้เรียนรู้วิธีดึงบาร์โค้ดและ QR Code ทั้งหมดจาก PDF และแสดงข้อความสำหรับแต่ละรายการ
ขั้นตอนการแปลง PDF เป็น QR Code โดยใช้ C#
- ตั้งค่า IDE เพื่อใช้ Aspose.PDF และ Aspose.BarCode สำหรับ .NET เพื่ออ่าน QR Code และบาร์โค้ด
- โหลด PDF ต้นฉบับที่มี QR Code และบาร์โค้ดโดยใช้วัตถุ Document
- วนลูปผ่านทุกหน้าใน PDF และประมวลผลคอลเลกชันรูปภาพในแต่ละหน้า
- บันทึกรูปภาพแต่ละรูปเป็น JPG ในสตรีมหน่วยความจำ
- สร้างอินสแตนซ์ของวัตถุ BarCodeReader ส่งสตรีมหน่วยความจำของรูปภาพและประเภทการถอดรหัสเป้าหมาย
- วิเคราะห์คอลเลกชันของบาร์โค้ดและ QR Code ทั้งหมดในรูปภาพ
- แสดงข้อความและประเภทของโค้ดที่ตรวจพบ
ขั้นตอนเหล่านี้จะอธิบายวิธีการแปลง PDF เป็น QR Code โดยใช้ C# ตั้งค่าสภาพแวดล้อมเพื่อใช้ทั้ง Aspose.PDF และ Aspose.BarCode โหลดไฟล์ PDF ต้นฉบับที่มี QR Code และบาร์โค้ด วนลูปผ่านทุกหน้าหรือหน้าที่เลือก และดึงคอลเลกชันรูปภาพในแต่ละหน้า บันทึกรูปภาพแต่ละรูปในสตรีมหน่วยความจำ ใช้ใน BarCodeReader เพื่ออ่าน QR Code และบาร์โค้ด และสุดท้ายแสดงข้อความและประเภทของโค้ด
โค้ดสำหรับตัวแปลง PDF เป็น QR Code โดยใช้ C#
// Necessary using directives | |
using Aspose.Pdf; | |
using System.IO; | |
using Aspose.BarCode.BarCodeRecognition; | |
// Custom namespace for the application | |
namespace DocumentProcessor | |
{ | |
// Core class of the application | |
class BarcodeExtractor | |
{ | |
// Application's entry method | |
static void Main(string[] args) | |
{ | |
// Set up licenses for Aspose.PDF and Aspose.BarCode | |
var pdfLicense = new Aspose.Pdf.License(); | |
pdfLicense.SetLicense("License.lic"); | |
var barcodeLicense = new Aspose.BarCode.License(); | |
barcodeLicense.SetLicense("License.lic"); | |
// Load the PDF file | |
using (var pdfDocument = new Document("bar_qr_code.pdf")) | |
{ | |
// Iterate through each page in the PDF | |
for (int pageIndex = 1; pageIndex <= pdfDocument.Pages.Count; pageIndex++) | |
{ | |
var page = pdfDocument.Pages[pageIndex]; | |
// Check if the page contains images | |
if (page.Resources.Images.Count > 0) | |
{ | |
// Process each image in the page | |
foreach (var image in page.Resources.Images) | |
{ | |
using (var imgStream = new MemoryStream()) | |
{ | |
// Save the image to a memory stream in JPEG format | |
image.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg); | |
imgStream.Position = 0; // Reset stream position | |
// Initialize the barcode reader for the image | |
var reader = new BarCodeReader(imgStream, DecodeType.AllSupportedTypes); | |
// Retrieve and display barcode results | |
foreach (var result in reader.ReadBarCodes()) | |
{ | |
var barcodeText = result.CodeText; | |
var barcodeType = result.CodeTypeName; | |
System.Console.WriteLine($"Detected {barcodeType} with content: {barcodeText}"); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
โค้ดด้านบนแสดงกระบวนการ สร้าง QR Code จาก PDF โดยใช้ C# เราวิเคราะห์ทุกหน้าใน PDF และใช้คอลเลกชันรูปภาพในแต่ละหน้าเพื่อตรวจจับ QR และบาร์โค้ด รูปภาพเดียวอาจมี QR/บาร์โค้ดหนึ่งหรือหลายรายการที่อ่านด้วยเมธอด ReadBarCodes() ประมวลผลทีละรายการ และแสดงข้อความและโค้ดของพวกเขา
บทความนี้ได้แนะนำเราในการเปลี่ยน PDF เป็น QR Code หากคุณต้องการสร้าง QR หรือบาร์โค้ดใหม่ โปรดดูบทความเกี่ยวกับ วิธีการสร้าง QR Code ใน C#