C#可以用itextsharp 打开PDF文件吗?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#可以用itextsharp 打开PDF文件吗?相关的知识,希望对你有一定的参考价值。
C#可以用itextsharp 打开PDF文件吗? 如果可以 请问如何打开。
其实我想实现的功能就是 在电脑上没有装任何PDF阅读器的情况下打开PDF 有网友说itextsharp可以实现。
请大神支招 坐等
要在电脑上展示
追答那就需要你用itextsharp开发一个pdf阅读器了
追问这么麻烦啊 还有其它办法可以实现这个功能吗 在没有安装PDF阅读器的情况下打开PDF文件,在窗体上打开也行
追答试试webBrowser,webBrowser1.Navigate(@"C:\xx\test.pdf");
追问打不开 只有在装了PDF阅读器情况下才可以打开
参考技术A 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#工具类:使用iTextSharp操作PDF文档
iTextSharp是一个用于操作PDF文件的组件DLL程序,在C#程序中可以引用iTextSharp组件,用于开发与PDF文件相关的报表等功能,利用iTextSharp组件提供出来的方法接口,我们可以实现很多与PDF文档有关的操作,如打开PDF文档对象、往PDF文档中添加段落、添加图片链接等等,功能非常的强大。这边简单对iTextSharp类进行了封装,提供一些常用的PDF操作方法。iTextSharp官网:http://www.itextpdf.com/ (英文好的建议直接查看原始文档)。
在Visual Studio开发的项目的过程中,你可以手动引入iTextSharp的两个DLL文件到你的项目中,引入成功后即可在项目中使用。如果你的Visual Studio安装有NuGet 工具,可以通过NuGet工具来自动安装。
依托iTextSharp组件封装好的PDF操作帮助类如下,只包含一些简单的操作,如果读者有更复杂的需求,请查阅官方文档后熟悉后自行封装编写:
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#工具类:使用SharpZipLib进行压缩、解压文件。
备注:此原创文章最早发表于博主个人技术站点:IT技术小趣屋。原文链接:C#工具类:使用iTextSharp操作PDF文档_IT技术小趣屋。
以上是关于C#可以用itextsharp 打开PDF文件吗?的主要内容,如果未能解决你的问题,请参考以下文章
iTextSharp:创建 PDF 文件时显示“打开/保存”对话框
c#如何将数据导出pdf文件,我用的是vs2010,里边没有水晶报表。然后用itextsharp就报字体啥的错误。