C\#를 이용 하여 PDF 문서 에 텍스트 와 그림 머 릿 말 을 추가 하 는 방법
무료 구성 요소Free Spire.PDF를 사용 하여 PDF 문서 에 텍스트 와 그림 머 릿 말 을 추가 하 는 방법 을 공유 합 니 다.이 구성 요 소 는 우리 가 이 목적 을 신속 하고 편리 하 게 실현 하 는 데 도움 을 줄 수 있 는 방법 을 제공 했다.
머 릿 말 추가:
우선,Visual C\#콘 솔 항목 을 만 들 고 구성 요소 인용 을 추가 하 며 다음 네 임 스페이스 를 사용 합 니 다.
using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
다음 코드 에서 PDF 문서 템 플 릿 을 만 드 는 방법SetDocumentTemplate()
을 정의 합 니 다.이 템 플 릿 은 텍스트 와 그림 머 릿 말 만 포함 합 니 다.그 다음 에DrawString(string s, PdfFontBase font, PdfBrush brush, float x, float y, PdfStringFormat format)
방법 과DrawImage(PdfImage image, float x, float y, float width, float height)
방법 을 호출 하여 사용자 정의 텍스트 와 그림 머 릿 말 을 삽입 합 니 다.
static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
{
// PDF
PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top);
topSpace.Foreground = true;
doc.Template.Top = topSpace;
//
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font(" ", 15f), true);
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
String Text = "PDF ";
float y = 0;
float x = PdfPageSize.A4.Width;
topSpace.Graphics.DrawString(Text, font1, PdfBrushes.PaleVioletRed, x, y, format);
//
PdfImage headerImage = PdfImage.FromFile(@"logo.png");
float width = headerImage.Width;
float height = headerImage.Height;
PointF pageLeftTop = new PointF(0 , 0);
topSpace.Graphics.DrawImage(headerImage,0,0,width/2,height/2);
}
다음은 PDF 문 서 를 만 듭 니 다.주 함수 내 호출SetDocumentTemplate()
방법 은 텍스트 와 그림 머 릿 말 이 있 는 템 플 릿 을 새 PDF 문서 에 적용 합 니 다.구체 적 인 절차:
첫 번 째 단계:PDF 문서 대상 을 만 듭 니 다.
PdfDocument doc = new PdfDocument();
두 번 째 단계:페이지 간격 설정.
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(4.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
세 번 째 단계:PDF 문서 에 템 플 릿 을 적용 합 니 다.
SetDocumentTemplate(doc, PdfPageSize.A4, margin);
STEP 4:PDF 문서 추가 페이지.
PdfPageBase page = doc.Pages.Add();
doc.Pages.Add();
다섯 번 째 단계:문 서 를 저장 하고 엽 니 다.
doc.SaveToFile(" .pdf");
System.Diagnostics.Process.Start(" .pdf");
머 릿 말 추가 후 효과 그림:모든 코드:
using System;
using Spire.Pdf;
using System.Drawing;
using Spire.Pdf.Graphics;
namespace PDF
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(4.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
SetDocumentTemplate(doc, PdfPageSize.A4, margin);
PdfPageBase page = doc.Pages.Add();
doc.Pages.Add();
doc.SaveToFile(" .pdf");
System.Diagnostics.Process.Start(" .pdf");
}
static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
{
PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top);
topSpace.Foreground = true;
doc.Template.Top = topSpace;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font(" ", 15f), true);
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
String Text = "PDF ";
float y = 0;
float x = PdfPageSize.A4.Width;
topSpace.Graphics.DrawString(Text, font1, PdfBrushes.PaleVioletRed, x, y, format);
PdfImage headerImage = PdfImage.FromFile(@"C:\Users\Administrator\Pictures\under_construction.jpg");
float width = headerImage.Width;
float height = headerImage.Height;
PointF pageLeftTop = new PointF(0, 0);
topSpace.Graphics.DrawImage(headerImage, 0, 0, width / 2, height / 2);
}
}
}
총결산이상 은 이 글 의 전체 내용 입 니 다.본 논문 의 내용 이 여러분 에 게 C\#를 배우 거나 사용 하 는 데 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 댓 글 을 남 겨 주 셔 서 저희 에 대한 지지 에 감 사 드 립 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
C#Task를 사용하여 비동기식 작업을 수행하는 방법라인이 완성된 후에 이 라인을 다시 시작할 수 없습니다.반대로 조인(Join)만 결합할 수 있습니다 (프로세스가 현재 라인을 막습니다). 임무는 조합할 수 있는 것이다. 연장을 사용하여 그것들을 한데 연결시키는 것이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.