Copy link to clipboard
Copied
Hello. I need to integrate PDF compression into my .Net desktop solution. I downloaded personalized samples, tried CompressPDFWithOptions example and it works perfectly (it's .NET 6 solution). But my application is .Net 4.7 WinForms, so I created test .Net 4.7 WinForms solution, got Adobe.PDFSdk version 3.4 from NuGet and copied exact the same code from CompressPDFWithOptions example. Unfortunately it doesn't work. When I call compressPDFOperation.Execute(executionContext); it completely hangs. The code is absolutely the same, copied from Adobe example. When I try previous library (for exampe 3.3 version) I'm getting 403 Forbidden error. Could you please help me to figure out how to use pdf compression under Framework 4.7. Thanks.
Here is my code:
private void button1_Click(object sender, EventArgs e)
{
try
{
// Initial setup, create credentials instance.
Credentials credentials = Credentials.ServicePrincipalCredentialsBuilder()
.WithClientId("myclientId")
.WithClientSecret("myClientSecret")
.Build();
// Create an ExecutionContext using credentials and create a new operation instance.
ExecutionContext executionContext = ExecutionContext.Create(credentials);
CompressPDFOperation compressPDFOperation = CompressPDFOperation.CreateNew();
// Set operation input from a source file.
FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"compressPDFInput.pdf");
compressPDFOperation.SetInput(sourceFileRef);
// Build Compress PDF options from supported compression levels and set them into the operation
CompressPDFOptions compressPDFOptions = CompressPDFOptions.CompressPDFOptionsBuilder()
.WithCompressionLevel(CompressionLevel.LOW)
.Build();
compressPDFOperation.SetOptions(compressPDFOptions);
// Execute the operation.
FileRef result = compressPDFOperation.Execute(executionContext);
//Generating a file name
String outputFilePath = CreateOutputFilePath();
// Save the result to the specified location.
result.SaveAs(Directory.GetCurrentDirectory() + outputFilePath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public static string CreateOutputFilePath()
{
String timeStamp = DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH'-'mm'-'ss");
return ("/output/compress" + timeStamp + ".pdf");
}
Copy link to clipboard
Copied
I created a couple of additional test projects and must admit that things are even a bit more strange. I was able to successfully run a code above in a .NET 4.7 console application, but WinForms .NET 4.7 application still hangs. Any idea what might be wrong or what's difference cause such a strange behaviour?