How to merge PDF files using dotnet

Sometimes one needs to merge multiple PDF-files to one PDF file, e.g. to send it by email to your customer.
This is how it could be done using dotnet and the PdfSharp.dll as an add-in (get it here)

Two functions, MergePDFFiles and a local function AddPages
function MergePDFFiles(PDFFile1 : Text;PDFFile2 : Text;NewPDFFile : Text)
PDFDocOut := PDFDocOut.PdfDocument;

AddPages( PDFFile1, PDFDocOut);
AddPages( PDFFile2, PDFDocOut);

IF EXISTS( NewPDFFile) THEN 
  ERASE( NewPDFFile);

PDFDocOut.Save( NewPDFFile);
PDFDocOut.Close;
PDFDocOut.Dispose;

with local vars declared as:
PDFDocOut	DotNet	PdfSharp.Pdf.PdfDocument.'PdfSharp, Version=1.32.2608.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb'

and the local function AddPages looks like:
LOCAL function  AddPages(PDFFileName : Text;VAR DestPDF : DotNet "PdfSharp.Pdf.PdfDocument")
FromPDF := PDFReader.Open( PDFFileName, PDFOpenDocMode.Import);
FOR i := 0 TO FromPDF.PageCount-1 DO  
  DestPDF.AddPage( FromPDF.Pages.Item(i));
FromPDF.Close;
FromPDF.Dispose;

with local vars:
FromPDF	DotNet	PdfSharp.Pdf.PdfDocument.'PdfSharp, Version=1.32.2608.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb'	
PDFReader	DotNet	PdfSharp.Pdf.IO.PdfReader.'PdfSharp, Version=1.32.2608.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb'	
PDFOpenDocMode	DotNet	PdfSharp.Pdf.IO.PdfDocumentOpenMode.'PdfSharp, Version=1.32.2608.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb'	
i	Integer

Comments

  • Remco_ReinkingRemco_Reinking Member Posts: 74
    Just saw this one (file-access-error-from-dotnet-assembly), almost showing the same functionality as a sample. The function to merge two PDF's works fine if running from a RTclient, but when called through a webservice it gives the file access error. So far I did not find a solution for this, who did?
  • vaprogvaprog Member Posts: 1,116
    If I remember correctly, the web service uses the service user's credentials and profile to access files, not the credentials used to access the web service.
  • DolshaDolsha Member Posts: 41
    edited 2017-04-07
    @Remco_Reinking what is version? I'll have so many errors with
    PdfSharp.Pdf.PdfDocument.'PdfSharp-WPF, Version=1.32.2608.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb'
    
    In Pdfsharp without WPF didn't see any Type in .NET Type list.

    With my Errors i found pdfsharp 1.50 beta3b, but only source code, mb u have of 1.50beta3b pdfsharp.dll?

    UPD.I'll rebuild source code and i saw Types with pdfsharp without WPF, OMG!
    THIS IS WORK!
  • DolshaDolsha Member Posts: 41
    P.S. version 1.50 beta3b work faster, i'll can link .DLL if someone have trouble with build source code in VS.
  • maxbamaxba Member Posts: 1
    I tried using @Remco_Reinking s Code to merge two PDF Files. I registerd the DLLs i downloaded via the link above. Unfortunately i can't compile the codeunit getting the following error: You have specified an unknown variable. Import. Which should be in:
    PDFOpenDocMode DotNet PdfSharp.Pdf.IO.PdfDocumentOpenMode.'PdfSharp, Version=1.32.2608.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb'

    Can anybody help me or had the same problem?
Sign In or Register to comment.