基本信息
源码名称:C# JPG转PDF
源码大小:1.42M
文件格式:.rar
开发语言:C#
更新时间:2019-03-20
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
调用itextsharp.dll实现 JPG转PDF
转换效果如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace jpgTOpdf
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void jpgbutton_Click(object sender, EventArgs e)
{
FolderBrowserDialog path = new FolderBrowserDialog();
path.ShowDialog();
jpgtextBox.Text = path.SelectedPath;
}
private void pdfbutton_Click(object sender, EventArgs e)
{
FolderBrowserDialog path = new FolderBrowserDialog();
path.ShowDialog();
pdftextBox.Text = path.SelectedPath;
}
private void zhbutton_Click(object sender, EventArgs e)
{
if (jpgtextBox.Text != "" && pdftextBox.Text != "")
{
try
{
var files = Directory.GetFiles(jpgtextBox.Text, "*.tif");
label3.Text = "共发现:" files.Count().ToString() "个文件";
foreach (var file in files)
{
图片转PDF(file, pdftextBox.Text "\\" Path.GetFileNameWithoutExtension(file) ".pdf");
}
MessageBox.Show("导出完成!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("JPG路径或PDF路径不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void 图片转PDF(string source, string output)//图片转PDF
{
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(source);//
using (FileStream fs = new FileStream(output, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (Document doc = new Document(image))
{
using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
{
//writer.CompressionLevel = 0;
writer.SetFullCompression();
writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_7);
//writer.CompressionLevel = PdfStream.NO_COMPRESSION;
doc.Open();
image.SetAbsolutePosition(0, 0);
doc.SetPageSize(new iTextSharp.text.Rectangle(0, 0, image.Width, image.Height, 0));
doc.NewPage();
writer.DirectContent.AddImage(image, false);
doc.Close();
}
}
}
}
}
}