C\#를 이용 하여 PDF 문서 에 텍스트 와 그림 머 릿 말 을 추가 하 는 방법

4921 단어 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\#를 배우 거나 사용 하 는 데 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 댓 글 을 남 겨 주 셔 서 저희 에 대한 지지 에 감 사 드 립 니 다.

좋은 웹페이지 즐겨찾기