ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Merge 3 PDF file + 1 TIF file
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

mazluta

Israel
15 Posts

Posted - Aug 30 2025 :  17:23:29  Show Profile  Reply
i have 3 pdf file + 1 tiff file (it could be 5 pdf and 5 tiff and 5 jpg/png)
i want to merge them all to one PDF all page with size A4 to all pages + stretch or reduce size so all pages will have the same Height and width.
how can i do it?
there is no problem to merge 2 pdf (so no matter the files/pdf count)
i want the result will be one PDF file all with size of A4

Yossi Mazal-Tov

xequte

39135 Posts

Posted - Aug 30 2025 :  21:17:45  Show Profile  Reply
Hi Yossi

Basically you have two options.

1. Importing images into a PDF.

- Open a PDF in the PdfViewer

- Import the other PDF file(s):
http://www.imageen.com/help/TIEPdfViewer.ImportPages.html

- Create new pages for each of your TIFF images
http://www.imageen.com/help/TIEPdfViewer.AddPage.html

- Save your PDF


2. Merge PDF documents

- Open your TIFF file and save as PDF

- Use ImportPagesIntoPDF to merge each of your PDF documents:
http://www.imageen.com/help/ImportPagesIntoPDF.html


Nigel
Xequte Software
www.imageen.com
Go to Top of Page

mazluta

Israel
15 Posts

Posted - Aug 31 2025 :  04:19:17  Show Profile  Reply
yes. but this dont keep the sizes.
i tried :

function  Tdm_ImageEn.AddMultiImageToNewPDF(SrcFileName : String;
                                             PDFFileName   : String;
                                             UseA4Size     : Boolean;
                                             UseColored    : Boolean;
                                             FitToPage     : Boolean;
                                             PdfWidth      : Integer;
                                             PdfHeight     : Integer) : Boolean;
Var
  SrcEnMView : TImageEnMView;
  DstEnMView : TImageEnMView;
  CurPage    : Integer;
  MS         : TMemoryStream;
begin
  Result := False;
  // Delete existing PDF file
  WinApi.Windows.DeleteFile(PWideChar(PDFFileName));
  if not FileExists(SrcFileName) then
    Exit;

  SrcEnMView := TImageEnMView.Create(nil);
  try

    if IsFilePDF(SrcFileName) then
    begin
      SrcEnMView.MIO.LoadFromFilePDF(SrcFileName);
    end
    else
    begin
      SrcEnMView.MIO.LoadFromFileTIFF(SrcFileName);
    end;

    DstEnMView := TImageEnMView.Create(nil);

    Try
      //DstEnMView.MIO.DuplicateCompressionInfo;

      for CurPage := 0 to SrcEnMView.ImageCount - 1 do
      begin
        MS := TMemoryStream.Create;
        Try
          SrcEnMView.SelectImage(CurPage);
          SrcEnMView.MIO.SaveToStreamPDF(MS, True);
          MS.Position := 0;

          DstEnMView.MIO.LoadFromStreamPDF(MS);
          DstEnMView.SelectImage(CurPage);

          if UseA4Size Then
          begin
            DstEnMView.MIO.Params[CurPage].PDF_PaperSize  := iepA4;
          end
          else
          begin
            DstEnMView.MIO.Params[CurPage].PDF_PaperWidth  := PdfWidth;
            DstEnMView.MIO.Params[CurPage].PDF_PaperHeight := PdfHeight;
          end;

          if FitToPage then
            DstEnMView.MIO.Params[CurPage].PDF_ImageOptions := [iepioShrinkOnly, iepioCentered];

          if UseColored then
          begin
            DstEnMView.MIO.Params[CurPage].SamplesPerPixel := 3;
            DstEnMView.MIO.Params[CurPage].BitsPerSample := 8;
            DstEnMView.MIO.Params[CurPage].PDF_Compression := ioPDF_JPEG;
          end
          else
          begin
            DstEnMView.MIO.Params[CurPage].BitsPerSample := 1;
            DstEnMView.MIO.Params[CurPage].SamplesPerPixel := 1;
            DstEnMView.MIO.Params[CurPage].PDF_Compression := ioPDF_G4FAX;
          end;

          DstEnMView.MIO.Params[CurPage].PDF_PageMargin  := Round( 0.25 * 72 );  // 1/4 inch
          DstEnMView.MIO.SaveToFilePDF(PDFFileName);
        Finally
          MS.Free;
        End;
      end;
    Finally
      DstEnMView.Free;
    End;
  finally
    SrcEnMView.Free;
  end;

  Result := FileExists(PDFFileName);

end;

this dont keep the size by width/height as well

Yossi Mazal-Tov
Go to Top of Page

mazluta

Israel
15 Posts

Posted - Aug 31 2025 :  08:55:59  Show Profile  Reply
for each multi page i export to jpg file :

Function Tdm_ImageEn.ExportSpecificPageToJpg(SrcFileName : String;
                                             PageIndex  : Integer;
                                             JpgFileName  : String): Boolean;
var
  ImageEnIO: TImageEnIO;
  Bitmap: TIEBitmap;
begin
  Result := False;
  try
    // Validate input file
    if not FileExists(SrcFileName) then
    begin
      JustWriteToLog('Source file not found: ' + SrcFileName);
      Exit;
    end;

    Bitmap := TIEBitmap.Create;
    ImageEnIO := TImageEnIO.Create(nil);
    try
      ImageEnIO.AttachedIEBitmap := Bitmap;

      // Load first page (0-based indexing)
      ImageEnIO.Params.ImageIndex := PageIndex;
      ImageEnIO.LoadFromFile(SrcFileName);

      // Save as JPG
      ImageEnIO.Params.JPEG_Quality := 90;
      ImageEnIO.SaveToFile(JpgFileName);

      Result := True;
    finally
      ImageEnIO.Free;
      Bitmap.Free;
    end;
  except
    on E: Exception do
    begin
      JustWriteToLog('Error exporting One Page From Multi File to JPG: ' + E.Message);
      Result := False;
    end;
  end;
end;

then : create new PDF file with the property i want.
function  Tdm_ImageEn.AddImageToNewPDF(SrcFileName : String;
                                        PDFFileName   : String;
                                        UseA4Size     : Boolean;
                                        UseColored    : Boolean;
                                        FitToPage     : Boolean;
                                        PdfWidth      : Integer;
                                        PdfHeight     : Integer) : Boolean;
var
  ImageEnView: TImageEnView;
begin
  Result := False;
  // Delete existing PDF file
  WinApi.Windows.DeleteFile(PWideChar(PDFFileName));
  if not FileExists(SrcFileName) then
    Exit;

  ImageEnView := TImageEnView.Create(nil);
  try
    if UseA4Size Then
    begin
      ImageEnView.IO.Params.PDF_PaperSize  := iepA4;
    end
    else
    begin
      ImageEnView.IO.Params.PDF_PaperSize   := iepUnknown;
      ImageEnView.IO.Params.PDF_PaperWidth  := PdfWidth;
      ImageEnView.IO.Params.PDF_PaperHeight := PdfHeight;
    end;

    if FitToPage then
      ImageEnView.IO.Params.PDF_ImageOptions := [iepioShrinkOnly, iepioCentered];

    if UseColored then
    begin
      ImageEnView.IO.Params.SamplesPerPixel := 3;
      ImageEnView.IO.Params.BitsPerSample := 8;
      ImageEnView.IO.Params.PDF_Compression := ioPDF_JPEG;
    end
    else
    begin
      ImageEnView.IO.Params.BitsPerSample := 1;
      ImageEnView.IO.Params.SamplesPerPixel := 1;
      ImageEnView.IO.Params.PDF_Compression := ioPDF_G4FAX;
    end;
    ImageEnView.IO.Params.PDF_PageMargin  := Round( 0.25 * 72 );  // 1/4 inch
    ImageEnView.IO.LoadFromFile(SrcFileName);

    ImageEnView.IO.SaveToFilePDF(PDFFileName);
  finally
    ImageEnView.Free;
  end;

  Result := FileExists(PDFFileName);
end;

function  Tdm_ImageEn.AddMultiImageToNewPDF(SrcFileName : String;
                                             PDFFileName   : String;
                                             UseA4Size     : Boolean;
                                             UseColored    : Boolean;
                                             FitToPage     : Boolean;
                                             PdfWidth      : Integer;
                                             PdfHeight     : Integer) : Boolean;
Var
  SrcEnMView : TImageEnMView;
  DstEnView  : TImageEnView;
  JpgFileName: String;
  TmpFileName: String;
  CurPage    : Integer;
  PageCount  : Integer;
  //MS         : TMemoryStream;
begin
  Result := False;
  // Delete existing PDF file
  WinApi.Windows.DeleteFile(PWideChar(PDFFileName));
  if not FileExists(SrcFileName) then
    Exit;

  SrcEnMView := TImageEnMView.Create(nil);
  try
    PageCount := dm_ImageEn.GetImagePageCount(SrcFileName);
    Try
      JpgFileName := GetJpgTempFileName;
      TmpFileName := GetPdfTempFileName;
      for CurPage := 0 to PageCount - 1 do
      begin
        WinApi.Windows.DeleteFile(PWideChar(JpgFileName));
        WinApi.Windows.DeleteFile(PWideChar(TmpFileName));

        ExportSpecificPageToJpg(SrcFileName,
                                CurPage,
                                JpgFileName);

        AddImageToNewPDF(JpgFileName,
                         TmpFileName,
                         UseA4Size,
                         UseColored,
                         FitToPage,
                         PdfWidth,
                         PdfHeight);

        DstEnView := TImageEnView.Create(nil);
        Try
          // Merge two PDF documents
          DstEnView.PdfViewer.Enabled := True;
          if CurPage > 0 then
            DstEnView.IO.LoadFromFilePDF(PDFFileName);
          DstEnView.PdfViewer.ImportPages(TmpFileName);
          DstEnView.IO.SaveToFilePDF(PDFFileName);
        Finally
          DstEnView.Free;
        End;
      end;
    Finally
    End;
  finally
    SrcEnMView.Free;
  end;

  Result := FileExists(PDFFileName);
end;

this work.
but i dont know if it is the best way...

Yossi Mazal-Tov
Go to Top of Page

xequte

39135 Posts

Posted - Sep 03 2025 :  01:13:01  Show Profile  Reply
Hi Yossi

For your second method, you shouldn't need to use an intermediary file at all, e.g. (untested):

function  Tdm_ImageEn.AddImageToNewPDF(SrcFileName : String;
                                       PageIndex  : Integer;
                                        PDFFileName   : String;
                                        UseA4Size     : Boolean;
                                        UseColored    : Boolean;
                                        FitToPage     : Boolean;
                                        PdfWidth      : Integer;
                                        PdfHeight     : Integer) : Boolean;
var
  ImageEnView: TImageEnView;
begin
  Result := False;
  // Delete existing PDF file
  WinApi.Windows.DeleteFile(PWideChar(PDFFileName));
  if not FileExists(SrcFileName) then
    Exit;

  ImageEnView := TImageEnView.Create(nil);
  try
    if UseA4Size Then
    begin
      ImageEnView.IO.Params.PDF_PaperSize  := iepA4;
    end
    else
    begin
      ImageEnView.IO.Params.PDF_PaperSize   := iepUnknown;
      ImageEnView.IO.Params.PDF_PaperWidth  := PdfWidth;
      ImageEnView.IO.Params.PDF_PaperHeight := PdfHeight;
    end;

    if FitToPage then
      ImageEnView.IO.Params.PDF_ImageOptions := [iepioShrinkOnly, iepioCentered];

    if UseColored then
    begin
      ImageEnView.IO.Params.SamplesPerPixel := 3;
      ImageEnView.IO.Params.BitsPerSample := 8;
      ImageEnView.IO.Params.PDF_Compression := ioPDF_JPEG;
    end
    else
    begin
      ImageEnView.IO.Params.BitsPerSample := 1;
      ImageEnView.IO.Params.SamplesPerPixel := 1;
      ImageEnView.IO.Params.PDF_Compression := ioPDF_G4FAX;
    end;
    ImageEnView.IO.Params.PDF_PageMargin  := Round( 0.25 * 72 );  // 1/4 inch
 
    ImageEnView.IO.Params.ImageIndex := PageIndex;
    ImageEnView.IO.LoadFromFile(SrcFileName);

    ImageEnView.IO.SaveToFilePDF(PDFFileName);
  finally
    ImageEnView.Free;
  end;

  Result := FileExists(PDFFileName);
end;

function  Tdm_ImageEn.AddMultiImageToNewPDF(SrcFileName : String;
                                             PDFFileName   : String;
                                             UseA4Size     : Boolean;
                                             UseColored    : Boolean;
                                             FitToPage     : Boolean;
                                             PdfWidth      : Integer;
                                             PdfHeight     : Integer) : Boolean;
Var
  SrcEnMView : TImageEnMView;
  DstEnView  : TImageEnView;
  TmpFileName: String;
  CurPage    : Integer;
  PageCount  : Integer;
  //MS         : TMemoryStream;
begin
  Result := False;
  // Delete existing PDF file
  WinApi.Windows.DeleteFile(PWideChar(PDFFileName));
  if not FileExists(SrcFileName) then
    Exit;

  SrcEnMView := TImageEnMView.Create(nil);
  try
    PageCount := dm_ImageEn.GetImagePageCount(SrcFileName);
    Try
      TmpFileName := GetPdfTempFileName;
      for CurPage := 0 to PageCount - 1 do
      begin
        WinApi.Windows.DeleteFile(PWideChar(TmpFileName));

        AddImageToNewPDF(SrcFileName,
                         CurPage,
                         TmpFileName,
                         UseA4Size,
                         UseColored,
                         FitToPage,
                         PdfWidth,
                         PdfHeight);

        DstEnView := TImageEnView.Create(nil);
        Try
          // Merge two PDF documents
          DstEnView.PdfViewer.Enabled := True;
          if CurPage > 0 then
            DstEnView.IO.LoadFromFilePDF(PDFFileName);
          DstEnView.PdfViewer.ImportPages(TmpFileName);
          DstEnView.IO.SaveToFilePDF(PDFFileName);
        Finally
          DstEnView.Free;
        End;
      end;
    Finally
    End;
  finally
    SrcEnMView.Free;
  end;

  Result := FileExists(PDFFileName);
end;


Performance of that should be OK.


Nigel
Xequte Software
www.imageen.com
Go to Top of Page

xequte

39135 Posts

Posted - Sep 03 2025 :  01:21:18  Show Profile  Reply
The first one, looks better to me, in terms of performance and cleanness. The code does have some technical issues, it should be something like this (untested):

function  Tdm_ImageEn.AddMultiImageToNewPDF(SrcFileName : String;
                               PDFFileName   : String;
                               UseA4Size     : Boolean;
                               UseColored    : Boolean;
                               FitToPage     : Boolean;
                               PdfWidth    : Integer;
                               PdfHeight     : Integer) : Boolean;
Var
  MView : TImageEnMView;
  CurPage : Integer;
begin
  Result := False;
  // Delete existing PDF file
  WinApi.Windows.DeleteFile(PWideChar(PDFFileName));
  if not FileExists(SrcFileName) then
    Exit;

  MView := TImageEnMView.Create(nil);
  try

    if IsFilePDF(SrcFileName) then
      MView.MIO.LoadFromFilePDF(SrcFileName)
    else
      MView.MIO.LoadFromFileTIFF(SrcFileName);

    for CurPage := 0 to MView.ImageCount - 1 do
    begin
        MView.MIO.LoadFromStreamPDF(MS);
        MView.SelectImage(CurPage);

        if UseA4Size Then
        begin
          MView.MIO.Params[CurPage].PDF_PaperSize  := iepA4;
        end
        else
        begin
          MView.MIO.Params[CurPage].PDF_PaperWidth  := PdfWidth;
          MView.MIO.Params[CurPage].PDF_PaperHeight := PdfHeight;
        end;

        if FitToPage then
          MView.MIO.Params[CurPage].PDF_ImageOptions := [iepioShrinkOnly, iepioCentered];

        if UseColored then
        begin
          MView.MIO.Params[CurPage].SamplesPerPixel := 3;
          MView.MIO.Params[CurPage].BitsPerSample := 8;
          MView.MIO.Params[CurPage].PDF_Compression := ioPDF_JPEG;
        end
        else
        begin
          MView.MIO.Params[CurPage].BitsPerSample := 1;
          MView.MIO.Params[CurPage].SamplesPerPixel := 1;
          MView.MIO.Params[CurPage].PDF_Compression := ioPDF_G4FAX;
        end;

        MView.MIO.Params[CurPage].PDF_PageMargin  := Round( 0.25 * 72 );  // 1/4 inch
    end;

    MView.MIO.SaveToFilePDF(PDFFileName);

  finally
    MView.Free;
  end;

  Result := FileExists(PDFFileName);
end;





Nigel
Xequte Software
www.imageen.com
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: