利用C#如何給PDF文檔添加文本與圖片頁(yè)眉
前言
下面這篇文章向大家分享如何使用了免費(fèi)組件Free Spire.PDF給PDF文檔添加文本和圖片頁(yè)眉。這個(gè)組件提供了一些方法,可以幫助我們快速方便地實(shí)現(xiàn)此目的。
添加頁(yè)眉步驟:
首先,創(chuàng)建一個(gè)Visual C#控制臺(tái)項(xiàng)目,添加組件引用并使用以下命名空間。
using System; using System.Drawing; using Spire.Pdf; using Spire.Pdf.Graphics;
在下列代碼中,我們先定義一個(gè)SetDocumentTemplate()
方法來(lái)創(chuàng)建一個(gè)PDF文檔模板,這個(gè)模板只包含文本和圖片頁(yè)眉。然后,調(diào)用DrawString(string s, PdfFontBase font, PdfBrush brush, float x, float y, PdfStringFormat format)
方法和DrawImage(PdfImage image, float x, float y, float width, float height)
方法,插入自定義的文本和圖片頁(yè)眉。
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; //添加文本頁(yè)眉 PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("宋體", 15f), true); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right); String Text = "PDF文本頁(yè)眉"; float y = 0; float x = PdfPageSize.A4.Width; topSpace.Graphics.DrawString(Text, font1, PdfBrushes.PaleVioletRed, x, y, format); //添加圖片頁(yè)眉 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); }
接下來(lái)再創(chuàng)建一個(gè)PDF文檔,主函數(shù)內(nèi)調(diào)用SetDocumentTemplate()
方法將帶有文本和圖片頁(yè)眉的模板應(yīng)用到新建的PDF文檔中。
具體步驟:
第一步:創(chuàng)建一個(gè)PDF文檔對(duì)象。
PdfDocument doc = new PdfDocument();
第二步:設(shè)置頁(yè)邊距。
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文檔中應(yīng)用模板。
SetDocumentTemplate(doc, PdfPageSize.A4, margin);
第四步:PDF文檔添加頁(yè)面。
PdfPageBase page = doc.Pages.Add(); doc.Pages.Add();
第五步:保存并打開(kāi)文檔。
doc.SaveToFile("頁(yè)眉.pdf"); System.Diagnostics.Process.Start("頁(yè)眉.pdf");
添加頁(yè)眉后的效果圖:
全部代碼:
using System; using Spire.Pdf; using System.Drawing; using Spire.Pdf.Graphics; namespace PDF添加頁(yè)眉 { 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("頁(yè)眉.pdf"); System.Diagnostics.Process.Start("頁(yè)眉.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文本頁(yè)眉"; 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); } } }
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家學(xué)習(xí)或者使用C#能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
C#?webApi創(chuàng)建與發(fā)布、部署、api調(diào)用詳細(xì)教程
這篇文章主要給大家介紹了關(guān)于C#?webApi創(chuàng)建與發(fā)布、部署、api調(diào)用的相關(guān)資料,WebApi是微軟在VS2012?MVC4版本中綁定發(fā)行的,WebApi是完全基于Restful標(biāo)準(zhǔn)的框架,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12C#簡(jiǎn)單實(shí)現(xiàn)表達(dá)式目錄樹(shù)(Expression)
表達(dá)式目錄樹(shù)以數(shù)據(jù)形式表示語(yǔ)言級(jí)別代碼。數(shù)據(jù)存儲(chǔ)在樹(shù)形結(jié)構(gòu)中。表達(dá)式目錄樹(shù)中的每個(gè)節(jié)點(diǎn)都表示一個(gè)表達(dá)式。這篇文章給大家介紹C#簡(jiǎn)單實(shí)現(xiàn)表達(dá)式目錄樹(shù)(Expression),需要的朋友參考下吧2017-11-11C#使用XSLT實(shí)現(xiàn)xsl、xml與html相互轉(zhuǎn)換
這篇文章介紹了C#使用XSLT實(shí)現(xiàn)xsl、xml與html相互轉(zhuǎn)換的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06C#程序最小化到托盤(pán)圖標(biāo)操作步驟與實(shí)現(xiàn)代碼
設(shè)置窗體屬性showinTask=false;加notifyicon控件notifyIcon1,為控件notifyIcon1的屬性Icon添加一個(gè)icon圖標(biāo);添加窗體最小化事件(首先需要添加事件引用)接下來(lái)介紹實(shí)現(xiàn)代碼,感興趣的朋友可以研究下2012-12-12基于C#解決庫(kù)存扣減及訂單創(chuàng)建時(shí)防止并發(fā)死鎖的問(wèn)題
這篇文章主要介紹了基于C#解決庫(kù)存扣減及訂單創(chuàng)建時(shí)防止并發(fā)死鎖的問(wèn)題,很多開(kāi)發(fā)人員對(duì)于這個(gè)問(wèn)題的排查起來(lái)是比較困難的,而生產(chǎn)生的原因多種多樣,很多人認(rèn)是因?yàn)楸碇械臄?shù)據(jù)太多了同時(shí)操作的人多人才會(huì)產(chǎn)生這種錯(cuò)誤,下面我們來(lái)還原一下死鎖的過(guò)程2022-05-05Unity3D Shader實(shí)現(xiàn)掃描顯示效果(2)
這篇文章主要為大家詳細(xì)介紹了Unity3D Shader實(shí)現(xiàn)掃描顯示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03