Print Page | Close Window

Split a document and store it in a stream in Delph

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: General Discussion
Forum Description: Discussion board for Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=1703
Printed Date: 29 Sep 24 at 6:23AM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Split a document and store it in a stream in Delph
Posted By: pmaltais
Subject: Split a document and store it in a stream in Delph
Date Posted: 10 Jan 11 at 9:33PM
Hi,

I need to split a PDF document bot I don't want to generate intermediate files. The resulting documents are stored in a database blob field using a MemoryStream.

Thanks



Replies:
Posted By: Ingo
Date Posted: 10 Jan 11 at 9:39PM
Hi!

Have a look in the online-reference here:
http://www.quickpdflibrary.com/help/quickpdf/FunctionGroups.php

These two chapters should be interest regarding your question:
http://www.quickpdflibrary.com/help/quickpdf/DocumentManipulation.php
http://www.quickpdflibrary.com/help/quickpdf/PageManipulation.php

Cheers, Ingo




Posted By: Paddy
Date Posted: 11 Jan 11 at 9:12AM
If you use the ExtractPageRanges function then you can get the split / extract pages into memory and then save the resulting new document to your database using the SteamToStream function if you're using Delphi or the SaveToVariant function if you're using the ActiveX edition or the SaveToString function if you're using the DLL edition. That should do the trick.


Posted By: pmaltais
Date Posted: 11 Jan 11 at 2:08PM
Ok I get it.

When you extract pages from document A. It creates document B. Do pages extracted from document A are deleted from that document or just copied to the new one (B)?

Thanks


Posted By: pmaltais
Date Posted: 11 Jan 11 at 5:11PM
Hi!

Here's a function I call to SPLIT a document conained in a TMemoryStream property of a custom maid object. The object is to be split at page iFromPage.

It works well as long as there's more than 2 pages in the document. Otherwise, the resulting splitted 2 documents only hava a blank page in it.

Any idea on what's wrong with my code? Is there a better solution?

Thanks

function  TDocumentService.SplitPDFDocument(oDocument : TDocument; iFromPage : integer) : TDocument;
var
  iStatus: Integer;
  myPDFLibCtrl: TQuickPDF0722;
  iSubDocumentID: Integer;
  oNewDocument: TDocument;
begin
  // Init.
  myPDFLibCtrl := nil;
  oNewDocument := nil; // Created by the  CopyToNewDocument function
  iStatus := 0;

  if Assigned(oDocument) then
  begin
    try
      // Load Library
      myPDFLibCtrl := TQuickPDF0722.Create();
      iStatus := myPDFLibCtrl.UnlockKey(LIC_QUICK_PDF_LIB);
      if (iStatus <> 0) then
      begin
        // Load document stream from custom document object into control
        // memStream is a property of type TMemoryStream
        oDocument.memStream.Seek(0, soFromBeginning);
        while not (oDocument.fileStream.Position = 0) do;
        myPDFLibCtrl.LoadFromStream(oDocument.memStream);

        // Save second part to new document
        iSubDocumentID := myPDFLibCtrl.ExtractPages(iFromPage, myPDFLibCtrl.PageCount);
        myPDFLibCtrl.SelectDocument(iSubDocumentID);
        oNewDocument := CopyToNewDocument(oDocument);
        myPDFLibCtrl.SaveToStream(oNewDocument.memStream);
        oNewDocument.numberOfPages := myPDFLibCtrl.PageCount;

        // Reload document object to process first part
        oDocument.fileStream.Seek(0, soFromBeginning);
        while not (oDocument.memStream.Position = 0) do;
        myPDFLibCtrl.LoadFromStream(oDocument.memStream);

        // Save pages from first part back into original document
        iSubDocumentID := myPDFLibCtrl.ExtractPages(1, iFromPage - 1);
        myPDFLibCtrl.SelectDocument(iSubDocumentID);
        myPDFLibCtrl.SaveToStream(oDocument.memStream);
        oDocument.numberOfPages := myPDFLibCtrl.PageCount;

      end
      else
      begin
        ShowMessage('Quick PDF Library: Invalid licence.');
      end;
    except
      on E: Exception do ShowMessage(E.Message);
    end;

    // Cleanup
    FreeAndNil(myPDFLibCtrl);
  end;
  Result := oNewDocument;
end;




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