C#給PDF每一頁都加上自己的圖片logo標(biāo)識
當(dāng)前位置:點(diǎn)晴教程→知識管理交流
→『 技術(shù)文檔交流 』
自己做了一個PDF說明文件給客戶,現(xiàn)在需要在每一頁上都加上自己的logo圖片,具體操作過程如下: 建項(xiàng)目這些就省略了,直接上代碼:using iTextSharp.text.pdf; using System; using System.IO; using System.Windows.Forms; namespace Pic2PDF { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string path = Application.StartupPath; //源PDF地址 string pdffilename = path + "\\test.pdf"; //加完圖片的PDF地址 string pdffilename1 = path + "\\test1.pdf"; //圖片文件地址 string picfilename = path + "\\sign.jpg"; //執(zhí)行操作,加上圖片 string r = AddPics(pdffilename, pdffilename1, picfilename); MessageBox.Show(r); } public static string AddPics(string PdfPath, string OutPdfPath, string picpath) { try { if (File.Exists(OutPdfPath)) { File.delete(OutPdfPath); } PdfReader reader = new PdfReader(PdfPath); PdfStamper stamp = new PdfStamper(reader, new FileStream(OutPdfPath, FileMode.create)); int n = reader.NumberOfPages; int i = 0; PdfContentByte under; iTextSharp.text.Image im = iTextSharp.text.Image.GetInstance(picpath); PDFATT att = new PDFATT(PdfPath); float ww = att.Width(); float hh = att.Height(); float w = im.Width; float h = im.Height; float locationx; float locationy; locationx = ww - w - 20; locationy = 50; im.SetAbsolutePosition(locationx, locationy); im.ScaleAbsolute(w, h); while (i < n) { i++; under = stamp.GetOverContent(i); under.AddImage(im, false); } stamp.Close(); reader.Close(); } catch (Exception ex) { return ex.Message; } return "ok"; } class PDFATT { PdfReader reader; public PDFATT(string iPdfFilePath) { reader = new PdfReader(iPdfFilePath); } public int PageCount() { return reader.NumberOfPages; } public float Width() { return reader.GetPageSize(1).Width; } public float Height() { return reader.GetPageSize(1).Height; } } } } 我這里設(shè)置的是添加到右下角了,位置的代碼主要在這個地方PDFATT att = new PDFATT(PdfPath); float ww = att.Width(); //PDF的寬度 float hh = att.Height(); //PDF的高度 float w = im.Width; //圖片寬 float h = im.Height; //圖片高 float locationx; float locationy; locationx = ww - w - 20; //橫向位置自己計算 locationy = 50; //縱向位置自己試下就行 其它的都很簡單了。 運(yùn)行效果如下:該文章在 2023/5/22 9:37:43 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |