1 Reply Latest reply on Jul 24, 2013 6:49 AM by thomas wornall

    Quality Setting for TIFF/JPG using SaveAs

    SD2k12

      Hi,

      I'm trying to export a jpg or tiff as a "preview" image for a single pdf page. However, they look terrible - I am accessing the javascript object through C# and calling "SaveAs", but I can't find any parameters or methods to set the quality of the image. Can anyone help?

       

      Edit: Using Acro 9/X.

       

      Thanks

        • 1. Re: Quality Setting for TIFF/JPG using SaveAs
          thomas wornall

          what i ended up doing was using c# to open a pdf select the first page and copying it to clipboard then pulling the image back out of the clipboard, resizing and saving...

          public static void copyPdfCoverToClipboard(string filePath)

                  {

                      arc.AcroAVDoc afile = new arc.AcroAVDoc();

                      bool returned = afile.Open(filePath,"");

                      if (returned == true) {

                          //Console.WriteLine("opened file");

                      }

                      arc.AcroPDDoc temp = afile.GetPDDoc();

                      arc.AcroPDPage apage = temp.AcquirePage(0);

                      arc.AcroPoint size = apage.GetSize();

                      arc.AcroRect rect = new arc.AcroRect();

                      rect.Top = 0;

                      rect.Left = 0;

                      rect.right = size.x;

                      rect.bottom = size.y;

                      bool returned2 = apage.CopyToClipboard(rect, 0, 0, 100);

                      if (returned2 == true) {

                          //Console.WriteLine("Copied");

                      } else {

                          Console.WriteLine("No Copied");

                      }

                      /**forgot to close and paid the price when i couldnt delelete... **/

                     

                      afile.Close(0);

                  }

           

          /** this took some reading to figure out... apparently it has to do with threading... **/

          public static Image collectIMGfromClipAndSave()

                  {

                      /***

                       * ref - http://ryanfarley.com/blog/archive/2006/01/31/15840.aspx (*3)

                       * http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.getimage.aspx?cs-sa ve-lang=1&cs-lang=csharp#code-snippet-2

                       *

                       *

                       *

                       * THIS WAS A GRADE A PAIN IN THE BUTT.....

                       * [STAThread] just above Main().... MAKES THIS WORK.....

                       * ouch my head hurts but is happy it works

                       *

                      **/

                      ImageFormat theFormat = ImageFormat.Jpeg;

                      //System.Threading.Thread.Sleep(1000);

                      if (Clipboard.ContainsImage())

                      {

                          Image clipboardImage = Clipboard.GetImage();

                          return clipboardImage;

                      }

                      else

                      {

                          Console.WriteLine("nothing contained!!!");

                      }

                      return null;

                  }

          public static Image resizeImage(Image imgToResize, float width)

                  {

                      //http://tech.pro/tutorial/620/csharp-tutorial-image-editing-saving-cropping-and-resizing

                      int sourceWidth = imgToResize.Width;

                      int sourceHeight = imgToResize.Height;

           

           

                      float nPercent = 0;

                      /**convert sourcewidth to float and divide width to get percentage*/

                      nPercent=(width/(float)sourceWidth);

                      /** find new hight and width **/

                      int destWidth = (int)(sourceWidth * nPercent);

                      int destHeight = (int)(sourceHeight * nPercent);

           

           

                      /** start of create new image to return... */

                      Bitmap b = new Bitmap(destWidth, destHeight);

                      Graphics g = Graphics.FromImage((Image)b);

                      g.InterpolationMode = InterpolationMode.HighQualityBicubic;

           

           

                      g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);

                      g.Dispose();

           

           

                      return (Image)b;

                  }

           

           

           

           

                  /** wrote this function so i could do overloads **/

                  private static void forpdftojpgFunc(string src, string outName, float w) {

                      Image image;

                      string newPath;

                      //Console.WriteLine("Copying page 1 of pdf");

                      methods.copyPdfCoverToClipboard(src);

                      image = methods.collectIMGfromClipAndSave();

                      image = methods.resizeImage(image, w);

                      newPath = Path.GetDirectoryName(src) + "\\" + outName + ".jpg";

                      image.Save(newPath);     

                  }

           

           

                  public static void pdfPg1ToJPG(string src, float w)

                  {

                      forpdftojpgFunc(src, Path.GetFileNameWithoutExtension(src), w);

                  }

                  public static void pdfPg1ToJPG(string src, string outName, float w)

                  {

                      forpdftojpgFunc(src, outName, w);

                  }

              }

          /************************ please note these are functions pulled form different files you will probably need to reorganize them to get them to work properlpy ***************/

          class Program

              {

                  [STAThread]/**this makes the magic work I DONT KNOW WHY **/

                  static void Main(string[] args)

                  {

                      List<string> sources = new List<string>();

                      string outName = "";

                      if (args.Count() < 1)

                      {

                          Console.WriteLine("Enter A PDF");

                          sources.Add(Console.ReadLine());

                          Console.WriteLine("file out name");

                          outName = Console.ReadLine();

                      }

                      else {

                          for (int i = 0; i < args.Count(); i++)

                          {

                              sources.Add(args[i]);

                          }

                      }

                      Console.WriteLine("New Width:");

                      int width = Convert.ToInt16(Console.ReadLine());

                     

                      foreach(string source in sources){

                          if (outName != "")

                          {

                              methods.pdfPg1ToJPG(source, outName, width);

                          }

                          else

                          {

                              methods.pdfPg1ToJPG(source, width);

                          }

                      }

                  }

              }

           

          I know its not exactly what you are looking for but maybe it can help you in some way...

           

          I use it to make cover previews for the website in bulk or 1 at a time....

           

          I hope this helps....