C#中的JPG到PDF转换器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1642280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPG to PDF Convertor in C#
提问by Coppermill
I would like to convert from an image (like jpg or png) to PDF.
我想从图像(如 jpg 或 png)转换为 PDF。
I've checked out ImageMagickNET, but it is far too complex for my needs.
我已经检查了ImageMagickNET,但它对于我的需求来说太复杂了。
What other .NET solutions or code are there for converting an image to a PDF?
还有哪些其他 .NET 解决方案或代码可以将图像转换为 PDF?
采纳答案by Ruben Bartelink
iTextSharpdoes it pretty cleanly and is open source. Also, it has a very good accompanying book by the authorwhich I recommend if you end up doing more interesting things like managing forms. For normal usage, there are plenty resources on mailing lists and newsgroups for samples of how to do common things.
iTextSharp做得非常干净并且是开源的。此外,它还有一本非常好的作者随书,如果你最终做了更有趣的事情,比如管理表单,我推荐它。对于正常使用,邮件列表和新闻组上有大量资源可提供如何做常见事情的示例。
EDIT: as alluded to in @Chirag's comment, @Darin's answerhas code that definitely compiles with current versions.
编辑:正如@Chirag 的评论中所提到的,@Darin 的答案具有绝对可以用当前版本编译的代码。
Example usage:
用法示例:
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);
}
}
}
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);
}
}
}
回答by Darin Dimitrov
Easy with iTextSharp:
使用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();
}
}
}
回答by Harry
Many diff tools out there. One I use is PrimoPDF (FREE) http://www.primopdf.com/you go to print the file and you print it to pdf format onto your drive. works on Windows
那里有许多差异工具。我使用的是 PrimoPDF(免费)http://www.primopdf.com/你去打印文件,然后将它打印为 pdf 格式到你的驱动器上。适用于 Windows
回答by Bob Palmer
One we've had great luck with is PDFSharp (we use it for TIFF and Text to PDF conversion for hundreds of medical claims every day).
我们非常幸运的一个是 PDFSharp(我们每天将它用于 TIFF 和文本到 PDF 的转换,以处理数百个医疗索赔)。
回答by yu-chen-pdfonline-com
not sure if you're looking for just free / open source solutions or considering commercial ones as well. But if you're including commercial solutions, there's a toolkit called EasyPDF SDK that offers an API for converting images (plus a number of other file types) to PDF. It supports C# and can be found here:
不确定您是在寻找免费/开源解决方案还是也在考虑商业解决方案。但是,如果您包含商业解决方案,则有一个名为 EasyPDF SDK 的工具包,它提供了一个 API,用于将图像(以及许多其他文件类型)转换为 PDF。它支持 C#,可以在这里找到:
http://www.pdfonline.com/
The C# code would look as follows:
C# 代码如下所示:
Printer oPrinter = new Printer();
ImagePrintJob oPrintJob = oPrinter.ImagePrintJob;
oPrintJob.PrintOut(imageFile, pdfFile);
To be fully transparent, I should disclaim that I do work for the makers of EasyPDF SDK (hence my handle), so this suggestion is not without some personal bias :) But feel free to check out the eval version if you're interested. Cheers!
为了完全透明,我应该否认我确实为 EasyPDF SDK 的制造商工作(因此我的处理),所以这个建议并非没有一些个人偏见:) 但如果你有兴趣,请随时查看 eval 版本。干杯!
回答by Bobrovsky
Such task can be easily done with help of Docotic.Pdf library.
借助Docotic.Pdf 库,可以轻松完成此类任务。
Here is a sample that creates PDF from given images (not only JPGs, actually):
这是一个从给定图像(实际上不仅是 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);
}
}
Disclaimer: I work for the vendor of the library.
免责声明:我为图书馆的供应商工作。
回答by Chirag
Another working code, try it
另一个工作代码,试试吧
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());
}
}
回答by AndrewK
You need Acrobat to be installed. Tested on Acrobat DC. This is a VB.net code. Due to that these objects are COM objects, you shall do a 'release object', not just a '=Nothing". You can convert this code here: https://converter.telerik.com/
您需要安装 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