Results 1 to 6 of 6

Thread: Problems with creating pdf documents via VB.net (Results in pages of differnt sizes)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    10

    Problems with creating pdf documents via VB.net (Results in pages of differnt sizes)

    Hello,

    I have the following problem. - Current situation: I am creating with VB.net via a WORD Add-IN a pdf from a word document using the following code and move the result into a MERGE folder:

    Code:
    'step 1
     'Export into PDF (For printing via print provider) -> First into MERGE Folder
    doc.ExportAsFixedFormat(Filepath_tmp & "Merge\" & doc_name, Word.WdExportFormat.wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:=WdExportOptimizeFor.wdExportOptimizeForPrint)
    After that I select all documents in that MERGE folder (all PDFs) and try to merge all documents into 1 PDF:

    Code:
    'step 2
    func.PDFMERGE(Filepath_tmp & "Merge", Filepath_tmp, doc_name) 'Merges the documents end copies the result to the temp folder
    UIsing the following function:

    Code:
    Public Sub PDFMERGE(Inp_Dir As String, Out_Dir As String, Out_Filename As String)
    
            Try
                'Define variables
                Dim pdffiles(100) As String
                Dim i As Integer
                Dim x As Integer
    
                'Get some file names - Get all pdfs in one folder in an array
                For Each items As String In System.IO.Directory.GetFiles(Inp_Dir)
                    If System.IO.Path.GetExtension(items) = ".pdf" Then
                        pdffiles(i) = System.IO.Path.GetFullPath(items)
                        i = i + 1
                    End If
                Next
    
    
                'Open the output document
                Dim outputDocument As New PdfDocument()
    
                'Iterate files in Inpufolder
                Dim idx As Integer
                For x = 0 To i - 1 Step 1
    
    
                    'Open the pdf document to import pages from it.
                    Dim inputDocument As New PdfDocument
                    inputDocument = PdfReader.Open(pdffiles(x), PdfDocumentOpenMode.Import)
    
                    'Iterate pages in pdf document
                    Dim count As Integer = inputDocument.PageCount
    
                    For idx = 0 To count - 1 Step 1
    
                        'Get the page from the external document...
                        Dim page As New PdfPage
                        page = inputDocument.Pages(idx)
                        'And add it to the output document.
                        outputDocument.AddPage(page)
    
                    Next
                Next
                'Define full output path incl. filename
                Dim filename As String = Out_Dir & Out_Filename
    
                'Check if target file exists with same name in output directory - Delete 
                If System.IO.File.Exists(System.IO.Path.Combine(Out_Dir, Out_Filename)) Then
                    System.IO.File.Delete(Out_Dir & Out_Filename)
                End If
    
                'Save the document to the output directory 
                System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance)
                outputDocument.Save(filename)
    
    
            Catch ex As Exception
                MsgBox("Das Anfügen der Anhänge hat nicht geklappt. Bitte prüfen Sie das Ergebnis.")
            End Try
    
        End Sub
    This all works perfecly fine - NEARLY.

    The only but urgent problem is that the newly created PDF from WORD has a different resolution (smaller) than the scanned attachements - with the results that the pages in the merged file have different sizes. This leads to the problem that the first page will be printed perfectly fine but all other pages are far too large so that they are cut (not shrinked).

    Does anyone have any idea on how to resolve it?

    Many thanks

    Christian

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,140

    Re: Problems with creating pdf documents via VB.net (Results in pages of differnt siz

    Are you merging text into your Word document, or images?

    Does the merged Word document look and print OK?

    A better idea might be to merge all of your content into Word, so that you have one document formatted correctly, and then export that to a .pdf file if needed.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    10

    Re: Problems with creating pdf documents via VB.net (Results in pages of differnt siz

    Quote Originally Posted by jdc2000 View Post
    Are you merging text into your Word document, or images?

    Does the merged Word document look and print OK?

    A better idea might be to merge all of your content into Word, so that you have one document formatted correctly, and then export that to a .pdf file if needed.
    I am first converting the Word document into a pdf and then merge the 2 pdfs (using pdfsharp).

    How could I merge the content into word first as you describe?

    Christian

  4. #4
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,140

    Re: Problems with creating pdf documents via VB.net (Results in pages of differnt siz

    If you have multiple Word documents already created, just combine those first and then just Save As a .pdf file. That would eliminate the need for pdfSharp and solve your formatting issues. I assume that you are using an up-to-date version of Word (2016 or later). If you are wanting to use VBA to automate the merging of documents, there are methods available to do this.

    Links:

    https://www.msofficeforums.com/word-...cel-macro.html

    https://excel-macro.tutorialhorizon....ents-into-one/

    http://www.vbaexpress.com/forum/show...o-one-word-doc

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    10

    Re: Problems with creating pdf documents via VB.net (Results in pages of differnt siz

    Hello, unfortunately I have at the beginning my WORD document and an existing pdf. This is the reason why I first convert the WORD document to PDF and then have to merge the two pdfs.

    Christian

  6. #6
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,140

    Re: Problems with creating pdf documents via VB.net (Results in pages of differnt siz

    In that case, you will probably need to re-size the converted Word documents when making them into .pdf files.

    Possibly useful links:

    http://www.pdfsharp.net/wiki/PageSiz...ookieSupport=1

    https://stackoverflow.com/questions/...height-of-page

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width