C#中PDF文件操作類
PDFOperation.cs
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace DotNet.Utilities
{
/// <summary>
/// PDF文件操作類
/// </summary>
//------------------------------------呼叫--------------------------------------------
//PDFOperation pdf = new PDFOperation();
//pdf.Open(new FileStream(path, FileMode.Create));
//pdf.SetBaseFont(@"C:\Windows\Fonts\SIMHEI.TTF");
//pdf.AddParagraph("測試文件(生成時間:" + DateTime.Now + ")", 15, 1, 20, 0, 0);
//pdf.Close();
//-------------------------------------------------------------------------------------
public class PDFOperation
{
#region 建構函式
/// <summary>
/// 建構函式
/// </summary>
public PDFOperation()
{
rect = PageSize.A4;
document = new Document(rect);
}
/// <summary>
/// 建構函式
/// </summary>
/// <param name="type">頁面大小(如"A4")</param>
public PDFOperation(string type)
{
SetPageSize(type);
document = new Document(rect);
}
/// <summary>
/// 建構函式
/// </summary>
/// <param name="type">頁面大小(如"A4")</param>
/// <param name="marginLeft">內容距左邊框距離</param>
/// <param name="marginRight">內容距右邊框距離</param>
/// <param name="marginTop">內容距上邊框距離</param>
/// <param name="marginBottom">內容距下邊框距離</param>
public PDFOperation(string type, float marginLeft, float marginRight, float marginTop, float marginBottom)
{
SetPageSize(type);
document = new Document(rect, marginLeft, marginRight, marginTop, marginBottom);
}
#endregion
#region 私有欄位
private Font font;
private Rectangle rect; //文件大小
private Document document;//文件物件
private BaseFont basefont;//字型
#endregion
#region 設定字型
/// <summary>
/// 設定字型
/// </summary>
public void SetBaseFont(string path)
{
basefont = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
}
/// <summary>
/// 設定字型
/// </summary>
/// <param name="size">字型大小</param>
public void SetFont(float size)
{
font = new Font(basefont, size);
}
#endregion
#region 設定頁面大小
/// <summary>
/// 設定頁面大小
/// </summary>
/// <param name="type">頁面大小(如"A4")</param>
public void SetPageSize(string type)
{
switch (type.Trim())
{
case "A4":
rect = PageSize.A4;
break;
case "A8":
rect = PageSize.A8;
break;
}
}
#endregion
#region 例項化文件
/// <summary>
/// 例項化文件
/// </summary>
/// <param name="os">文件相關資訊(如路徑,開啟方式等)</param>
public void GetInstance(Stream os)
{
PdfWriter.GetInstance(document, os);
}
#endregion
#region 開啟文件物件
/// <summary>
/// 開啟文件物件
/// </summary>
/// <param name="os">文件相關資訊(如路徑,開啟方式等)</param>
public void Open(Stream os)
{
GetInstance(os);
document.Open();
}
#endregion
#region 關閉開啟的文件
/// <summary>
/// 關閉開啟的文件
/// </summary>
public void Close()
{
document.Close();
}
#endregion
#region 新增段落
/// <summary>
/// 新增段落
/// </summary>
/// <param name="content">內容</param>
/// <param name="fontsize">字型大小</param>
public void AddParagraph(string content, float fontsize)
{
SetFont(fontsize);
Paragraph pra = new Paragraph(content, font);
document.Add(pra);
}
/// <summary>
/// 新增段落
/// </summary>
/// <param name="content">內容</param>
/// <param name="fontsize">字型大小</param>
/// <param name="Alignment">對齊方式(1為居中,0為居左,2為居右)</param>
/// <param name="SpacingAfter">段後空行數(0為預設值)</param>
/// <param name="SpacingBefore">段前空行數(0為預設值)</param>
/// <param name="MultipliedLeading">行間距(0為預設值)</param>
public void AddParagraph(string content, float fontsize, int Alignment, float SpacingAfter, float SpacingBefore, float MultipliedLeading)
{
SetFont(fontsize);
Paragraph pra = new Paragraph(content, font);
pra.Alignment = Alignment;
if (SpacingAfter != 0)
{
pra.SpacingAfter = SpacingAfter;
}
if (SpacingBefore != 0)
{
pra.SpacingBefore = SpacingBefore;
}
if (MultipliedLeading != 0)
{
pra.MultipliedLeading = MultipliedLeading;
}
document.Add(pra);
}
#endregion
#region 新增圖片
/// <summary>
/// 新增圖片
/// </summary>
/// <param name="path">圖片路徑</param>
/// <param name="Alignment">對齊方式(1為居中,0為居左,2為居右)</param>
/// <param name="newWidth">圖片寬(0為預設值,如果寬度大於頁寬將按比率縮放)</param>
/// <param name="newHeight">圖片高</param>
public void AddImage(string path, int Alignment, float newWidth, float newHeight)
{
Image img = Image.GetInstance(path);
img.Alignment = Alignment;
if (newWidth != 0)
{
img.ScaleAbsolute(newWidth, newHeight);
}
else
{
if (img.Width > PageSize.A4.Width)
{
img.ScaleAbsolute(rect.Width, img.Width * img.Height / rect.Height);
}
}
document.Add(img);
}
#endregion
#region 新增連結、點
/// <summary>
/// 新增連結
/// </summary>
/// <param name="Content">連結文字</param>
/// <param name="FontSize">字型大小</param>
/// <param name="Reference">連結地址</param>
public void AddAnchorReference(string Content, float FontSize, string Reference)
{
SetFont(FontSize);
Anchor auc = new Anchor(Content, font);
auc.Reference = Reference;
document.Add(auc);
}
/// <summary>
/// 新增連結點
/// </summary>
/// <param name="Content">連結文字</param>
/// <param name="FontSize">字型大小</param>
/// <param name="Name">連結點名</param>
public void AddAnchorName(string Content, float FontSize, string Name)
{
SetFont(FontSize);
Anchor auc = new Anchor(Content, font);
auc.Name = Name;
document.Add(auc);
}
#endregion
}
}
相關文章
- C#中URL的操作類C#
- C# 將PDF文件轉換為Markdown文件C#
- C# 在PDF文件中應用多種不同字型C#
- 用C#實現生成PDF文件(原碼)C#
- C# 給PDF文件設定過期時間C#
- C# 提取PDF中的表格C#
- c# Lambda操作類封裝C#封裝
- C# Array類的基本操作C#
- C#操作SQL Server通用類C#SQLServer
- Excelize - Golang 操作 Office Excel 文件類庫ExcelizeGolang
- Ubuntu中使用pdftk合併、分離PDF文件等操作Ubuntu
- pdf文件相關操作軟體簡介及其下載
- 公共的Json操作C#類JSONC#
- Java 生成 PDF 文件Java
- JSP生成WORD文件,EXCEL文件,PDF文件JSExcel
- C#神器"BlockingCollection"類實現C#神仙操作C#BloCGC
- C#中操作IIS 7.0C#
- 在C#中操作XML .C#XML
- C#生成PDFC#
- C#:資料庫SQL操作通用類C#資料庫SQL
- POI操作Excel文件-中級篇Excel
- PDF文件搜尋工具:PDF Search Mac版Mac
- java-pdf-itext7、itextpdf 生成pdf 文件Java
- c#中DateTime類C#
- FastReport資訊:Blazor中的報告和PDF文件ASTBlazor
- Laravel 生成 PDF 文件 - tcpdfLaravelTCP
- Java 操作PDFJava
- c#中的insert操作C#
- C#中索引器的操作C#索引
- Master PDF Editor for Mac PDF文件編輯軟體ASTMac
- PDF文件壓縮工具:Cisdem PDF Compressor for MacMac
- C#的Access資料庫操作 AccessHelper類C#資料庫
- C#中類的繼承C#繼承
- toLua中Lua呼叫C#中的類C#
- java生成簡易pdf文件Java
- pdf轉換成word文件
- C# 將PDF轉為線性化PDFC#
- PDF文件轉換器PDF to Word Document Converter MacMac