C#可以用itextsharp 打开PDF文件吗?

2025-02-24 14:45:04
推荐回答(2个)
回答1:

itextsharp里封装的都有,你指的打开是要在电脑上展示信息,还是用软件可以读到pdf信息

回答2:

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace DotNet.Utilities
{
///


/// PDF文档操作类
///

//------------------------------------调用--------------------------------------------
//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 构造函数
///
/// 构造函数
///

public PDFOperation()
{
rect = PageSize.A4;
document = new Document(rect);
}

///
/// 构造函数
///

/// 页面大小(如"A4")
public PDFOperation(string type)
{
SetPageSize(type);
document = new Document(rect);
}

///
/// 构造函数
///

/// 页面大小(如"A4")
/// 内容距左边框距离
/// 内容距右边框距离
/// 内容距上边框距离
/// 内容距下边框距离
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 设置字体
///
/// 设置字体
///

public void SetBaseFont(string path)
{
basefont = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
}

///
/// 设置字体
///

/// 字体大小
public void SetFont(float size)
{
font = new Font(basefont, size);
}
#endregion

#region 设置页面大小
///
/// 设置页面大小
///

/// 页面大小(如"A4")
public void SetPageSize(string type)
{
switch (type.Trim())
{
case "A4":
rect = PageSize.A4;
break;
case "A8":
rect = PageSize.A8;
break;
}
}
#endregion

#region 实例化文档
///
/// 实例化文档
///

/// 文档相关信息(如路径,打开方式等)
public void GetInstance(Stream os)
{
PdfWriter.GetInstance(document, os);
}
#endregion

#region 打开文档对象
///
/// 打开文档对象
///

/// 文档相关信息(如路径,打开方式等)
public void Open(Stream os)
{
GetInstance(os);
document.Open();
}
#endregion

#region 关闭打开的文档
///
/// 关闭打开的文档
///

public void Close()
{
document.Close();
}
#endregion

#region 添加段落
///
/// 添加段落
///

/// 内容
/// 字体大小
public void AddParagraph(string content, float fontsize)
{
SetFont(fontsize);
Paragraph pra = new Paragraph(content, font);
document.Add(pra);
}

///
/// 添加段落
///

/// 内容
/// 字体大小
/// 对齐方式(1为居中,0为居左,2为居右)
/// 段后空行数(0为默认值)
/// 段前空行数(0为默认值)
/// 行间距(0为默认值)
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 添加图片
///
/// 添加图片
///

/// 图片路径
/// 对齐方式(1为居中,0为居左,2为居右)
/// 图片宽(0为默认值,如果宽度大于页宽将按比率缩放)
/// 图片高
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 添加链接、点
///
/// 添加链接
///

/// 链接文字
/// 字体大小
/// 链接地址
public void AddAnchorReference(string Content, float FontSize, string Reference)
{
SetFont(FontSize);
Anchor auc = new Anchor(Content, font);
auc.Reference = Reference;
document.Add(auc);
}

///
/// 添加链接点
///

/// 链接文字
/// 字体大小
/// 链接点名
public void AddAnchorName(string Content, float FontSize, string Name)
{
SetFont(FontSize);
Anchor auc = new Anchor(Content, font);
auc.Name = Name;
document.Add(auc);
}
#endregion
}
}