locked
Converting pictures in Excel to PDF RRS feed

  • Question

  • Hello,

    Recently I figured how to convert .jpg and .tiff files into PDF. I have to insert them into Excel and then save them to PDF. My problem is that if the picture is too big, it split into 2 or more parts and it looks really bad. Is there any possibility that during conversion to pdf there will be option  like this one in settings when we print something "Fitting all columns on one page" ?

    There is small part of my code to make problem clearer: 

    Sub jpg(scwejscia As String, scwyjscia As String)
    
    Dim ws As Worksheet
    Dim pic As Variant
    Dim exwb As Excel.Workbook
    Set exwb = Workbooks.Add
    Set ws = ActiveSheet
    ws.PageSetup.PaperSize = xlPaperA4
    
    
    ws.Range("A1").Activate
    Set pic = ws.Pictures.Insert(scwejscia)
    'Set picture size.
    With pic.ShapeRange
        .LockAspectRatio = msoFalse
     
        
    End With
    
    ws.ExportAsFixedFormat Type:=xlTypePDF, _
        FileName:=scwyjscia, OpenAfterPublish:=False
                  exwb.Close SaveChanges:=False
                  Application.ScreenUpdating = True
    End Sub
    The base that I'm working on it is MS Outlook.

    Thank you for your help. 

    Best regards,
    Robert

    Thursday, April 9, 2015 2:42 PM

Answers

  • Try...

    With ws.PageSetup
        .PaperSize = xlPaperA4
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    End With
    

    Hope this helps!
    • Edited by Domenic Tamburino Thursday, April 9, 2015 8:25 PM
    • Marked as answer by RobRaj Monday, April 13, 2015 12:09 PM
    Thursday, April 9, 2015 8:24 PM

All replies

  • Try...

    With ws.PageSetup
        .PaperSize = xlPaperA4
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    End With
    

    Hope this helps!
    • Edited by Domenic Tamburino Thursday, April 9, 2015 8:25 PM
    • Marked as answer by RobRaj Monday, April 13, 2015 12:09 PM
    Thursday, April 9, 2015 8:24 PM
  • Here is a solution,http://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Image/Split-tiff-image-and-Convert-to-pdf-document.html

    U can refer this article,and try this PDF library.

    The main steps of the method are:

    One: Split multi-page TIFF file to multiple frames

             Load a multipage Tiff.

             Use Image.GetFrameCount method to get the number of frames of tiff Image.

             Initialize a new instance of Guid to save the dimension from tiff image.

    Using Guid to initialize a new instance of the System.Drawing.Imaging.FrameDimension class.

    Iterate over the Tiff Frame Collection and save them to Image array.

    Two: Draw image to PDF

            Initialize a new instance of Spire.Pdf.PdfDocument.

            Iterate over the image array.

             Use Spire.Pdf.PdfPageBase.Canvas.DrawImage method to draw image into page with the specified coordinates and image size.

    Code as follows:

    
    
    Imports System
    Imports System.Drawing
    Imports System.Drawing.Imaging
    Imports System.IO
    Imports Spire.Pdf
    Imports Spire.Pdf.Graphics
    
    Namespace SplitTiff
        Class Program
            Shared Sub Main(ByVal args() As String)
                Using pdfDocument As New PdfDocument
    
                    Dim tiffImage As Image = Image.FromFile("..\..\demo.tiff")
                    Dim images() As Image = SplitTIFFImage(tiffImage)
    
                    Dim i As Integer
                    For i = 0 To images.Length - 1 Step i + 1
                        Dim pdfImg As PdfImage = PdfImage.FromImage(images(i))
                        Dim page As PdfPageBase = pdfDocument.Pages.Add()
                        Dim width As Single = pdfImg.Width * 0.5F
                        Dim height As Single = pdfImg.Height * 0.5F
                        Dim x As Single = (page.Canvas.ClientSize.Width - width) / 2
                        'set the image of the page 
                        page.Canvas.DrawImage(pdfImg, x, 0, width, height)
                    Next
    
    
                    pdfDocument.SaveToFile("..\..\result.pdf")
                    System.Diagnostics.Process.Start("..\..\result.pdf")
                End Using
            End Sub
            Private Shared Function SplitTIFFImage(ByVal tiffImage As Image)
    
                Dim frameCount As Integer = tiffImage.GetFrameCount(FrameDimension.Page)
                Dim images() As Image = New Image(frameCount) {}
                Dim objGuid As Guid = tiffImage.FrameDimensionsList(0)
                Dim objDimension As FrameDimension = New FrameDimension(objGuid)
                Dim i As Integer
                For i = 0 To frameCount - 1 Step i + 1
                    tiffImage.SelectActiveFrame(objDimension, i) 
    
                    Using ms As New MemoryStream
    
                        tiffImage.Save(ms, ImageFormat.Tiff)
                        images(i) = Image.FromStream(ms)
                    End Using
    
                Next
                Return images
            End Function
        End Class
    End Namespace


    Friday, April 10, 2015 2:03 AM