List<BoundingBoxPair> readPDFBox(string inputPDFFileName, List<int> pages, string boxname) { try { using (var mupdf = new MuPDF(Path.Combine(GetToolsPath(), "mudraw.exe"))) { if (controller_ != null) controller_.appendOutput("Getting the size of PDFBox...\n"); var rv = new List<BoundingBoxPair>(); var doc = mupdf.Execute<int>("open_document", Path.Combine(workingDir, inputPDFFileName)); if (doc == 0) return null; foreach (var p in pages) { if (abort) { if (controller_ != null) controller_.appendOutput(Properties.Resources.STOPCONVERTMSG + "\n"); return null; } int rotate = 0; BoundingBox box = new BoundingBox(), media = new BoundingBox(); const int repeatTimes = 10; for (int i = 1; i <= repeatTimes; ++i) { try { var page = mupdf.Execute<int>("load_page", doc, p - 1); media = mupdf.Execute< BoundingBox>("pdfbox_page", page, "media"); box = mupdf.Execute<BoundingBox>("pdfbox_page", page, boxname); rotate = mupdf.Execute<int>("rotate_page", page); break; } catch (Exception) { mupdf.ClearError(); if (i == repeatTimes) throw; } } BoundingBox bb; switch (rotate) { default: case 0: bb = new BoundingBox(box.Left - media.Left, box.Bottom - media.Bottom, box.Right - media.Left, box.Top - media.Bottom); break; case 90: bb = new BoundingBox(box.Bottom - media.Bottom, media.Right - box.Right, box.Top - media.Bottom, media.Right - box.Left); break; case 180: bb = new BoundingBox(media.Right - box.Right, media.Top - box.Top, media.Right - box.Left, media.Top - box.Bottom); break; case 270: bb = new BoundingBox(media.Top - box.Top, box.Left - media.Left, media.Top - box.Bottom, box.Right - media.Left); break; } if (controller_ != null) controller_.appendOutput(bb.ToString() + " (Page " + p + ")\n"); rv.Add(new BoundingBoxPair(bb.HiresBBToBB(), bb)); } if (controller_ != null) controller_.appendOutput("\n"); return rv; } } catch (Exception e) { if (controller_ != null) controller_.appendOutput(e.Message + "\n"); #if DEBUG System.Diagnostics.Debug.WriteLine(e.StackTrace); #endif return null; } }
bool pdfinfo(string file, out int page, out int version) { page = -1;version = -1; using (var mupdf = new MuPDF(Path.Combine(GetToolsPath(), "mudraw.exe"))) { try { int doc = mupdf.Execute<int>("open_document", Path.Combine(workingDir, file)); const int repeatTimes = 10; for (int i = 1; i <= repeatTimes; ++i) { try { if (page == -1) page = mupdf.Execute<int>("count_pages", doc); if (version == -1) version = mupdf.Execute<int>("version_document", doc); return true; } catch (Exception) { mupdf.ClearError(); if (i == repeatTimes) throw; } } } catch (Exception e) { if (controller_ != null) controller_.appendOutput(Properties.Resources.FAIL_PDFPAGES + "\n" + e.Message); return false; } } return false; }