Print Page | Close Window

Using MergeFiles in Delphi

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: I need help - I can help
Forum Description: Problems and solutions while programming with the Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=2453
Printed Date: 29 Mar 24 at 6:01AM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Using MergeFiles in Delphi
Posted By: TomInJax
Subject: Using MergeFiles in Delphi
Date Posted: 14 Nov 12 at 8:53PM
I hope that this is an easy one to help me with, but I seem to not be using the MergeFiles function properly.  The error that is reported is "Could not open output file".  Embarrassed

What I wish to do, is to merge several PDF documents into a large PDF - but not remove or alter the original documents.  I pass the two names of the original documents in, and pass the full path including filename of the document that I would like to create as the result.

Can someone post a sample of this?  I don't need any bookmarks, annotations, etc.  Just the plain PDF documents merged.

Thanks in advance!
Tom




Replies:
Posted By: Ingo
Date Posted: 14 Nov 12 at 10:04PM
Hi Tom!

"...Could not open output file..."
This sounds like some probs in your code structure
about what comes first and what comes last.
But we can't look inside the glass ball ...
perhaps it's better to post your relevant code parts here?

Cheers and welcome here,
Ingo

BTW: For merge there's a sample on the official support pages.



Posted By: TomInJax
Date Posted: 14 Nov 12 at 11:58PM
I found the problem.  It seems that if the path as spaces in it, then there is a problem with saving the document.  I am now using a simple path with no spaces and everything is working.

Tom


Posted By: AndrewC
Date Posted: 15 Nov 12 at 10:17AM
The library fully support spaces in file names so it could be something else ? 

Can you post a small code sample and tell us which compiler / tool you are using ?

Andrew.


Posted By: TomInJax
Date Posted: 15 Nov 12 at 12:15PM
Thanks Andrew.  Here is the actual test code that I am using.  I added a tab to the demo project for this.

I am using version 8.15 (Source Code License), Delphi XE, on Windows XP.

The functionality is this:

1. "Add" button adds a file name to the TListbox (lstDocs), which also has an object with more properties (full path including filename).
2. A temporary file folder and file name is selected in a Filename Edit control (edtTmpFile.Text).  This will be the merged document.
3. Save button is the event sample code.  It reads the full path and passes it into the MergeFiles function, along with the path for the merged document.  It iterates through the list and concatenates the documents together.

The GetErrorMessage function decodes the error number, translating it to the error text.

Here is the code:

procedure TfrmDemo.btnSaveDocClick(Sender: TObject);
var
  x: integer;
  QP: TQuickPDF;
  tempFileName: string;
begin
  if (lstDocs.Items.Count > 0) then
  begin
    QP := TQuickPDF.Create;

    // Concatenate Documents to one big PDF document
    try
      tempFileName := TDocumentProps(lstDocs.Items.Objects[0]).FullPath;
      for x := 1 to lstDocs.Items.Count - 1 do
      begin
        if QP.MergeFiles(tempFileName, TDocumentProps(lstDocs.Items.Objects[x]).FullPath, edtTmpFile.Text) = 0 then
          ShowMessage(GetErrorMessage(QP.LastErrorCode));

        tempFileName := edtTmpFile.Text;
      end;
    finally
      QP.Free;
    end;
  end;
end;

Tom




Posted By: TomInJax
Date Posted: 15 Nov 12 at 1:03PM
I found the problem.  The filename edit component that I am using is adding double quotes to the beginning and end of the filename. 

Sorry!  D'Oh!!! Embarrassed

Tom


Posted By: TomInJax
Date Posted: 15 Nov 12 at 1:26PM
Ok, there must be a simpler method to concatenate several PDF documents than the way I am doing it.  This code creates a result document with a number added to the filename.  This seems to be necessary since the MergeFiles function cannot merge into the same document it is concatenating to.  The oldest temporary file is removed, then the final document is renamed to the document that was selected by the user in the FilenameEdit control.

This test code is limited to 10 documents, otherwise the code breaks.

Code:
procedure TfrmDemo.btnSaveDocClick(Sender: TObject);
var
  x: integer;
  QP: TQuickPDF;
  tempFileName: string;
begin
  if (lstDocs.Items.Count > 0) then
  begin
    QP := TQuickPDF.Create;
    tempFileName := TDocumentProps(lstDocs.Items.Objects[0]).FullPath;
    try
      for x := 1 to lstDocs.Items.Count - 1 do
      begin

      // Concatenate Documents to one big PDF document
        begin
          if QP.MergeFiles(tempFileName, TDocumentProps(lstDocs.Items.Objects[x]).FullPath, Copy(edtTmpFile.Text,1,POS('.PDF',ToUpper(edtTmpFile.Text))-1)+IntToStr(x)+'.PDF') = 0 then
            ShowMessage(GetErrorMessage(QP.LastErrorCode));

          tempFileName := Copy(edtTmpFile.Text,1,POS('.PDF',ToUpper(edtTmpFile.Text))-1)+IntToStr(x)+'.PDF';

          //  Delete old file
          if FileExists(Copy(edtTmpFile.Text,1,POS('.PDF',ToUpper(edtTmpFile.Text))-1)+IntToStr(x-1)+'.PDF') then
            DeleteFile(Copy(edtTmpFile.Text,1,POS('.PDF',ToUpper(edtTmpFile.Text))-1)+IntToStr(x-1)+'.PDF');
        end;
      end;
    finally
      QP.Free;
      RenameFile(Copy(edtTmpFile.Text,1,POS('.PDF',ToUpper(edtTmpFile.Text))-1)+IntToStr(x-1)+'.PDF',edtTmpFile.Text);
    end;
  end;
end;



Posted By: Wheeley
Date Posted: 15 Nov 12 at 11:26PM
I would suggest using MergeFilesFast. You still have the can't merge into the same file problem, but you can use file lists and merge everything in one command. Just make sure the files are not encrypted before merging. Search the forums for merge. I gave some pseudo code before on this method.

Wheeley


Posted By: TomInJax
Date Posted: 16 Nov 12 at 1:33AM
Thanks Wheely,

That code looks much easier and simpler.  I will try in in my test app tomorrow. 

Now I will know a couple ways to merge a PDF document together! Big smile  (Yes I read the documentation, but example code is always much easier to understand)

I appreciate you, Ingo, and AndrewC taking time to assist me so quickly.  If any of you ever come to North Florida, I will be happy to buy a glass of your favorite drink for you.

Cheers,
Tom


Posted By: lcap
Date Posted: 14 Dec 13 at 1:04PM
Sorry, I was testing the code, but does not identify "TDocumentProps", which unit or library I have to import. In the unit have now uses QuickPDF .... and recognized all other commands or less "TDocumentProps".

Can you help me?


Posted By: tfrost
Date Posted: 15 Dec 13 at 11:30AM
TDocumentprops looks like a class in the OP's own program.  The parameters for the Debenu MergeFiles function (look them up) are simple strings.


Posted By: TomInJax
Date Posted: 15 Dec 13 at 7:57PM
Originally posted by lcap lcap wrote:

Sorry, I was testing the code, but does not identify "TDocumentProps", which unit or library I have to import. In the unit have now uses QuickPDF .... and recognized all other commands or less "TDocumentProps".

Can you help me?

As TFrost said, TDocumentProps is an object that I create that is used in each item with additional properties.  In this case:

TDocumentProps(lstDocs.Items.Objects[x]).FullPath
translates to the full filepath to the file.  It contains something like this: c:\documents\doc3.pdf. 
Sorry for not simplifying my code at the time to make a better example.

Cheers,
Tom


Posted By: lcap
Date Posted: 08 Feb 14 at 6:13PM
I can unify one imagems file "teste.jpg" + * file. pdf "test.pdf" and turn into a big pdf file "1_teste.pdf" unified with debenu?


Posted By: Ingo
Date Posted: 11 Feb 14 at 7:46AM
Hi Icap!
 
Yes! You can ;-)
But first you have to convert the image into pdf (this you can do with QP, too).
Then you can merge both pdf to one new big pdf.
Have a look at the merge-functions here:
http://www.debenu.com/docs/pdf_library_reference/DocumentManipulation.php" rel="nofollow - http://www.debenu.com/docs/pdf_library_reference/DocumentManipulation.php
 
Cheers and welcome here,
Ingo
 


-------------
Cheers,
Ingo




Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk