Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的JPG到PDF转换器#_C#_Pdf_Jpeg_Image Conversion - Fatal编程技术网

C# C语言中的JPG到PDF转换器#

C# C语言中的JPG到PDF转换器#,c#,pdf,jpeg,image-conversion,C#,Pdf,Jpeg,Image Conversion,我想将图像(如jpg或png)转换为PDF 我已经退房了,但对我的需要来说太复杂了 还有什么其他的.NET解决方案或代码可以将图像转换为PDF?非常干净,而且是开源的。另外,如果你最终做了更多有趣的事情,比如管理表单,我建议你使用它。对于正常使用,在邮件列表和新闻组中有大量的资源,可以提供如何做普通事情的示例 编辑:正如中所提到的,它的代码肯定是用当前版本编译的 用法示例: public static void ImagesToPdf(string[] imagepaths, string pd

我想将图像(如jpg或png)转换为PDF

我已经退房了,但对我的需要来说太复杂了

还有什么其他的.NET解决方案或代码可以将图像转换为PDF?

非常干净,而且是开源的。另外,如果你最终做了更多有趣的事情,比如管理表单,我建议你使用它。对于正常使用,在邮件列表和新闻组中有大量的资源,可以提供如何做普通事情的示例

编辑:正如中所提到的,它的代码肯定是用当前版本编译的

用法示例:

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

轻松使用:


有很多不同的工具。我使用的一个是PrimoPDF(FREE),您可以打印文件,并将其打印为pdf格式到驱动器上。在Windows上工作

我们非常幸运的一个是PDFSharp(我们每天使用它进行TIFF和文本到PDF的转换,用于数百份医疗索赔)


不确定您是在寻找免费/开源解决方案,还是也在考虑商业解决方案。但是,如果您包括商业解决方案,那么有一个名为EasyPDF SDK的工具包,它提供了一个将图像(加上许多其他文件类型)转换为PDF的API。它支持C#,可以在这里找到:

 http://www.pdfonline.com/
C代码如下所示:

 Printer oPrinter = new Printer();

 ImagePrintJob oPrintJob = oPrinter.ImagePrintJob;
 oPrintJob.PrintOut(imageFile, pdfFile);

为了完全透明,我应该否认我为EasyPDF SDK的制造商工作(因此我的处理方式),所以这个建议并非没有个人偏见:),但如果您感兴趣,请随时查看eval版本。干杯

这样的任务可以在你的帮助下轻松完成

下面是一个从给定图像(实际上不仅仅是JPG)创建PDF的示例:

publicstaticvoidimagestopdf(字符串[]图像,字符串pdfName)
{
使用(PdfDocument pdf=new PdfDocument())
{
对于(int i=0;i0)
pdf.AddPage();
PdfPage page=pdf.Pages[i];
字符串imagePath=images[i];
PdfImage PdfImage=pdf.AddImage(图像路径);
页面宽度=pdfImage.Width;
页面高度=PDF图像高度;
page.Canvas.DrawImage(pdfImage,0,0);
}
pdf.Save(pdfName);
}
}

免责声明:我为库的供应商工作。

另一个工作代码,请尝试

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());
        }
}

您需要安装Acrobat。在Acrobat DC上测试。这是一个VB.net代码。由于这些对象是COM对象,您应该执行“释放对象”,而不仅仅是“=Nothing”。您可以在此处转换此代码:

Private Function ImageToPDF(ByVal FilePath作为字符串,ByVal DestinationFolder作为字符串)作为字符串
Const PDSaveCollectGarbage为整数=32
常数pdsave线性化为整数=4
常量PDSaveFull为整数=1
作为对象的Dim PDFAVDoc=无
作为对象的Dim PDFDoc=无
尝试
'检查目的地要求
如果不是DestinationFolder.EndsWith(“\”),则DestinationFolder+=“\”
如果System.IO.Directory.Exists(DestinationFolder)不存在,则抛出新异常(“目标目录不存在:”&DestinationFolder)
Dim CreatedFile As String=DestinationFolder&System.IO.Path.GetFileNameWithoutExtension(文件路径)&.pdf”
'避免冲突,因此将删除以前的文件
如果File.Exists(CreatedFile),则File.Delete(CreatedFile)
'获取PDF文档
PDFAVDoc=GetPDFAVDoc(文件路径)
PDFDoc=PDFAVDoc.GetPDDoc
如果不是PDFDoc.Save(PDSaveCollectGarbage或PDSaveLinearized或PDSaveFull,CreatedFile),则抛出新异常(“无法保存PDF文件:”&PDFDoc.GetFileName()
如果不是PDFDoc.Close(),则抛出新异常(“无法关闭PDF文件:”&PDFDoc.GetFileName())
PDFAVDoc.关闭(1)
返回CreatedFile
特例
投手
最后
System.Runtime.InteropServices.Marshal.ReleaseComObject(PDFDoc)
System.Runtime.InteropServices.Marshal.FinalEleaseComObject(PDFDoc)
PDFDoc=无
System.Runtime.InteropServices.Marshal.ReleaseComObject(PDFAVDoc)
System.Runtime.InteropServices.Marshal.FinalEleaseComObject(PDFAVDoc)
PDFAVDoc=无
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
结束尝试
端函数

还有一个改进版og ImageMagick(代码方面).我不知道你是否能找到它的.NET库。它是用于桌面/服务器/web应用程序吗?它是一个web应用程序,但这对应用程序的类型没有任何区别,因为编码将是相同的错误答案。他想用编程的方式来做(具体来说是C#).Nice,+1用于整理。有趣的是(我很久没有使用它了),有没有更干净的方法来使用文档w/IDisposable和/或应该有一个try/finally来保护关闭?如果imageStream是实际的资源持有者/所有者,可能没有?我得到了“PdfWriter在当前上下文中不存在”“如何在pdf中添加图像宽度和高度?以下内容似乎可以在不扭曲尺寸的情况下缩小大型图像:float maxWidth=document.PageSize.width-document.LeftMargin-document.rightargin;float maxHeight=document.PageSize.Height-document.TopMargin-document.BottomMargin;如果(image.Height>maxHeight | | image.Width>maxWidth)image.ScaleToFit(maxWidth,maxHeight);iTextSharp是开源的,但不是免费的。由于他们使用的是AGPL许可证,您必须支付许可证费用或使您自己的代码开源。请参见此处:获取错误2'iTextSharp.text.Document':using语句中使用的类型必须隐式转换为'System.IDisposable'@Chirag我不记得当时是否编译了代码,也不记得
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);
    }
}
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());
        }
}