答案 0 :(得分:28)
轻松iTextSharp:
class Program
{
static void Main(string[] args)
{
Document document = new Document();
using (var stream = new FileStream("test.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfWriter.GetInstance(document, stream);
document.Open();
using (var imageStream = new FileStream("test.jpg", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var image = Image.GetInstance(imageStream);
document.Add(image);
}
document.Close();
}
}
}
答案 1 :(得分:16)
iTextSharp干净利落,是开源的。此外,如果你最终做了更有趣的事情,比如管理表单,我建议a very good accompanying book by the author。对于正常使用,邮件列表和新闻组中有大量资源可用于处理常见事项的示例。
编辑:正如@Chirag's comment中提到的那样,@Darin's answer的代码肯定会与当前版本编译。
示例用法:
public static void ImagesToPdf(string[] imagepaths, string pdfpath)
{
using(var doc = new iTextSharp.text.Document())
{
iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new FileStream(pdfpath, FileMode.Create));
doc.Open();
foreach (var item in imagepaths)
{
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(item);
doc.Add(image);
}
}
}
击> <击> 撞击>
答案 2 :(得分:4)
我们运气好的是PDFSharp(我们每天用它来进行TIFF和文本到PDF的转换,数百种医疗索赔)。
答案 3 :(得分:2)
这项任务可以在Docotic.Pdf library的帮助下轻松完成。
以下是从给定图像(实际上不仅仅是JPG)创建PDF的示例:
public static void imagesToPdf(string[] images, string pdfName)
{
using (PdfDocument pdf = new PdfDocument())
{
for (int i = 0; i < images.Length; i++)
{
if (i > 0)
pdf.AddPage();
PdfPage page = pdf.Pages[i];
string imagePath = images[i];
PdfImage pdfImage = pdf.AddImage(imagePath);
page.Width = pdfImage.Width;
page.Height = pdfImage.Height;
page.Canvas.DrawImage(pdfImage, 0, 0);
}
pdf.Save(pdfName);
}
}
免责声明:我为图书馆的供应商工作。
答案 4 :(得分:2)
另一个有效的代码,试试吧
public void ImagesToPdf(string[] imagepaths, string pdfpath)
{
iTextSharp.text.Rectangle pageSize = null;
using (var srcImage = new Bitmap(imagepaths[0].ToString()))
{
pageSize = new iTextSharp.text.Rectangle(0, 0, srcImage.Width, srcImage.Height);
}
using (var ms = new MemoryStream())
{
var document = new iTextSharp.text.Document(pageSize, 0, 0, 0, 0);
iTextSharp.text.pdf.PdfWriter.GetInstance(document, ms).SetFullCompression();
document.Open();
var image = iTextSharp.text.Image.GetInstance(imagepaths[0].ToString());
document.Add(image);
document.Close();
File.WriteAllBytes(pdfpath+"cheque.pdf", ms.ToArray());
}
}
答案 5 :(得分:1)
您需要安装Acrobat。在Acrobat DC上测试。这是一个VB.net代码。由于这些对象是COM对象,因此您应该执行“释放对象”,而不仅仅是“ = Nothing”。您可以在此处转换此代码:https://converter.telerik.com/
Private Function ImageToPDF(ByVal FilePath As String, ByVal DestinationFolder As String) As String
Const PDSaveCollectGarbage As Integer = 32
Const PDSaveLinearized As Integer = 4
Const PDSaveFull As Integer = 1
Dim PDFAVDoc As Object = Nothing
Dim PDFDoc As Object = Nothing
Try
'Check destination requirements
If Not DestinationFolder.EndsWith("\") Then DestinationFolder += "\"
If Not System.IO.Directory.Exists(DestinationFolder) Then Throw New Exception("Destination directory does not exist: " & DestinationFolder)
Dim CreatedFile As String = DestinationFolder & System.IO.Path.GetFileNameWithoutExtension(FilePath) & ".pdf"
'Avoid conflicts, therefore previous file there will be deleted
If File.Exists(CreatedFile) Then File.Delete(CreatedFile)
'Get PDF document
PDFAVDoc = GetPDFAVDoc(FilePath)
PDFDoc = PDFAVDoc.GetPDDoc
If Not PDFDoc.Save(PDSaveCollectGarbage Or PDSaveLinearized Or PDSaveFull, CreatedFile) Then Throw New Exception("PDF file cannot be saved: " & PDFDoc.GetFileName())
If Not PDFDoc.Close() Then Throw New Exception("PDF file could not be closed: " & PDFDoc.GetFileName())
PDFAVDoc.Close(1)
Return CreatedFile
Catch Ex As Exception
Throw Ex
Finally
System.Runtime.InteropServices.Marshal.ReleaseComObject(PDFDoc)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(PDFDoc)
PDFDoc = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(PDFAVDoc)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(PDFAVDoc)
PDFAVDoc = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
End Try
End Function
答案 6 :(得分:0)
不确定您是在寻找免费/开源解决方案还是考虑商业解决方案。但是,如果您要包含商业解决方案,那么有一个名为EasyPDF SDK的工具包,它提供了一个API,用于将图像(以及许多其他文件类型)转换为PDF。它支持C#,可以在这里找到:
http://www.pdfonline.com/
C#代码如下所示:
Printer oPrinter = new Printer();
ImagePrintJob oPrintJob = oPrinter.ImagePrintJob;
oPrintJob.PrintOut(imageFile, pdfFile);
为了完全透明,我应该声称我为EasyPDF SDK的制造商工作(因此我的手柄),所以这个建议并非没有个人偏见:)但如果你',请随时查看eval版本感兴趣。干杯!
答案 7 :(得分:-4)
那里有很多差异工具。我使用的是PrimoPDF(免费)http://www.primopdf.com/您打印文件并将其打印为pdf格式到您的驱动器上。适用于Windows