>  기사  >  백엔드 개발  >  C#을 사용하여 PDF 문서에 주석을 추가하기 위한 샘플 코드 공유(그림)

C#을 사용하여 PDF 문서에 주석을 추가하기 위한 샘플 코드 공유(그림)

黄舟
黄舟원래의
2017-03-25 11:35:521520검색

이 기사에서는 C#의 무료 구성 요소를 사용하여 무료 텍스트 주석을 포함하여 PDF 문서에 텍스트 주석을 추가하는 방법에 대한 예를 제공합니다. 무료 텍스트 주석을 사용하면 스타일과 모양을 사용자 정의할 수 있어 매우 실용적입니다.

문서를 정리할 때 텍스트의 일부 또는 단락을 설명하기 위해 주석을 추가해야 할 수도 있습니다. 이를 프로그래밍 방식으로 수행하는 방법은 무엇입니까? 옷감? 이 문서에서는 무료 구성 요소를 사용하여 무료 텍스트 주석을 포함하여 C#의 PDF 문서에 텍스트 주석을 추가하는 방법에 대한 예를 제공합니다. 자유 텍스트 댓글을 사용하면 스타일과 모양을 사용자 정의할 수 있으며 이는 매우 실용적인 가치가 있습니다.

먼저 Free Spire.PDF 구성 요소의 무료 버전을 다운로드하세요. 구성 요소를 다운로드하고 설치한 후 Visual Studio는 C# 콘솔 프로젝트를 만들고 bin 폴더의 .DLL을 참조로 추가하며 다음 네임스페이스를 추가합니다.

using System;
using System.Drawing;
using System.Windows.Forms;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Annotations;

이제 주석을 추가하는 방법을 살펴보겠습니다. 새 문서.

1단계: 새 PDF 문서 개체 를 만들고 새 페이지를 추가합니다.

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

2단계: 문서에 텍스트를 추가하고 텍스트의 위치, 글꼴 크기, 색상을 설정합니다.

PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13);
string text = "HelloWorld";
PointF point = new PointF(200, 100);
page.Canvas.DrawString(text, font, PdfBrushes.Red, point);

3단계: 텍스트에 주석을 추가하고 주석의 테두리, 색상, 위치를 설정합니다.

PdfTextMarkupAnnotation annotation1 = new PdfTextMarkupAnnotation("管理员", "一般来说,这是每一种计算机编程语言中最基本、最简单的程序", text, new PointF(0, 0), font);
annotation1.Border = new PdfAnnotationBorder(0.75f);
annotation1.TextMarkupColor = Color.Green;
annotation1.Location = new PointF(point.X + doc.PageSettings.Margins.Left, point.Y + doc.PageSettings.Margins.Left);

4단계: 페이지에 댓글을 추가하고 마지막으로 문서를 저장합니다.

(page as PdfNewPage).Annotations.Add(annotation1);
doc.SaveToFile("result.pdf");

댓글을 추가한 후의 렌더링입니다:

전체 코드:

PdfDocument doc = new PdfDocument();
      PdfPageBase page = doc.Pages.Add();
      PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13);
      string text = "HelloWorld";
      PointF point = new PointF(200, 100);
      page.Canvas.DrawString(text, font, PdfBrushes.Red, point);
 
      PdfTextMarkupAnnotation annotation1 = new PdfTextMarkupAnnotation("管理员", "一般来说,这是每一种计算机编程语言中最基本、最简单的程序", text, new PointF(0, 0), font);
      annotation1.Border = new PdfAnnotationBorder(0.75f);
      annotation1.TextMarkupColor = Color.Green;
      annotation1.Location = new PointF(point.X + doc.PageSettings.Margins.Left, point.Y + doc.PageSettings.Margins.Left);
      (page as PdfNewPage).Annotations.Add(annotation1);
      doc.SaveToFile("result.pdf");
      System.Diagnostics.Process.Start("result.pdf");

자유 텍스트 댓글 추가

마찬가지로 문서에 자유 텍스트 댓글을 추가하는 것도 비교적 간단합니다.

1단계: 새 PDF 문서 개체를 만들고 새 페이지를 추가합니다.

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

2단계: PdfFreeTextAnnotation을 초기화한 다음 주석 텍스트를 사용자 정의합니다.

RectangleF rect = new RectangleF(0, 40, 150, 50);
PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);
textAnnotation.Text = "Free text annotation ";

3단계: 글꼴, 채우기 색상, 테두리 색상, 투명도 등 주석의 속성을 설정합니다.

PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 10);
PdfAnnotationBorder border = new PdfAnnotationBorder(1f);
textAnnotation.Font = font;
textAnnotation.Border = border;
textAnnotation.BorderColor = Color. Purple;
textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle;
textAnnotation.Color = Color. Pink;
textAnnotation.Opacity = 0.8f;

4단계: 페이지에 댓글을 추가합니다.

page.AnnotationsWidget.Add(textAnnotation);

5단계: 문서를 저장하고 다시 엽니다.

doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start("FreeTextAnnotation.pdf");

자유 텍스트 댓글을 추가한 렌더링입니다:

전체 코드:

PdfDocument doc = new PdfDocument();
      PdfPageBase page = doc.Pages.Add();
      
      RectangleF rect = new RectangleF(0, 40, 150, 50);
      PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);
      textAnnotation.Text = "Free text annotation ";
    
      PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 10);
      PdfAnnotationBorder border = new PdfAnnotationBorder(1f);
      textAnnotation.Font = font;
      textAnnotation.Border = border;
      textAnnotation.BorderColor = Color. Purple;
      textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle;
      textAnnotation.Color = Color.Pink;
      textAnnotation.Opacity = 0.8f;
      
      page.AnnotationsWidget.Add(textAnnotation);
      doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF);
      System.Diagnostics.Process.Start("FreeTextAnnotation.pdf");

위 내용은 C#을 사용하여 PDF 문서에 주석을 추가하기 위한 샘플 코드 공유(그림)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.