How to merge pdf files for open Invoices in one single pdf file?

Hi Friends,

[Version: NAV 2009]

I have an another issue where I got struct. I need to merge the Open Invoices in one single pdf file. Below were the code I have done.

Function: SendMailToRegOpenInvoices
Locals:
Parameter
CustNo (Code20)

Variables:
Customer (Record) -	Customer	
File1	(Automation) -	'Microsoft Scripting Runtime'.FileSystemObject	
Filename1- (Text200)
RunOnceFile - (Text250)
TimeOut - (Integer)
SalesInv (Report) - Sales Invoice report
SalesInvoice - (Record) - Sales Invoice Header	
PrinterSelection - (Record) - Printer Selection	
CustLedgEntry - (Record) - Cust. Ledger Entry	
InvoiceNo - (Code20)
mergeAfterFilename - (Text200)

Coding..

SendMailToRegOpenInvoices(CustNo : Code[20])
SMTPSetup.GET;
Customer.GET(CustNo);
CustLedgEntry.SETCURRENTKEY("Customer No.",Open,Positive,"Due Date","Currency Code");
CustLedgEntry.SETRANGE(CustLedgEntry."Customer No.",CustNo);
CustLedgEntry.SETRANGE(Open,TRUE);
CustLedgEntry.SETRANGE(CustLedgEntry."Document Type",CustLedgEntry."Document Type"::Invoice);

IF CustLedgEntry.FINDFIRST THEN BEGIN
  REPEAT
    InvoiceNo := CustLedgEntry."Document No.";

    SalesInvoice.SETFILTER(SalesInvoice."No.",InvoiceNo);
    IF SalesInvoice.FINDFIRST THEN BEGIN

      IF ISCLEAR(File1) THEN
        CREATE(File1);

      IF ISCLEAR(BullZipPDF) THEN
      CREATE(BullZipPDF);

      IF NOT File1.FolderExists(SMTPSetup."Path to Save Inv. Report") THEN
        File1.CreateFolder(SMTPSetup."Path to Save Inv. Report");

      Filename1:= SMTPSetup."Path to Save Inv. Report" +'SalesInvoice' + '.pdf';
      mergeAfterFilename := SMTPSetup."Path to Save Inv. Report" +'SalesInvoiceA' + '.pdf';

      IF NOT PrinterSelection.GET('',50003) THEN BEGIN
        PrinterSelection.INIT;
        PrinterSelection."Report ID" := 50003;
        PrinterSelection."Printer Name" := 'Bullzip PDF Printer';
        PrinterSelection.INSERT;
      END;

      BullZipPDF.Init;
      BullZipPDF.LoadSettings;
      RunOnceFile := BullZipPDF.GetSettingsFileName(TRUE);
      BullZipPDF.SetValue('Output',Filename1);
      BullZipPDF.SetValue('OwnerPassword','abc');
      BullZipPDF.SetValue('UserPassword', CustNo);
      BullZipPDF.SetValue('EncryptionType', 'Standard128bit');
      BullZipPDF.SetValue('ShowSaveAs', 'never');
      BullZipPDF.SetValue('Showsettings', 'never');
      BullZipPDF.SetValue('ShowPDF', 'no');
      BullZipPDF.SetValue('ShowProgress', 'no');
      BullZipPDF.SetValue('ShowProgressFinished', 'no');
      BullZipPDF.SetValue('SuppressErrors', 'yes');
      BullZipPDF.SetValue('ConfirmOverwrite', 'no');

      BullZipPDF.SetValue('Mergefile', Filename1 + '|.|' + mergeAfterFileName);
      BullZipPDF.SetValue('MergePosition', 'below');
      
      BullZipPDF.WriteSettings(TRUE);

      SalesInv.SETTABLEVIEW(SalesInvoice);
      SalesInv.USEREQUESTFORM(FALSE);
      SalesInv.RUNMODAL;

      TimeOut := 0;
      WHILE (TimeOut < 20) DO BEGIN
        SLEEP(3000);
        TimeOut += 1;
      END;

      CLEAR(File1);
      CLEAR(BullZipPDF);
    END;
  UNTIL CustLedgEntry.NEXT = 0;
END;

Can anybody let me know where I made mistake?

Thanks in advance, Aarvi.

Comments

  • alana_oalana_o Member Posts: 2
    However, the branch is quite old, but I want to give my opinion on how to overcome such kind of issues. Ofc, the solution is quite reasonable, but instead of using a code it'd be much easier to do so with this tool https://pdfliner.com/ds_11_form it's paid, but you can have a trial period, which is of the long duration, so you'll have enough time to figure out all issues with your files
  • Aravindh_NavisionAravindh_Navision Member Posts: 254
    Thank you Alana. I'll check and get back to you.
  • Tony_NCDTony_NCD Member Posts: 22
    For basic, merging functionalities, I use the free iTextSharp library for this. Below is the entire code, pretty simple to use. Just create it as a DLL and drop it into your add-in folder. You can then reference it as a DotNet variable.

    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;

    namespace iTextPDF
    {
    public static class PDFtools
    {
    public static bool MergePDFs(string file1, string file2, string targetPdf)
    {
    using (FileStream stream = new FileStream(targetPdf, FileMode.Create))
    {
    using (Document document = new Document())
    {
    PdfCopy pdf = new PdfCopy(document, stream);
    try
    {
    document.Open();
    using (PdfReader reader = new PdfReader(file1))
    {
    pdf.AddDocument(reader);
    reader.Close();
    }
    using (PdfReader reader = new PdfReader(file2))
    {
    pdf.AddDocument(reader);
    reader.Close();
    }
    }
    catch (Exception ex)
    {
    Errs.WriteEventLog(ex.Message);

    return false;
    }
    finally
    {
    if (document != null)
    {
    document.Close();
    }
    }
    }
    }

    return true;
    }
    }
    }
Sign In or Register to comment.