PDF(Portable Document Format)는 디지털 문서 세계에서 널리 사용되는 형식 중 하나가 되었습니다. 다양한 산업에서 PDF 형식을 사용하여 보고서, 송장, 청구서 및 기타 유형의 비즈니스 문서를 생성하고 있습니다. 반면에 바코드는 기계가 읽을 수 있는 형식으로 정보를 포함하는 비즈니스의 필수 부분이 되었습니다. 요즘에는 청구서와 청구서에서 바코드를 찾을 수도 있습니다. 이 기사에서는 예를 들어 인보이스를 생성할 때 PDF 파일을 만들고 여기에 바코드를 포함해야 하는 사용 사례를 제시합니다. 데모를 위해 ASP.NET Core 웹 응용 프로그램 내에서 C#을 사용하여 PDF 파일과 바코드를 생성할 수 있는 ASP.NET PDF 편집기를 만들겠습니다.
이 ASP.NET 웹 응용 프로그램에는 다음과 같은 기능이 있습니다.
- PDF 문서의 내용을 작성하는 WYSIWYG 편집기.
- 제공된 텍스트를 기반으로 바코드를 생성하는 옵션입니다.
- 바코드의 원하는 기호를 설정하는 옵션입니다.
ASP.NET에서 바코드가 있는 PDF를 생성하기 위한 전제 조건
다음은 바코드 기능이 있는 ASP.NET PDF 편집기를 만드는 데 필요한 도구 및 API입니다.
- Visual Studio 2017 이상.
- .NET용 Aspose.PDF – A .NET PDF API.
- .NET용 Aspose.BarCode – .NET Barcode Generator and Scanner API.
바코드 기능이 있는 ASP.NET PDF 편집기 만들기
여정을 시작하고 한 번의 클릭으로 PDF를 생성하고 바코드를 포함할 수 있는 ASP.NET PDF 편집기를 만드는 방법을 살펴보겠습니다.
- Visual Studio에서 새 ASP.NET Core 웹 애플리케이션을 만듭니다.
- 템플릿에서 웹 애플리케이션(Model-View-Controller)을 선택합니다.
- Aspose.PDF, Aspose.BarCode 및 CKEditor 패키지를 설치합니다.
CKEditor의 패키지를 다운로드하여 압축을 풀고 wwwroot 디렉터리에 폴더를 복사/붙여넣기 합니다. 요구 사항에 따라 선호하는 WYSIWYG HTML 편집기를 통합할 수도 있습니다.
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;
}
- 애플리케이션을 빌드하고 즐겨 사용하는 브라우저에서 실행하십시오.
ASP.NET PDF 편집기로 PDF 생성
다음은 한 번의 클릭으로 PDF 파일을 생성하고 바코드를 삽입하는 방법의 단계와 데모입니다.
- 편집기 영역에서 PDF 문서의 내용을 쓰거나 복사/붙여넣기합니다.
- 바코드를 생성할 코드 텍스트를 설정합니다.
- 원하는 바코드 기호를 선택합니다(모든 지원되는 바코드 기호 참조).
- PDF 생성 버튼을 클릭하여 바코드가 있는 PDF를 생성합니다.
소스 코드 다운로드
이 ASP.NET PDF 편집기의 전체 소스 코드는 여기에서 다운로드할 수 있습니다.
Aspose API를 무료로 사용해 보세요
임시 라이선스를 얻고 한 달 동안 무료로 API를 사용해 보십시오.