导航:首页 > 文档加密 > itextsharp打印pdf

itextsharp打印pdf

发布时间:2024-08-10 15:05:59

1. C#如何编写把图片转换为pdf格式的程序

我前两天正好写了这样一个程序:(C#的)
你首先要下载 iTextSharp.dll文件,之后添加到引用中
我给你提供一个类 :
public void ExportDataIntoPDF(string pathName, String path)
{//导出至PDF
iTextSharp.text.Document document = new iTextSharp.text.Document();
try
{
iTextSharp .text .pdf .PdfWriter .GetInstance (document, new FileStream(pathName, FileMode.CreateNew ));
document.Open();
iTextSharp.text.pdf.BaseFont bfChinese = iTextSharp.text.pdf.BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font Titlefont = new iTextSharp.text.Font(bfChinese, 16, iTextSharp.text.Font.BOLD , new iTextSharp.text.Color(0, 0, 0));

document.Add(new Paragraph(" 将图片转成PDF:" + "\n", Titlefont));
iTextSharp.text.Image je = iTextSharp.text.Image.GetInstance(path);//path就是函数入参,即你要转成PDF的图片路径
document.Add(je);
}

catch (Exception de)
{
MessageBox .Show (de.ToString());
}
document.Close();
}
你调用这个类就ok了

2. 怎么用iTextSharp获得PDF文件中的某一页内容

本文实例讲述了C#使用iTextSharp从PDF文档获取内容的方法。分享给大家供大家参考。具体实现方法如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ExtractTextFromPDFPage("c:\sample.pdf", 1);
}
public void ExtractTextFromPDFPage(string pdfFile, int pageNumber)
{
PdfReader reader = new PdfReader(pdfFile);
string text = PdfTextExtractor.GetTextFromPage(reader, pageNumber);
try { reader.Close(); }
catch { }
richTextBox1.Text = text;
}
}
}

3. itextsharp 可以显示pdf文件吗

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
}
}

阅读全文

与itextsharp打印pdf相关的资料

热点内容
r11s手机管家没有加密 浏览:779
怎么看电脑连接哪个服务器 浏览:189
二手服务器设备欺诈如何解决 浏览:877
单片机服务器安装win10 浏览:658
胸椎压缩性骨折伤残 浏览:954
mt怎么解压文件 浏览:41
达芬奇项目服务器有什么用 浏览:854
自制怎么捏都可以复原的解压球 浏览:615
qq软件管理怎么加密 浏览:740
手机使用代码编程器 浏览:939
单片机四位99秒表制作流程图 浏览:617
压缩包软件如何安装 浏览:767
我的世界服务器1122地址 浏览:5
苹果如何与安卓系统连接服务器 浏览:934
农业银行app学生缴费怎么切换 浏览:470
福建在哪个app上有安康码 浏览:547
用友商贸宝t1登录找不到加密狗 浏览:557
区间测速算法不正确 浏览:333
appstore转到哪个国家比较好 浏览:926
程序员为什么被公司埋炸弹 浏览:943