问题
as we all know pdfium is now part of chrome, and this is a nice pdf render, but i am confronted with some problem.
the code is as follows, the default page is 12 as sepcified by the #page=12 assignment, when this page is opened, i could jump or navigate to other pages, but how to get the page number using javascript? is there any js api i can use to the get the page number?
<head>
<style type="text/css">
.pdf {
width: 100%;
height: 99%;
}
</style>
</head>
<div>
okok
</div>
<iframe class="pdf" src="http://127.0.0.1/test.pdf#page=12" frameborder="0"></iframe>
回答1:
I was finding the JS API too. On http://src.chromium.org/viewvc/chrome/trunk/src/pdf/instance.cc
there are the following methods if someone need it:
object accessibility()
bool documentLoadComplete()
number getHeight()
number getHorizontalScrollbarThickness()
string getPageLocationNormalized()
number getVerticalScrollbarThickness()
number getWidth()
double getZoomLevel()
void goToPage(int arg)
void grayscale(bool arg)
void loadPreviewPage(string arg0, int arg1)
void onload(function arg)
void onPluginSizeChanged(function arg)
void onScroll(function arg)
number pageXOffset()
number pageYOffset()
void printPreviewPageCount(int arg)
void reload()
void removePrintButton()
void resetPrintPreviewUrl(string arg)
void sendKeyEvent(number arg)
void setPageNumbers(string arg)
void setPageXOffset(number arg)
void setPageYOffset(number arg)
void setZoomLevel(double arg)
void fitToHeight()
void fitToWidth()
void zoomIn()
void zoomOut()
void print()
an example to get the current page is like that
<html>
<head>
<script type='text/javascript'>
window.addEventListener('load', function(){
var embed = document.getElementsByName('plugin')[0];
// this method should return the page location (not the page number, i suppose that made some calculation on the portion of PDF that is showed in the embed window)
alert(embed.getPageLocationNormalized());
// we can use other api methods
embed.removePrintButton();
}, false);
</script>
</head>
<body marginwidth='0' marginheight='0' style='background-color: rgb(38,38,38)' width='100%' height='100%'>
<embed width='100%' height='100%' name='plugin' src='path_to_pdf_file' type='application/pdf' />
</body>
</html>
回答2:
2019 update: While looking at the PDFium engine source code and header files (link here). I filtered out these API calls definitions:
virtual bool New(const char* url);
virtual bool New(const char* url,
virtual void PageOffsetUpdated(const pp::Point& page_offset);
virtual void PluginSizeUpdated(const pp::Size& size);
virtual void ScrolledToXPosition(int position);
virtual void ScrolledToYPosition(int position);
virtual void PrePaint();
virtual void Paint(const pp::Rect& rect,
virtual void PostPaint();
virtual bool HandleDocumentLoad(const pp::URLLoader& loader);
virtual bool HandleEvent(const pp::InputEvent& event);
virtual uint32_t QuerySupportedPrintOutputFormats();
virtual void PrintBegin();
virtual pp::Resource PrintPages(
virtual void PrintEnd();
virtual void StartFind(const char* text, bool case_sensitive);
virtual bool SelectFindResult(bool forward);
virtual void StopFind();
virtual void ZoomUpdated(double new_zoom_level);
virtual void RotateClockwise();
virtual void RotateCounterclockwise();
virtual std::string GetSelectedText();
virtual std::string GetLinkAtPosition(const pp::Point& point);
virtual bool IsSelecting();
virtual bool HasPermission(DocumentPermission permission) const;
virtual void SelectAll();
virtual int GetNumberOfPages();
virtual int GetNamedDestinationPage(const std::string& destination);
virtual int GetFirstVisiblePage();
virtual int GetMostVisiblePage();
virtual pp::Rect GetPageRect(int index);
virtual pp::Rect GetPageContentsRect(int index);
virtual int GetVerticalScrollbarYPosition() { return position_.y(); }
virtual void PaintThumbnail(pp::ImageData* image_data, int index);
virtual void SetGrayscale(bool grayscale);
virtual void OnCallback(int id);
virtual std::string GetPageAsJSON(int index);
virtual bool GetPrintScaling();
virtual void AppendBlankPages(int num_pages);
virtual void AppendPage(PDFEngine* engine, int index);
virtual pp::Point GetScrollPosition();
virtual void SetScrollPosition(const pp::Point& position);
virtual bool IsProgressiveLoad();
virtual pp::Instance* GetPluginInstance();
virtual pp::URLLoader CreateURLLoader();
virtual void OnPartialDocumentLoaded();
virtual void OnPendingRequestComplete();
virtual void OnNewDataAvailable();
virtual void OnDocumentComplete();
void UnsupportedFeature(int type);
void GetVisibleSelectionsScreenRects(std::vector<pp::Rect>* rects);
static int GetBlock(void* param, unsigned long position,
static bool IsDataAvail(FX_FILEAVAIL* param,
static void AddSegment(FX_DOWNLOADHINTS* param,
void LoadDocument();
bool TryLoadingDoc(bool with_password,
bool* needs_password);
void GetPasswordAndLoad();
void OnGetPasswordComplete(int32_t result,
void ContinueLoadingDocument(bool has_password,
void FinishLoadingDocument();
void LoadPageInfo(bool reload);
void CalculateVisiblePages();
bool IsPageVisible(int index) const;
bool CheckPageAvailable(int index, std::vector<int>* pending);
void UpdateTickMarks();
void ContinueFind(int32_t result);
void AddFindResult(const PDFiumRange& result);
void SearchUsingPDFium(const base::string16& term,
bool case_sensitive,
bool first_search,
int character_to_start_searching_from,
int current_page);
void SearchUsingICU(const base::string16& term,
bool case_sensitive,
bool first_search,
int character_to_start_searching_from,
int current_page);
bool OnMouseDown(const pp::MouseInputEvent& event);
bool OnMouseUp(const pp::MouseInputEvent& event);
bool OnMouseMove(const pp::MouseInputEvent& event);
bool OnKeyDown(const pp::KeyboardInputEvent& event);
bool OnKeyUp(const pp::KeyboardInputEvent& event);
bool OnChar(const pp::KeyboardInputEvent& event);
void FitContentsToPrintableAreaIfRequired(
void SaveSelectedFormForPrint();
int* page_index,
int* char_index,
int* page_index,
int* char_index,
void OnSingleClick(int page_index, int char_index);
void OnMultipleClick(int click_count, int page_index, int char_index);
int StartPaint(int page_index, const pp::Rect& dirty);
bool ContinuePaint(int progressive_index, pp::ImageData* image_data);
void FinishPaint(int progressive_index, pp::ImageData* image_data);
void CancelPaints();
void InvalidateAllPages();
void FillPageSides(int progressive_index);
void PaintPageShadow(int progressive_index, pp::ImageData* image_data);
void DrawSelections(int progressive_index, pp::ImageData* image_data);
void PaintUnavailablePage(int page_index,
int GetProgressiveIndex(int page_index) const;
void GetPDFiumRect(int page_index, const pp::Rect& rect, int* start_x,
int* start_y, int* size_x, int* size_y) const;
int GetRenderingFlags() const;
void Highlight(void* buffer,
int stride,
void DeviceToPage(int page_index,
int GetVisiblePageIndex(FPDF_PAGE page);
void SetCurrentPage(int index);
void TransformPDFPageForPrinting(FPDF_PAGE page,
void DrawPageShadow(const pp::Rect& page_rect,
void GetRegion(const pp::Point& location,
void** region,
int* stride) const;
void OnSelectionChanged();
static void Form_Invalidate(FPDF_FORMFILLINFO* param,
static void Form_OutputSelectedRect(FPDF_FORMFILLINFO* param,
static void Form_SetCursor(FPDF_FORMFILLINFO* param, int cursor_type);
static int Form_SetTimer(FPDF_FORMFILLINFO* param,
int elapse,
static void Form_KillTimer(FPDF_FORMFILLINFO* param, int timer_id);
static FPDF_SYSTEMTIME Form_GetLocalTime(FPDF_FORMFILLINFO* param);
static void Form_OnChange(FPDF_FORMFILLINFO* param);
static FPDF_PAGE Form_GetPage(FPDF_FORMFILLINFO* param,
int page_index);
static FPDF_PAGE Form_GetCurrentPage(FPDF_FORMFILLINFO* param,
static int Form_GetRotation(FPDF_FORMFILLINFO* param, FPDF_PAGE page);
static void Form_ExecuteNamedAction(FPDF_FORMFILLINFO* param,
static void Form_SetTextFieldFocus(FPDF_FORMFILLINFO* param,
static void Form_DoURIAction(FPDF_FORMFILLINFO* param, FPDF_BYTESTRING uri);
static void Form_DoGoToAction(FPDF_FORMFILLINFO* param,
int page_index,
int zoom_mode,
int size_of_array);
static int Form_Alert(IPDF_JSPLATFORM* param,
int type,
int icon);
static void Form_Beep(IPDF_JSPLATFORM* param, int type);
static int Form_Response(IPDF_JSPLATFORM* param,
void* response,
int length);
static int Form_GetFilePath(IPDF_JSPLATFORM* param,
void* file_path,
int length);
static void Form_Mail(IPDF_JSPLATFORM* param,
void* mail_data,
int length,
static void Form_Print(IPDF_JSPLATFORM* param,
int start,
int end,
static void Form_SubmitForm(IPDF_JSPLATFORM* param,
void* form_data,
int length,
static void Form_GotoPage(IPDF_JSPLATFORM* param, int page_number);
static int Form_Browse(IPDF_JSPLATFORM* param, void* file_path, int length);
static FPDF_BOOL Pause_NeedToPauseNow(IFSDK_PAUSE* param);
int32_t password_tries_remaining_;
bool defer_page_unload_;
bool selecting_;
int next_page_to_search_;
int last_page_to_search_;
int last_character_index_to_search_; // -1 if search until end of page.
int current_find_index_;
int next_timer_id_;
int last_page_mouse_down_;
int first_visible_page_;
int most_visible_page_;
bool called_do_document_action_;
bool render_grayscale_;
int page_index;
int painted_;
int progressive_paint_timeout_;
bool getting_password_;
virtual bool RenderPDFPageToDC(const void* pdf_buffer,
int buffer_size,
int page_number,
virtual bool RenderPDFPageToBitmap(const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
void* bitmap_buffer);
virtual bool GetPDFDocInfo(const void* pdf_buffer,
int buffer_size,
int* page_count,
virtual bool GetPDFPageSizeByIndex(const void* pdf_buffer,
int pdf_buffer_size, int page_number,
来源:https://stackoverflow.com/questions/28391198/does-pdfium-expose-its-api-to-javascript-i-want-to-get-the-number-of-current-pa