JPG to PDF Convertor in C#

后端 未结 8 789
鱼传尺愫
鱼传尺愫 2020-12-01 14:07

I would like to convert from an image (like jpg or png) to PDF.

I\'ve checked out ImageMagickNET, but it is far too complex for my needs.

What other .NET so

相关标签:
8条回答
  • 2020-12-01 14:56

    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());
            }
    }
    
    0 讨论(0)
  • 2020-12-01 14:57

    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/

    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
    
    0 讨论(0)
提交回复
热议问题