|
|
Comments and Discussions
|
|
 |

|
I agree with smesser i think you have a problem with the DPI.
try to force the DPI (they are genereted with the -d switch) to what you find more usefull.
Default Ghostscript is 72dpi
go to Ghostscript device website
search for "72 dpi" and u will find the spot.
|
|
|
|

|
yes, I use wide screen, the default dpi is 96, after I changed it to 96, the "zoom" is solved.
but the text seems a little bit "not smooth", like has been "carved".
Regards,
unruledboy_at_gmail_dot_com
http://www.xnlab.com
|
|
|
|

|
did u try the text anti aliasing? does it help?
|
|
|
|

|
I did not change the text anti aliasing, leave it as the default -1
what will be the best value?
Regards,
unruledboy_at_gmail_dot_com
http://www.xnlab.com
|
|
|
|

|
Try 4.
or 2 if thats too much
|
|
|
|

|
I don't know what is happening here and I don't know what the default DPI of ghostscript is but I will second the idea that it's probably a DPI issue.
|
|
|
|

|
For me when using multipage output it makes since to zero pad the number.
I use PDF's with many pages and looking for one that is not zero padded is a pain.
Instead of using %d you can use %02d or %0nd where n is the desired number of padding.
for example
page01.jpg
page02.jpg
instead of
page1.jpg
page10.jpg
...
page2.jpg
page20.jpg
You can either build this functionality into the GUI or people can just hard code
|
|
|
|

|
It could be a good idea, as soon as i get enough bugs i will update and provide this also as an option
|
|
|
|

|
I get poor image quality unless I add the following:
lstExtraArgs.Add("-dGraphicsAlphaBits=4 -dTextAlphaBits=4");
This is from the docs:
dTextAlphaBits=n
-dGraphicsAlphaBits=n
These options control the use of subsample antialiasing. Their use is highly recommended for producing high quality rasterizations. The subsampling box size n should be 4 for optimum output, but smaller values can be used for faster rendering. Antialiasing is enabled separately for text and graphics content. Allowed values are 1, 2 or 4.
Note that because of the way antialiasing blends the edges of shapes into the background when they are drawn some files that rely on joining separate filled polygons together to cover an area may not render as expected with GraphicsAlphaBits at 2 or 4. If you encounter strange lines within solid areas, try rendering that file again with -dGraphicsAlphaBits=1.
|
|
|
|

|
Hi, i know i already made a patch to add this very important options
but i can no longer edit my project so i can't update the article.
When it will be updated you will have it aviable
with multithreading too.
|
|
|
|

|
How did you make it thread-safe?
It was my understanding that ghostscript itself is not thread-safe and never would be. I believe I read that on their site.
Edit: Hum I may be thinging about iTextSharp, so I could be wrong.
|
|
|
|

|
I didn't made it thread safe, i just made it use more then 1 thread for the conversion is a simple line command :P
|
|
|
|

|
Hum.. I will look once you get it posted.
|
|
|
|
|

|
In your GraphicsAlphaBit and TextAlphaBit properties you have:
(value > 4) | (value == 3)
don't you mean ?
(value > 4) || (value == 3)
EDIT: Well I guess it doesn't matter I have just never seen | used in an if condition.
PS: Also a small GUI issue. You have the max value of the quality NumericUpDown set to 10 when it should be 100. The default quality value for jpeg is 75.
modified on Monday, April 13, 2009 1:10 PM
|
|
|
|

|
about the GUI issue:
you are right next update i will do (hopefull with the convert to stream) i will fix them!
about the |
their meaning is different, | is a normal OR
|| mean or but it also will not perform the second comparison if the first was false
it have the related & and && with the AND operator
with the same description (at least this is my understanding of it)
but in this case i think their use can be exchanged
|
|
|
|

|
I think you are slightly confused about the usage of these operators.
| and & are bitwise operators. They are useful for applying masks among other things or checking individual bits.
E.g.
Int8 mask = 1 | 2 | 4; // mask will equal the eight bit number binary 0000 0111 ( == 7 decimal)
Int8 justBit2 = mask & 2; // justBit2 will equal 2 if bit 2 is set, or 0 if bit 2 is unset.
|| and && are logical operators.
if (value1 == true || value2 == true)
do something
In your particular example the bitwise OR operator happens to do the job of the logical OR operator 'by accident'.
|
|
|
|

|
Im creating images from pdf to use with MODI to OCR the image.
How can I double the image size to have clearer text for the OCR to be more accurate ?
|
|
|
|

|
nvm. I hardcoded values to represent A4 porpotions :
lstExtraArgs.Add(string.Format(GS_PageSizeFormat, 924, 1302.4));
|
|
|
|

|
But i think the best way to set the paper size to A4 with papersize option and then
increase the resolutoin with -r
I create a new version with support for this but i'm unable to edit this article...
as soon as i find out why i will post the update!
|
|
|
|

|
Hi
I can see so much of white spces next to mu pdf page's image file.
I tried gsview and opened same pdf ,which also shows 75% white space next to my pdf page.
I tried some paid software which egnerated image file.
any thoughts
Thanks
abhi
|
|
|
|

|
Hi TaGoH,
You really did a great job since I played with the first version you provided.
Just wanted to notice, I would have worded the "DLL not found" explicit error message this way :
"The gsdll32.dll wasn't found in default dlls search path or is not in correct version (doesn't expose the required methods). Please download the version 8.64 from the original website"
Please note I corrected "gsdll32.dll" and not "gs32dll.dll" in this sentence.
It also seems that I can get "-dNOPAUSE" option twice in args when using this call :
PDFConvert converter = new PDFConvert();
converter.JPEGQuality = 75;
converter.OutputFormat = "jpeg";
converter.FirstPageToConvert = 1;
converter.LastPageToConvert = 1;
converter.Width = 200;
converter.Height = 200;
converter.FitPage = true;
converter.ThrowOnlyException = true;
Best regards,
Barbara Post
|
|
|
|

|
Of course i duplicate the parameter always :P
My mistake i correct the issue but i'm unable to edit my article..
it seems i have been locked out...
I no longer see an edit button...
I even add Multithread options... :(
|
|
|
|

|
Hello again,
I hope your troubles finally went away...
I noticed a tiny bug when calling the converted providing every option as a string, you loose last parameter :
{ args = new string[presetParameters.Length + 3];
for (int i = 1; i < presetParameters.Length + 1; i++)
args[i] = presetParameters[i - 1];
}
Around line 520.
Barbara
|
|
|
|

|
Well nope, but at least they update my article, just after i read ur commet! :P
So for a while this little bug will remain :P
Thanks you very much for your debugging!
to everyone reading in case you are passing a string with all the parameters to the dll use the fix provided here from barbara!
I will correct it as:
for (int i = 1; i <= presetParameters.Length; i++)
args[i] = presetParameters[i - 1];
Thanks Again Barbara!
Ps are u using the dll on a real project or just for fun?
|
|
|
|

|
Hello,
I'm using this great teaching piece of code/dll for a company's project, an intranet application that allows to browse a file tree via a web interface, and provides previsualisation of PDF files (for people to preview them before printing).
I enjoyed realizing this project, as well as coming back here to savour updated code
Barbara (who was a Java programmer but likes C# a lot after some time practicing it. First reason : development "standard" IDE is less resource-consuming. Throwing a troll hahaha, there are some nice full featured lightweight Java IDE around of course).
|
|
|
|

|
In the file: PDFConvert.cs
Line: 487
The method GetGeneratedArgs(string, string, string[]) seems to have an incomplete else statement.
else
args = new string[presetParameters.Length + 3];
should read something like
{ args = new string[presetParameters.Length + 3];
for (int xi = 0; xi < presetParameters.Length; xi++)
{
args[xi + 1] = presetParameters[xi];
}
}
|
|
|
|
|

|
pdf2img -dNOPAUSE -dNOPAUSE -dBATCH -sDEVICE=png16m -dFirstPage=1 -dLastPage=22 -sOutputFile=C:\Users\test.pdf.png C:\Users\test.pdf
|
|
|
|

|
What would the command line version of Ghostscript do with the same parameters for your file?
|
|
|
|

|
Is the result show in txtArguments.
|
|
|
|

|
Check this !
PDFConvert.cs
private string[] GetGeneratedArgs(string inputFile, string outputFile,string[] presetParameters)
...
args[2] = GS_Fixed2ndParameter;//"-dBATCH";//stop after
args[3] = GS_Fixed3rdParameter;//"-dSAFER";
Convert to tiff all pages converted but no colors , convert to png same problem (only one page)
modified on Thursday, March 26, 2009 4:45 PM
|
|
|
|

|
Please help me to fix it by telling me what ur sistem do if:
1st: open command prompt
2nd: go to Ghostscript directory
3rd: execute gs32win.exe with the parameters u r having. (add " to encapsulate the last 2 arguents)
tell me the result please.
|
|
|
|

|
pdf2img -dSAFER -dBATCH -dNOPAUSE -sDEVICE=tifflzw -sOutputFile=C:\Documents and Settings\svjc\Desktop\Nieuwe map\ConvertPDF\ConvertPDF\test.pdf.tif C:\Documents and Settings\svjc\Desktop\Nieuwe map\ConvertPDF\ConvertPDF\test.pdf
This works with the windows application.
With ghostscript 8.64 (GS> ...) Error : / undefined in pdf2img
With my webapplication it doesn't work : Cannot load dll
|
|
|
|

|
pdf2img -dSAFER -dBATCH -dNOPAUSE -sDEVICE=tifflzw -sOutputFile=C:\Documents and Settings\svjc\Desktop\Nieuwe map\ConvertPDF\ConvertPDF\test.pdf.tif C:\Documents and Settings\svjc\Desktop\Nieuwe map\ConvertPDF\ConvertPDF\test.pdf
windows version : OK
aspx-version
Unable to load DLL 'gsdll32.dll': Kan opgegeven module niet vinden. (Exception from HRESULT: 0x8007007E)
Ghostscript 8.64 (GS> ..) Error: /undefined in pdf2img
|
|
|
|

|
I just realease version 1.1.2b
try with that, it indeed didn't create multiple file for multiple page with older functions.
(i add a new propriety to the class so set it before call the convert function)
The problem of DLL not found is because it can't find the ghostscript DLL.
did u copy it where your binary file are?
i'm not sure about ASP but maybe should be in the same directory of ur pages..
|
|
|
|

|
FYI. the code just worked for Web application.
I put gsdll32.dll(GhostScript v8.64) and the wrapper dll
under the Bin folder of the Web project, and that's OK
(the wrapper class can be referenced to in your .cs code successfully)
|
|
|
|

|
Oh thanks you tchu_2000
|
|
|
|

|
Hi
A am also getting on asp.net:-
Unable to load DLL 'gsdll32.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
I have got the gsdll32.dll in the bin file.
Sry I'm no expert so I would apreciate any help you can give.
Thanks
All the best
Hawkmoth
|
|
|
|

|
Hi,
1.I'm not sure about ur case, but have u also put the wrapper dll
into the bin folder?
* wrapper dll: a C# dll calling the C-style gsdll32.dll.
Lord has already build such a wrapper dll.
There must be a public class and method in the wrapper dll.
You would successfully reference to the public class and method,
after including the wrapper dll to the bin folder.
2.If you have already done the above and still have problem:
I am using a differnt version of wrapper dll, but the code
should like this (in ur *.aspx.cs):
using aaa.bbb; protected void Page_Load(object sender, EventArgs e)
{
WrapperClass test = new WrapperClass(...); test.WrapperMethod(..., ...); }
|
|
|
|

|
Hi tchu_2000
thanks for your time!
Your are correct, I didn't have a wrapper in the bin File.
I found an example here:-
http://www.mattephraim.com/blog/2009/01/06/a-simple-c-wrapper-for-ghostscript/[^]
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace GSWrapper
{
public class GSWrapper
{
[DllImport("gsdll32.dll", EntryPoint = "gsapi_new_instance")]
private static extern int CreateAPIInstance(out IntPtr pinstance, IntPtr caller_handle);
[DllImport("gsdll32.dll", EntryPoint = "gsapi_init_with_args")]
private static extern int InitAPI(IntPtr instance, int argc, IntPtr argv);
[DllImport("gsdll32.dll", EntryPoint = "gsapi_exit")]
private static extern int ExitAPI(IntPtr instance);
[DllImport("gsdll32.dll", EntryPoint = "gsapi_delete_instance")]
private static extern void DeleteAPIInstance(IntPtr instance);
private string[] GetArgs(string inputPath, string outputPath, int firstPage, int lastPage, int width, int height)
{
return new string[]
{ "-q",
"-dQUIET",
"-dPARANOIDSAFER",
"-dBATCH",
"-dNOPAUSE", "-dNOPROMPT", "-dMaxBitmap=500000000", String.Format("-dFirstPage={0}", firstPage),
String.Format("-dLastPage={0}", lastPage),
"-dAlignToPixels=0",
"-dGridFitTT=0",
"-sDEVICE=jpeg",
"-dTextAlphaBits=4",
"-dGraphicsAlphaBits=4",
String.Format("-r{0}x{1}", width, height),
String.Format("-sOutputFile={0}", outputPath), inputPath };
}
public void CallAPI(string[] args)
{
GCHandle[] argStrHandles = new GCHandle[args.Length];
IntPtr[] argPtrs = new IntPtr[args.Length];
for (int i = 0; i < args.Length; i++)
{
argStrHandles[i] = GCHandle.Alloc(StringToAnsi(args[i]), GCHandleType.Pinned);
argPtrs[i] = argStrHandles[i].AddrOfPinnedObject();
} GCHandle argPtrsHandle = GCHandle.Alloc(argPtrs, GCHandleType.Pinned);
IntPtr gsInstancePtr;CreateAPIInstance(out gsInstancePtr, IntPtr.Zero);
InitAPI(gsInstancePtr, args.Length, argPtrsHandle.AddrOfPinnedObject());
}
private void Cleanup(GCHandle[] argStrHandles, GCHandle argPtrsHandle, IntPtr gsInstancePtr)
{
for (int i = 0; i < argStrHandles.Length; i++)
argStrHandles[i].Free();
argPtrsHandle.Free();
ExitAPI(gsInstancePtr);
DeleteAPIInstance(gsInstancePtr);
}
public void GeneratePageThumbs(string inputPath, string outputPath, int firstPage, int lastPage, int width, int height)
{
CallAPI(GetArgs(inputPath, outputPath, firstPage, lastPage, width, height));
}
public static byte[] StringToAnsi(string original)
{
byte[] strBytes = new byte[original.Length + 1];
for (int i = 0; i < original.Length; i++)
strBytes[i] = (byte)original[i];
strBytes[original.Length] = 0;
return strBytes;
}
}
}
The resulting GSWapper.ddl I put into the bin file of my main asp.net project with the gsdll32.dll.
Then called it like this:-
GSWrapper.GSWrapper myThumbnail = new GSWrapper.GSWrapper();
string strPath = Server.MapPath("~");
myThumbnail.GeneratePageThumbs(strPath + "SVS.pdf", strPath+"SVS.jpg", 1, 2, 150, 200);
When I ran it, it seemed to work fine but I had no resulting jpg.
I stepped through the code and it doesn't seem to error so I'm at a loss.
Am I doing some obviously stupid?
Thanks again
All the best
Hawkmoth
|
|
|
|

|
Do you have both the ConversionPDF.dll and Ghostscript in ur bin directory?
PS i don't have IIS installed so i can't test it i'm just giving ideas from other users..
|
|
|
|
|

|
Blimey...That worked great!
Without sounding to slushy, it's nice to see there are still some good and kind people in the world.
Thank you very much for your help guys!
All the best
Hawkmoth
|
|
|
|

|
Don't mention it. I have also been helped out by others, so i ought to produce the same
|
|
|
|

|
I add support for GraphicsAlphaBits and TextAlphaBits. in case you are still interested
|
|
|
|

|
Hi Guys
I'm in need of your advice again!
Everything has been working fine up till now.
Recently, we had to migrate to a new host for our website.
Now I back to the same error:-
Unable to load DLL 'gsdll32.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.DllNotFoundException: Unable to load DLL 'gsdll32.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[DllNotFoundException: Unable to load DLL 'gsdll32.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)]
module.GSWrapper.gsapi_new_instance(IntPtr& pinstance, IntPtr caller_handle) +0
module.GSWrapper.RunGhostScriptAPI(String strGhostScriptOption, String strSrcImagePath, String strDestImagePath) +518
DataPanel_PdfUpload.btnSubmit_Click(Object sender, EventArgs e) +630
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
The new host is a Windows system with NFS storage on a Linux system. Not sure if this makes a differance.
Any Ideas?
Thanks
All the best
Hawkmoth
|
|
|
|

|
Hey Hawkmoth
Did you ever figure this one out?
I have the same problem.
|
|
|
|

|
Useful article which really helped me.
1.Something to share:
- I've converted the source to a DLL, wrapped the function of gsapi into the form such as:
RunGhostScriptAPI(string strGhostScriptOption, string strSrcImagePath, string strDestImagePath)
So that it can be used more freely in many case (include successfully called from WebSite)
If anyone need this, I'd be glad to share the code in the next post.
- A version of gsdll32 8.64 rather than 8.63, will settle the imcompatibility probelm occured in Win2003/Vista
(I have tested it)
2.Problem: If the image files are located in a path comprised of Unicode characters(such as Asian Characters),
then the calling will never succeed.
I know that the path will be firstly converted to AnsiZ, and then to GCHandle, and finally fed to the gsapi.
The problem maybe caused by the Ansi conversion. Is there any safe way to work around the problem?
|
|
|
|

|
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace module
{
public class GSWrapper
{
#region GhostScript API::Import
[DllImport("gsdll32.dll", EntryPoint = "gsapi_new_instance")]
private static extern int gsapi_new_instance(out IntPtr pinstance, IntPtr caller_handle);
[DllImport("gsdll32.dll", EntryPoint = "gsapi_init_with_args")]
private static extern int gsapi_init_with_args(IntPtr instance, int argc, IntPtr argv);
[DllImport("gsdll32.dll", EntryPoint = "gsapi_exit")]
private static extern int gsapi_exit(IntPtr instance);
[DllImport("gsdll32.dll", EntryPoint = "gsapi_delete_instance")]
private static extern void gsapi_delete_instance(IntPtr instance);
#endregion
private GSWrapper()
{
}
public static int RunGhostScriptAPI(string strGhostScriptOption, string strSrcImagePath, string strDestImagePath)
{
int nRet = 0;
int numArgs;
object[] ansiArgs;
GCHandle[] gchArgs;
IntPtr[] ptrArgs;
GCHandle gchPtrArgs;
IntPtr prtGchPtrArgs;
IntPtr ptrGSInstance;
string[] strGhostScriptArguments = GetGeneratedArgs(strGhostScriptOption, strSrcImagePath, strDestImagePath);
numArgs = strGhostScriptArguments.Length;
ansiArgs = new object[numArgs];
gchArgs = new GCHandle[numArgs];
ptrArgs = new IntPtr[numArgs];
for (int i = 0; i < numArgs; i++)
{
ansiArgs[i] = StringToAnsiZ(strGhostScriptArguments[i]);
gchArgs[i] = GCHandle.Alloc(ansiArgs[i], GCHandleType.Pinned);
ptrArgs[i] = gchArgs[i].AddrOfPinnedObject();
}
gchPtrArgs = GCHandle.Alloc(ptrArgs, GCHandleType.Pinned);
prtGchPtrArgs = gchPtrArgs.AddrOfPinnedObject();
nRet = gsapi_new_instance(out ptrGSInstance, IntPtr.Zero);
if (nRet < 0)
goto LEVEL_0;
try
{
nRet = gsapi_init_with_args(ptrGSInstance, numArgs, prtGchPtrArgs);
if (nRet < 0)
goto LEVEL_0;
}
catch (Exception e)
{
nRet = -1000;
goto LEVEL_0;
}
LEVEL_0:
for (int i = 0; i < numArgs; i++)
{
gchArgs[i].Free();
}
gchPtrArgs.Free();
gsapi_exit(ptrGSInstance);
gsapi_delete_instance(ptrGSInstance);
return nRet;
}
private static readonly string FORMAT_GHOSTSCRIPT_ARGUMENT_FIRST = "pdf2img"; private static readonly string FORMAT_GHOSTSCRIPT_ARGUMENT_OUTPUTFILE = "-sOutputFile={0}"; private static string[] GetGeneratedArgs(string strGhostScriptOption, string strSrcImagePath, string strDestImagePath)
{
string[] options = strGhostScriptOption.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
string[] args = new string[options.Length + 3];
args[0] = FORMAT_GHOSTSCRIPT_ARGUMENT_FIRST; for (int i = 0; i < options.Length; i++)
{
args[1 + i] = options[i];
}
args[options.Length + 1] = String.Format(FORMAT_GHOSTSCRIPT_ARGUMENT_OUTPUTFILE, strDestImagePath);
args[options.Length + 2] = strSrcImagePath;
return args;
}
private static byte[] StringToAnsiZ(string str)
{
int intElementCount;
int intCounter;
byte[] aAnsi;
byte bChar;
intElementCount = str.Length;
aAnsi = new byte[intElementCount + 1];
for (intCounter = 0; intCounter < intElementCount; intCounter++)
{
bChar = (byte)str[intCounter];
aAnsi[intCounter] = bChar;
}
aAnsi[intElementCount] = 0;
return aAnsi;
}
}
}
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
How to use Ghostscript library to create an image (or images) from a PDF file
| Type | Article |
| Licence | CPOL |
| First Posted | 15 Jan 2009 |
| Views | 535,472 |
| Downloads | 15,536 |
| Bookmarked | 183 times |
|
|