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