PDF-Datei speichern mit PDFIUM.DLL

Für allgemeine Fragen zur Programmierung mit PureBasic.
fabulouspaul
Beiträge: 120
Registriert: 01.04.2011 21:59

PDF-Datei speichern mit PDFIUM.DLL

Beitrag von fabulouspaul »

Hallo zusammen,

hat schonmal jemand mit PDFIUM eine PDF-Datei gespeichert?

Irgendwie stehe ich da auf dem Schlauch.
Es gibt 2 Funktionen (SaveAsCopy und SaveWithVersion), aber beide erfordern einen Pointer auf eine Struktur (FPDF_FILEWRITE), die ich nicht kenne.

PDF erstellen/laden/ändern klappt alles prima, nur beim Speichern hapert es. :freak:
pstryk
Beiträge: 4
Registriert: 03.12.2013 20:46

Re: PDF-Datei speichern mit PDFIUM.DLL

Beitrag von pstryk »

Hier ein Beispiel bei dem die Seiten 2 bis 4 in "neue.pdf" gespeichert werden.

Code: Alles auswählen

Prototype protoInitPdfLibrary()
Prototype protoLoadDocument(documentpath.p-ascii, password.p-ascii)
Prototype protoLoadPage(document, pagenumber)
Prototype protoGetMediaBox(page, left, bottom, right, top)
Prototype protoRenderPageDC(hDC, page, startx, starty, width, height, rotation, flags)
Prototype protoRenderPageBitmap(bitmap, page, startx, starty, width, height, rotation, flags)
Prototype protoRenderPage_Close(page)
Prototype protoGetPageCount(document)
Prototype protoCloseDocument(document)
Prototype protoGetRotation(page)
Prototype protoSetRotation(page, rotation)
Prototype protoClosePage(page)
Prototype proto_SetProcessDPIAware()
Prototype protoBitmap_Create(x, y, alpha)
Prototype GetPageSizeByIndex(document, pagenumber, width, height)
Prototype protoGetMetaText(document, tag.p-utf8, buffer, buflen)
Prototype protoGetLastError()
Prototype protoImportPages(dest_doc.i, src_doc.i, pagerange.p-ascii, index.l)
Prototype protoLoadTextPage(textpage)
Prototype protoCloseTextPage(textpage)
Prototype protoCountChars(textpage)
Prototype protoGetText(textpage, istart, iCharCnt, *result)
Prototype protoCreateNewDocument()
Prototype protoGetCharOrigin(textpage, index.i, x, y)
Prototype protoGetFontSize(textpage, index)
Prototype protoGetCharBox(textpage, index, left, right, bottom, top)
Prototype protoGetCharIndexAtPos(text_page, x.d, y.d, xTolerance.d, yTolerance.d)
Prototype protoPage_Delete(document, pagenumber)
Prototype protoPage_GenerateContent(page)
Prototype protoSaveAsCopy(document, fileWrite, flags)
Prototype protoBookmark_Find(document.i, *title.string)
Prototype protoBookmark_GetAction(bookmark.i)
Prototype protoBookmark_GetDest(document.i, bookmark.i)
Prototype protoBookmark_GetFirstChild(document.i, bookmark.i)
Prototype protoBookmark_GetNextSibling(document.i, bookmark.i)
Prototype protoBookmark_GetTitle(bookmark.i, *buffer, buflen.i)
Prototype protoDest_GetPageIndex(document.i, dest.i)
Prototype protoPage_New(document.i, page_index.i, width.d, height.d)
Prototype protoImageObj_SetBitmap(pages, nCount.i, *image_object, *bitmap);, ImageCompression compress )
Prototype protoPage_InsertObject(page, page_obj)
Prototype protoPageObj_NewImgeObj(document)
Prototype protoPageObj_Transform(page_obj, a.d, b.d, c.d, d.d, e.d, f.d)
Prototype protoImageObj_LoadJpegFile(pages, count, imgObject, fileObject)
Prototype protoGetDefaultTTFMap()
Prototype protoAddInstalledFont(mapper.i, *face.p-ascii, charset.l)
Prototype protoText_LoadFont(document, FontType, cid.b, fontData, index.i, Count.i)
Prototype protoPageObj_NewTextObj(document, font.p-ascii, fontsize.f)
Prototype protoPageObj_CreateTextObj(document, ffont.i, fontsize.f)
Prototype protoText_SetFillColor(text_object, R.i, G.i, B.i, A.i);
Prototype protoText_SetText(pageObj, string.s)
Prototype protoGetDefaultSystemFontInfo()
Prototype protoSetSystemFontInfo(pFontInfo.i)
Prototype protoGetLinkAtPoint(page, x.d, y.d)
Prototype protoLink_GetDest(document, link)
Prototype protoDest_GetDestPageIndex(document, destination)
Prototype protoLink_GetAction(link)
Prototype protoAction_GetURIPath(document, action, buf, SizeOf_buf)
Prototype protoDeviceToPage(page, start_x, start_y, size_x, size_y, rotate, device_x, device_y, page_x.d, page_y.d)
Prototype protoTextObj_GetText(obj.i, textpage.i, point, code)

Structure FPDF_FILEACCESS
  FileLen.i
  *GetBlock.protoGetBlock
  param.i
  file$
  file_handle.i
EndStructure
Structure FPDF_FILEWRITE
  version.i
  *WriteBlock.protoWriteBlock
  size.l
EndStructure

Global pdfium = OpenLibrary(#PB_Any, "pdfium.dll")

Global Action_GetURIPath.protoAction_GetURIPath               = GetFunction(pdfium, "_FPDFAction_GetURIPath@16")
Global AddInstalledFont.protoAddInstalledFont                 = GetFunction(pdfium, "_FPDF_AddInstalledFont@12")
Global Bitmap_Create.protoBitmap_Create                       = GetFunction(pdfium, "_FPDFBitmap_Create@12")
Global Bookmark_Find.protoBookmark_Find                       = GetFunction(pdfium, "_FPDFBookmark_Find@8")
Global Bookmark_GetAction.protoBookmark_GetAction             = GetFunction(pdfium, "_FPDFBookmark_GetAction@4")
Global Bookmark_GetDest.protoBookmark_GetDest                 = GetFunction(pdfium, "_FPDFBookmark_GetDest@8")
Global Bookmark_GetFirstChild.protoBookmark_GetFirstChild     = GetFunction(pdfium, "_FPDFBookmark_GetFirstChild@8")
Global Bookmark_GetNextSibling.protoBookmark_GetNextSibling   = GetFunction(pdfium, "_FPDFBookmark_GetNextSibling@8")
Global Bookmark_GetTitle.protoBookmark_GetTitle               = GetFunction(pdfium, "_FPDFBookmark_GetTitle@12")
Global CloseDocument.protoCloseDocument                       = GetFunction(pdfium, "_FPDF_CloseDocument@4")
Global CloseDocument.protoCloseDocument                       = GetFunction(pdfium, "_FPDF_CloseDocument@4")
Global ClosePage.protoClosePage                               = GetFunction(pdfium, "_FPDF_ClosePage@4")
Global ClosePage.protoClosePage                               = GetFunction(pdfium, "_FPDF_ClosePage@4")
Global CloseTextPage.protoClosePage                           = GetFunction(pdfium, "_FPDFText_ClosePage@4")
Global CountChars.protoCountChars                             = GetFunction(pdfium, "_FPDFText_CountChars@4")
Global CreateNewDocument.protoCreateNewDocument               = GetFunction(pdfium, "_FPDF_CreateNewDocument@0")
Global Dest_GetDestPageIndex.protoDest_GetDestPageIndex       = GetFunction(pdfium, "_FPDFDest_GetDestPageIndex@8")
Global GetCharBox.protoGetCharBox                             = GetFunction(pdfium, "_FPDFText_GetCharBox@24")
Global GetCharIndexAtPos.protoGetCharIndexAtPos               = GetFunction(pdfium, "_FPDFText_GetCharIndexAtPos@36")
Global GetCharOrigin.protoGetCharOrigin                       = GetFunction(pdfium, "_FPDFText_GetCharOrigin@16")
Global GetCropBox.protoGetMediaBox                            = GetFunction(pdfium, "_FPDFPage_GetCropBox@20")
Global GetDefaultSystemFontInfo.protoGetDefaultSystemFontInfo = GetFunction(pdfium, "_FPDF_GetDefaultSystemFontInfo@0")
Global GetDefaultTTFMap.protoGetDefaultTTFMap                 = GetFunction(pdfium, "_FPDF_GetDefaultTTFMap@0")
Global GetFontSize.protoGetFontSize                           = GetFunction(pdfium, "_FPDFText_GetFontSize@8")
Global GetLastError.protoGetLastError                         = GetFunction(pdfium, "_FPDF_GetLastError@0")
Global GetLinkAtPoint.protoGetLinkAtPoint                     = GetFunction(pdfium, "_FPDFLink_GetLinkAtPoint@20")
Global GetMediaBox.protoGetMediaBox                           = GetFunction(pdfium, "_FPDFPage_GetMediaBox@20")
Global GetMetaText.protoGetMetaText                           = GetFunction(pdfium, "_FPDF_GetMetaText@16")
Global GetPageCount.protoGetPageCount                         = GetFunction(pdfium, "_FPDF_GetPageCount@4")
Global GetPageRotation.protoGetRotation                       = GetFunction(pdfium, "_FPDFPage_GetRotation@4")
Global GetPageSizeByIndex.GetPageSizeByIndex                  = GetFunction(pdfium, "_FPDF_GetPageSizeByIndex@16")
Global GetText.protoGetText                                   = GetFunction(pdfium, "_FPDFText_GetText@16")
Global GetUnicode.protoGetFontSize                            = GetFunction(pdfium, "_FPDFText_GetUnicode@8")
Global ImageObj_LoadJpegFile.protoImageObj_LoadJpegFile       = GetFunction(pdfium, "_FPDFImageObj_LoadJpegFile@16")
Global ImageObj_SetBitmap.protoImageObj_SetBitmap             = GetFunction(pdfium, "_FPDFImageObj_SetBitmap@16")
Global ImportPages.protoImportPages                           = GetFunction(pdfium, "_FPDF_ImportPages@16")
Global InitPdfLibrary.protoInitPdfLibrary                     = GetFunction(pdfium, "_FPDF_InitLibrary@0")
Global Link_GetAction.protoLink_GetAction                     = GetFunction(pdfium, "_FPDFLink_GetAction@4")
Global Link_GetDest.protoLink_GetDest                         = GetFunction(pdfium, "_FPDFLink_GetDest@8")
Global LoadDocument.protoLoadDocument                         = GetFunction(pdfium, "_FPDF_LoadDocument@8")
Global LoadPage.protoLoadPage                                 = GetFunction(pdfium, "_FPDF_LoadPage@8")
Global LoadTextPage.protoLoadTextPage                         = GetFunction(pdfium, "_FPDFText_LoadPage@4")
Global Page_Delete.protoPage_Delete                           = GetFunction(pdfium, "_FPDFPage_Delete@8")
Global Page_GenerateContent.protoPage_GenerateContent         = GetFunction(pdfium, "_FPDFPage_GenerateContent@4")
Global Page_InsertObject.protoPage_InsertObject               = GetFunction(pdfium, "_FPDFPage_InsertObject@8")
Global Page_New.protoPage_New                                 = GetFunction(pdfium, "_FPDFPage_New@24")
Global PageObj_CreateTextObj.protoPageObj_NewTextObj          = GetFunction(pdfium, "_FPDFPageObj_CreateTextObj@12")
Global PageObj_NewImgeObj.protoPageObj_NewImgeObj             = GetFunction(pdfium, "_FPDFPageObj_NewImageObj@4")
Global PageObj_NewTextObj.protoPageObj_NewTextObj             = GetFunction(pdfium, "_FPDFPageObj_NewTextObj@12")
Global PageObj_Transform.protoPageObj_Transform               = GetFunction(pdfium, "_FPDFPageObj_Transform@52")
Global RenderPage_Close.protoRenderPage_Close                 = GetFunction(pdfium, "_FPDF_RenderPage_Close@4")
Global RenderPageBitmap.protoRenderPageBitmap                 = GetFunction(pdfium, "_FPDF_RenderPageBitmap@32")
Global RenderPageDC.protoRenderPageDC                         = GetFunction(pdfium, "_FPDF_RenderPage@32")
Global SaveAsCopy.protoSaveAsCopy                             = GetFunction(pdfium, "_FPDF_SaveAsCopy@12")
Global SetPageRotation.protoSetRotation                       = GetFunction(pdfium, "_FPDFPage_SetRotation@8")
Global SetSystemFontInfo.protoSetSystemFontInfo               = GetFunction(pdfium, "_FPDF_SetSystemFontInfo@4")
Global Text_LoadFont.protoText_LoadFont                       = GetFunction(pdfium, "_FPDFText_LoadFont@20")
Global Text_SetFillColor.protoText_SetFillColor               = GetFunction(pdfium, "_FPDFPageObj_SetFillColor@20")
Global Text_SetText.protoText_SetText                         = GetFunction(pdfium, "_FPDFText_SetText@8")
Global TextObj_GetText.protoTextObj_GetText                   = GetFunction(pdfium, "_FPDFTextObj_GetText@16")

ProcedureC GetBlockCallback(*param.FPDF_FILEACCESS,  position, *buf, size)
  
  
  If *param\file_handle = 0
    If FileSize(*param\file$) > 0
      *param\file_handle = ReadFile(#PB_Any, *param\file$)
    EndIf
  EndIf
  
  If *param\file_handle
    FileSeek(*param\file_handle, position)
    ReadData(*param\file_handle, *buf, size)
    CloseFile(*param\file_handle)
    *param\file_handle = 0
    ProcedureReturn #True
  EndIf
  
  ProcedureReturn #False
  
EndProcedure

Procedure Open_FileAccess(file$)
  
  Protected *this.FPDF_FILEACCESS = AllocateStructure(FPDF_FILEACCESS)
  
  *this\FileLen  = FileSize(file$)
  *this\GetBlock = @GetBlockCallback()
  *this\param    = *this
  *this\file$    = file$
  
  ProcedureReturn *this
  
EndProcedure
ProcedureC GetWriteCallback(*this.FPDF_FILEWRITE, *pData, size)
  
  WriteData(11, *pData, size)
  ProcedureReturn size
  
EndProcedure
Procedure fileWrite()
  
  Global *this.FPDF_FILEWRITE = AllocateStructure(FPDF_FILEWRITE)
  *this\version               = 1
  *this\size                  = 32768
  *this\WriteBlock            = @GetWriteCallback()
  
  ProcedureReturn *this
  
EndProcedure

If InitPdfLibrary()
  document.s=OpenFileRequester("pdf auswählen","", "*.pdf|*.pdf", 0)
  hdocument.i   = LoadDocument(document.s, "")  ; Handle document.
  If hdocument = 0 And GetLastError() = 4
    passwort.s    = InputRequester("Passwortgeschützt", "Geben Sie das Passwort ein", "")
    hdocument.i   = LoadDocument(document.s, passwort.s)
    
  EndIf
  maxpages      = GetPageCount(hdocument)
  GetPageSizeByIndex(hdocument, 0, @pdfpagewidth, @pdfpageheight)
  name$=GetPathPart(document.s)+"neue"
  If  SelectedFilePattern() = 0
    If GetExtensionPart(name$) = ""
      name$ =  name$ + ".pdf"
    EndIf
    *filewrite.FPDF_FILEWRITE = fileWrite()
    npdf.i = CreateNewDocument()
    bereichsanfang=2
    bereichende=4
    For s =  bereichende To bereichsanfang Step - 1
      ImportPages(npdf, hdocument, Str(s), 0)
    Next s
    If CreateFile(11, name$)
      SaveAsCopy(npdf, *filewrite, 3)
      CloseFile(11)
    Else
      MessageRequester("Fehler", "Datei konnte nicht gespeichert werden!", #PB_MessageRequester_Error)
    EndIf
    CloseDocument(npdf)
    
  EndIf
  CloseLibrary(pdfium)   
EndIf
fabulouspaul
Beiträge: 120
Registriert: 01.04.2011 21:59

Re: PDF-Datei speichern mit PDFIUM.DLL

Beitrag von fabulouspaul »

Supi! Das hat geholfen!

Ich hatte nicht verstanden, dass es eine eigene kleine Routine braucht, die die Daten blockweise speichert.
:allright:
Antworten