欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

利用C#如何給PDF文檔添加文本與圖片頁眉

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

前言

下面這篇文章向大家分享如何使用了免費組件Free Spire.PDF給PDF文檔添加文本和圖片頁眉。這個組件提供了一些方法,可以幫助我們快速方便地實現(xiàn)此目的。

添加頁眉步驟:

首先,創(chuàng)建一個Visual C#控制臺項目,添加組件引用并使用以下命名空間。

using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;

在下列代碼中,我們先定義一個SetDocumentTemplate()方法來創(chuàng)建一個PDF文檔模板,這個模板只包含文本和圖片頁眉。然后,調用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)
{
 //創(chuàng)建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); 
}

接下來再創(chuàng)建一個PDF文檔,主函數(shù)內調用SetDocumentTemplate()方法將帶有文本和圖片頁眉的模板應用到新建的PDF文檔中。

具體步驟:

第一步:創(chuàng)建一個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);

第四步: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#能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對我們的支持。

上一篇:C#使用GET、POST請求獲取結果

欄    目:C#教程

下一篇:C# #define條件編譯詳解

本文標題:利用C#如何給PDF文檔添加文本與圖片頁眉

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6039.html

網頁制作CMS教程網絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網 版權所有