public void LoadDocument(Document document, string filename, bool attemptSingleImageMode, ResolutionDpi viewingResolution)
    {
      try
      {
        IntPtr docPtr = LibPdfium.LoadDocument(filename);

        for(int i = 0; i < LibPdfium.GetPageCount(docPtr); i++)
        {
          IntPtr pagePtr = LibPdfium.LoadPage(docPtr, i);

          double height = LibPdfium.GetPageHeight(pagePtr);
          double width = LibPdfium.GetPageWidth(pagePtr);
          SizeInches pageSize = new SizeInches(width, height);

          Page myPage = new PageFromPdf(filename, i, pageSize, attemptSingleImageMode, viewingResolution);
          document.AddPage(myPage);

          LibPdfium.ClosePage(pagePtr);
        }

        LibPdfium.CloseDocument(docPtr);
      }
      catch(Exception ex)
      {
        string msg = ex.Message;
      }
    }
 public void LoadPagesFromFiles(Document document, string[] filenames, bool attemptPdfSingleImageImport, ResolutionDpi viewingResolution)
 {
   foreach(string filename in filenames)
   {
     LoadDocument(document, filename, attemptPdfSingleImageImport, viewingResolution);
   }
 }
 public PageFromPdf(string filename, int pageIndex, SizeInches size, bool attemptSingleImageMode, ResolutionDpi viewingResolution)
 {
   fFilename = filename;
   fPageIndex = pageIndex;
   fSingleImageMode = attemptSingleImageMode;
   fViewingResolution = viewingResolution;
   Initialize(size, 0, 0);
 }
 public Page()
 {
   fImageHandler = new ImageHandler(this);
   fLayoutThumbnail = null;
   fImageBoundsInches = null;
   fSizeInch = null;
   fResolutionDpi = null;
 }
 public void Initialize(int horizontalDpi, int verticalDpi)
 {
   using(Image myImage = CreateImage())
   {
     fSizePixels = new SizePixels(myImage.Size.Width, myImage.Size.Height);
     fResolutionDpi = new ResolutionDpi(horizontalDpi, verticalDpi);
     fSourceThumbnail = Utils.Imaging.CreateThumbnail(myImage, 200);
   }
   RefreshImage();
 }
 public ImageHandler(InterfaceImageCreator imageCreator)
 {
   fImageCreator = imageCreator;
   fSourceThumbnail = null;
   fThumbnail = null;
   fSizePixels = null;
   fResolutionDpi = null;
   fOrientation = 0;
   fIsMirrored = false;
   fTemporaryFilePath = null;
 }
    private Image CompressImage(Image image, int compressionFactor, ResolutionDpi maxResolution)
    {
      Image result;

      // TODO: Limit to maxResolution
      if(image.PixelFormat == System.Drawing.Imaging.PixelFormat.Format1bppIndexed)
      {
        // Jpeg format will make a Monochome image file bigger and lower the quality; Use PNG instead
        result = Imaging.ConvertImage(image, System.Drawing.Imaging.ImageFormat.Png, 0);
      }
      else
      {
        result = Imaging.ConvertImage(image, System.Drawing.Imaging.ImageFormat.Jpeg, compressionFactor);
      }

      return result;
    }
    public void SaveDocument(Document document, string filename, List<int> pageNumbers, bool append, 
      bool compressImage, 
      int compressionFactor, 
      bool importNativePages,
      ResolutionDpi exportResolution)
    {
      IntPtr pdfDocument = LibPdfium.CreateNewDocument();
      
      if(append && File.Exists(filename))
      {
        IntPtr sourceDoc = LibPdfium.LoadDocument(filename);

        for(int i = 0; i < LibPdfium.GetPageCount(sourceDoc); i++)
        {
          LibPdfium.CopyPage(pdfDocument, sourceDoc, i + 1);
        }

        LibPdfium.CloseDocument(sourceDoc);
      }
      
      foreach(int num in pageNumbers)
      {
        // Get the current page from document
        Page page = document.GetPage(num);

        Image image = null;

        if(page is PageFromPdf)
        {
          PageFromPdf pageFromPdf = page as PageFromPdf;

          if(pageFromPdf.SingleImageMode)
          {
            image = pageFromPdf.GetSingleImage();
          }
          else if(importNativePages)
          {
            pageFromPdf.ExportToPdfDocument(pdfDocument);
          }
          else
          {
            image = pageFromPdf.Render(exportResolution);
          }
        }
        else
        {
          image = page.GetSourceImage();
        }

        if(image != null)
        {
          if(compressImage)
          {
            image = CompressImage(image, compressionFactor, exportResolution);
          }

          DrawPage(pdfDocument, page, image);
        }
      }

      // Save the document...
      LibPdfium.SaveDocument(pdfDocument, filename);
    }
    public Image Render(ResolutionDpi resolution)
    {
      Image result = null;

      IntPtr docPtr = LibPdfium.LoadDocument(fFilename);
      IntPtr pagePtr = LibPdfium.LoadPage(docPtr, fPageIndex);

      int pixWidth = (int)(Size.Width * resolution.Horizontal); // width * dpiX
      int pixHeight = (int)(Size.Height * resolution.Vertical); // height * dpiY

      result = LibPdfium.Render(pagePtr, pixWidth, pixHeight);

      LibPdfium.ClosePage(pagePtr);
      LibPdfium.CloseDocument(docPtr);

      return result;
    }
    private void CalculateBounds()
    {
      SizePixels imageSizePixels = fImageHandler.SizePixels;
      SizeInches pageSizeInches = fSizeInch;

      fResolutionDpi = fImageHandler.ResolutionDpi;

      if(fResolutionDpi.IsDefined == false)
      {
        double image_aspect_ratio = imageSizePixels.Width / (double)imageSizePixels.Height;
        double page_aspect_ratio = pageSizeInches.Width / pageSizeInches.Height;

        double res;

        if(image_aspect_ratio > page_aspect_ratio)
        {
          // means our image has the width as the maximum dimension
          res = imageSizePixels.Width / pageSizeInches.Width;
        }
        else
        {
          // means our image has the height as the maximum dimension
          res = imageSizePixels.Height / pageSizeInches.Height;
        }

        fResolutionDpi = new ResolutionDpi(res);
      }

      // Calculate Image Layout
      double imageWidthInch = imageSizePixels.Width / fResolutionDpi.Horizontal;
      double imageHeightInch = imageSizePixels.Height / fResolutionDpi.Vertical;

      double x = (pageSizeInches.Width - imageWidthInch) / 2;
      double y = (pageSizeInches.Height - imageHeightInch) / 2;

      fImageBoundsInches = new BoundsInches(x, y, imageWidthInch, imageHeightInch);
    }