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;
|