C#中建立PDF網格並插入圖片的方法_C#教程

來源:互聯網
上載者:User

這篇文章我將向大家示範如何以編程的方式在PDF文檔中建立一個網格,並將圖片插入特定的網格中。

網上有一些類似的解決方案,在這裡我選擇了一個免費版的PDF組件。安裝控制項後,建立新項目,添加安裝目錄下的dll檔案作為項目的引用以及命名空間,如下:

using Spire.Pdf;using Spire.Pdf.Graphics;using Spire.Pdf.Grid;

接下來是詳細步驟及程式碼片段:

步驟1: 首先建立一個PDF文檔,並添加一個新頁面。

PdfDocument doc = new PdfDocument();PdfPageBase page = doc.Pages.Add();

步驟2:建立一個一行兩列的網格。

PdfGrid grid = new PdfGrid();PdfGridRow row = grid.Rows.Add();grid.Columns.Add(2);

步驟3:設定儲存格邊框與填充內容的間距。

grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);

步驟4:設定列寬。

float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);grid.Columns[0].Width = width * 0.1f;grid.Columns[1].Width = width * 0.1f;

步驟5:載入圖片。

PdfGridCellTextAndStyleList lst = new PdfGridCellTextAndStyleList();PdfGridCellTextAndStyle textAndStyle = new PdfGridCellTextAndStyle();textAndStyle.Image=PdfImage.FromFile(@"C:\Users\Administrator\Pictures\448a5ba8f8851709a1f53e.jpg");

步驟6:設定圖片的大小,將其插入第一個儲存格。

textAndStyle.ImageSize = new SizeF(50, 50);lst.List.Add(textAndStyle);row.Cells[0].Value = lst;

步驟7:在頁面特定位置繪製PDF網格。

PdfLayoutResult result = grid.Draw(page, new PointF(10, 30));

步驟8:儲存並運行PDF檔案。

doc.SaveToFile(outputFile, FileFormat.PDF);System.Diagnostics.Process.Start(outputFile);

效果圖:

這個Spire. PDF組件基於.NET的辦公軟體庫,還有其他豐富的功能。所以對於有辦公開發需求的朋友,感興趣的話可以在官網參考線上教程。

全部代碼:

using System;using System.Drawing;using System.Windows.Forms;using Spire.Pdf;using Spire.Pdf.Graphics;using Spire.Pdf.Grid;namespace Insert_an_Image_to_PDF_Grid_Cell{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){string outputFile ="output.pdf";//建立一個PDF文檔PdfDocument doc = new PdfDocument();//添加頁面PdfPageBase page = doc.Pages.Add();//建立PDF網格PdfGrid grid = new PdfGrid();//設定儲存格邊框與填充內容的間距grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);//添加行PdfGridRow row = grid.Rows.Add();//添加列grid.Columns.Add(2);float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);//設定列寬grid.Columns[0].Width = width * 0.1f;grid.Columns[1].Width = width * 0.1f;//載入圖片PdfGridCellTextAndStyleList lst = new PdfGridCellTextAndStyleList();PdfGridCellTextAndStyle textAndStyle = new PdfGridCellTextAndStyle();textAndStyle.Image=PdfImage.FromFile (@"C:\Users\Administrator\Pictures\448a5ba8f8851709a1f53e.jpg");//設定圖片大小textAndStyle.ImageSize = new SizeF(50, 50);lst.List.Add(textAndStyle);//在第一個儲存格添加圖片row.Cells[0].Value = lst;//在頁面特定位置繪製PDF網格PdfLayoutResult result = grid.Draw(page, new PointF(10, 30));//儲存並運行PDF檔案doc.SaveToFile(outputFile, FileFormat.PDF);System.Diagnostics.Process.Start(outputFile);}}}

以上所述是小編給大家介紹的C#中建立PDF網格並插入圖片的方法,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.