---
title: "pdf till jpg"
type: "forum-thread"
url: "https://www.webforum.nu/amne/dotnet/137594-pdf-till-jpg"
topic: ".NET"
topic_url: "https://www.webforum.nu/amne/dotnet"
author: "lucgo446"
published: "2005-11-14T15:43:05.000Z"
updated: "2005-11-15T21:24:09.000Z"
replies: 5
views: 573
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/dotnet/137594-pdf-till-jpg"
---

# pdf till jpg

## #1 — lucgo446, 2005-11-14T15:43Z

heloooo, jag vill konvertera ett pdf doc till jpg bilder(varge sida en bild) med asp.net 2.0

Nån som gjort det? funkar det bra? fort? kodsnutt?

Permalänk: https://www.webforum.nu/p/137594

## #2 — Mc Fetto, 2005-11-15T10:30Z

Ta en titt på ghostscript!

Permalänk: https://www.webforum.nu/p/1710617

## #3 — lucgo446, 2005-11-15T15:01Z

Det verkar inte finnas mkt på kombinationen GS och .net... suck...

**MEN**

Jag har hittat en kodsnutt som funkar på winform. behöver BARA hitta vad motsvarigheten till Clipboard är i asp.net... nån som vet?

OBS du måste ha Interop.Acrobat.dll i bin mappen

ladda ner lösningen här:
<http://www.codeproject.com/dotnet/pdfthumbnail.asp>

```
using System;
using System.IO;
using System.Drawing;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Acrobat objects
        Acrobat.CAcroPDDoc pdfDoc;
        Acrobat.CAcroPDPage pdfPage;
        Acrobat.CAcroRect pdfRect;
        Acrobat.CAcroPoint pdfPoint;

        AppSettingsReader appSettings = new AppSettingsReader();

        string pdfInputPath = appSettings.GetValue("pdfInputPath", typeof(string)).ToString();
        string pngOutputPath = appSettings.GetValue("pngOutputPath", typeof(string)).ToString();

        string templatePortraitFile = pdfInputPath + @"\pdftemplate_portrait.gif";
        string templateLandscapeFile = pdfInputPath + @"\pdftemplate_portrait.gif"; ;

        try
        {
            // Get list of files to process from the input path
            // Could change to read list from database instead
            string[] files = Directory.GetFiles(pdfInputPath, "*.pdf");

            for (int n = 0; n < files.Length; n++)
            {
                string inputFile = files[n].ToString();
                string outputFile = pngOutputPath + files[n].Substring(files[n].LastIndexOf(@"\") + 1).Replace(".pdf", ".png");

                /* Could skip if thumbnail already exists in output path
                FileInfo fi = new FileInfo(inputFile);
				if (!fi.Exists) {} */

                // Create the document (Can only create the AcroExch.PDDoc object using late-binding)
                // Note using VisualBasic helper functions, have to add reference to DLL in
                // C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Microsoft.VisualBasic.dll
                // Will always be available as .NET framework ships with all"
				
                pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");

                int ret = pdfDoc.Open(inputFile);

                if (ret == 0)
                {
                    throw new FileNotFoundException();
                }

                // Get the number of pages (to be used later if you wanted to store that information)
                int pageCount = pdfDoc.GetNumPages();

                // Get the first page
                pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(0);

                pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();

                pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");

                pdfRect.Left = 0;
                pdfRect.right = pdfPoint.x;
                pdfRect.Top = 0;
                pdfRect.bottom = pdfPoint.y;

                // Render to clipboard, scaled by 100 percent (ie. original size)
                // Even though we want a smaller image, better for us to scale in .NET
                // than Acrobat as it would greek out small text
                // see http://www.adobe.com/support/techdocs/1dd72.htm
                [B]pdfPage.CopyToClipboard(pdfRect, 0, 0, 100);

                IDataObject clipboardData = Clipboard.GetDataObject();[/B]

                if (clipboardData.GetDataPresent(DataFormats.Bitmap))
                {
                    Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);

                    // Size of generated thumbnail in pixels
                    int thumbnailWidth = 45;
                    int thumbnailHeight = 59;

                    string templateFile;

                    // Switch between portrait and landscape
                    if (pdfPoint.x < pdfPoint.y)
                    {
                        templateFile = templatePortraitFile;
                    }
                    else
                    {
                        templateFile = templateLandscapeFile;
                        // Swap width and height (little trick not using third temp variable)
                        thumbnailWidth = thumbnailWidth ^ thumbnailHeight;
                        thumbnailHeight = thumbnailWidth ^ thumbnailHeight;
                        thumbnailWidth = thumbnailWidth ^ thumbnailHeight;
                    }

                    // Load the template graphic
                    Bitmap templateBitmap = new Bitmap(templateFile);

                    // Render to small image using the bitmap class
                    Image pdfImage = pdfBitmap.GetThumbnailImage(thumbnailWidth, thumbnailHeight,
                                                                 null, IntPtr.Zero);

                    // Create new blank bitmap (+ 7 for template border)						 
                    Bitmap thumbnailBitmap = new Bitmap(thumbnailWidth + 7, thumbnailHeight + 7,
                                                        System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                    // To overlayout the template with the image, we need to set the transparency
                    // http://www.sellsbrothers.com/writing/default.aspx?content=dotnetimagerecoloring.htm
                    templateBitmap.MakeTransparent();

                    using (Graphics thumbnailGraphics = Graphics.FromImage(thumbnailBitmap))
                    {
                        // Draw rendered pdf image to new blank bitmap
                        thumbnailGraphics.DrawImage(pdfImage, 2, 2, thumbnailWidth, thumbnailHeight);

                        // Draw template outline over the bitmap (pdf with show through the transparent area)
                        thumbnailGraphics.DrawImage(templateBitmap, 0, 0);

                        // Save as .png file
                        thumbnailBitmap.Save(outputFile, System.Drawing.Imaging.ImageFormat.Png);

                        Console.WriteLine("Generated thumbnail... {0}", outputFile);
                    }

                    pdfDoc.Close();

                    // Not sure how why it is to do this, but Acrobat is not the best behaved COM object
                    // see http://blogs.msdn.com/yvesdolc/archive/2004/04/17/115379.aspx
                    Marshal.ReleaseComObject(pdfPage);
                    Marshal.ReleaseComObject(pdfRect);
                    Marshal.ReleaseComObject(pdfDoc);
                }
            }
        }
        catch (System.Exception ex)
        {
            Console.Write(ex.ToString());
        }

    }
}
```

Permalänk: https://www.webforum.nu/p/1710742

## #4 — Mc Fetto, 2005-11-15T18:01Z

> **lucgo446 skrev:**
>
> Det verkar inte finnas mkt på kombinationen GS och .net... suck...

Nej det som finnes är ett rätt bra program som du med olika parametrar kan göra olika saker. T ex skapa pdf av bilder eller tvärtom. Detta program kan du med lätthet anropa från .NET

Permalänk: https://www.webforum.nu/p/1710817

## #5 — lucgo446, 2005-11-15T18:05Z

Hej Mc F

Det braiga programmet är GS menar du? då ska jag kolla upp syntax och hur man anropar det.... 

Det är lite pyssel med det, nån annan lösning? nån?

Permalänk: https://www.webforum.nu/p/1710821

## #6 — emission, 2005-11-15T21:24Z

Det finns många .NET-komponenter för ändamålet. En som är gratis och ruggigt bra är

<http://www.tallcomponents.com/Default.aspx?id=pdfthumbnail>

Om du vill generera med "mer kontroll" så får du välja en annan komponent eller köra med ImageMagick + GhostScript, startat mha System.Diagnostics.Process

<http://www.webforum.nu/showthread.php?t=134351>

<http://www.imagemagick.org>

<http://www.cs.wisc.edu/~ghost/>

<http://www.c-sharpcorner.com/Code/2002/July/ShellCommandsInCS.asp>

Permalänk: https://www.webforum.nu/p/1711032

---

Tråden på webben: https://www.webforum.nu/amne/dotnet/137594-pdf-till-jpg
