PdfLoadedDocument filenot found - c#

I try to load a pdf document using PdfLoadedDocument method from Syncfusion.Pdf.WinForms NuGet package.
The code is pretty simple and look like this:
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(#"\\server\folder\sample.pdf");
But it raises file not found error.

You may:
Test manually if the file is here: open your file explorer and enter the path, does the file open ?
Test programmatically if the file is here: Put File.Exists(#"\\server\folder\sample.pdf") in your code. This may raise other issues (like access denied that the PdfLoadedDocument may have swallowed).
Edit:
If you take a look to de-compiled code of PdfLoadedDocument (from Syncfusion.Pdf.WinForms v17.4.0.46 NuGet package) it is:
public PdfLoadedDocument(string filename)
: this(PdfLoadedDocument.CreateStream(filename))
{
this.isDispose = true;
this.m_bCloseStream = true;
this.m_fileName = filename;
}
The PdfLoadedDocument.CreateStream code is:
if (filename == null)
throw new ArgumentNullException(nameof (filename));
if (!File.Exists(filename))
throw new ArgumentException("File doesn't exist", nameof (filename));
...
So the "File doesn't exist" exception occurs when File.Exists return false.
If you take a look to File.Exists documentation:
The Exists method returns false if any error occurs while trying to determine if the specified file exists. This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.
The file may be there but your program may not have the right to access it.
Anyway, I tested PdfLoadedDocument(#"\\server\users\Orace\test.pdf"); where I have full access to \\server\users\Orace, and it works fine.

https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Parsing.PdfLoadedField.html
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing; //<================
namespace Text_Extraction
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
using (PdfDocument document = new PdfDocument())
{
// Load an existing PDF document.
PdfLoadedDocument loadedDocument = new
PdfLoadedDocument(#"C:\Users\grayg\Documents\Text To Speech
Stuff\NationalTreeClimbingGuide2015April.pdf");
// Loading page collections

Related

Revit IExternal Application Error- doesn't seems to run at all

Should we have something with the External application to properly register the event?
I also tried putting two breakpoints one inside the start module and other inside the Export module.
the first responded and waited for me to continue and the next didn't respond(hope did not run the line)
Also,I had manually tried coping the addin file to the addin location to avoid any post build event error but still doesnt seem to work.
could you tell me what I am I doing wrong here.
Here is the code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.DB.Events;
using System.IO;
namespace UserDataSheet
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class UserDataSheetclass : IExternalApplication
{
public Result OnShutdown(UIControlledApplication application)
{
return Result.Succeeded;
}
public Result OnStartup(UIControlledApplication application)
{
try
{
// Register event.
application.ControlledApplication.DocumentOpened += new EventHandler<Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(ExportLog);
return Result.Succeeded;
}
catch (Exception)
{
return Result.Failed;
}
}
public void ExportLog(object sender, DocumentOpenedEventArgs args)
{
var doc = sender as Document;
var isFamilyDoc = doc.IsFamilyDocument;
// variables to use
string RevitUserName = "";
DateTime OpenTime = new DateTime();
string localUserName = "";
string filename = "";
string filepath = "";
string content = "";
if (isFamilyDoc == false)
{
var IsloggedIn = Autodesk.Revit.ApplicationServices.Application.IsLoggedIn;
if (IsloggedIn == true )//&& doc.IsModelInCloud == true)
{
// use variables
filepath = doc.PathName;
filename = doc.Title;
RevitUserName = doc.Application.Username;
OpenTime = DateTime.Now;
localUserName = System.Environment.MachineName;
}
content = string.Format("Date and time : {0} \n Revit UserName : {1} \n Local PC UserName : {2} \n FileName : {3} \n FilePath : {4} "
, OpenTime.ToString(), RevitUserName, localUserName, filename, filepath);
TaskDialog.Show("Model Open Writer info", "user and file details : \n " + content);
}
var writefilepath = Path.GetTempPath();
var Writefile = writefilepath + "//records.txt";
FileStream fs = new FileStream(Writefile, FileMode.Append);
StreamWriter writer = new StreamWriter(fs);
writer.Write(content);
writer.Close();
File.OpenRead(Writefile);
}
}
}
First of all, you can completely remove the TransactionMode and RegenerationOption. The latter is completely obsolete and does nothing at all anywhere whatsoever. The former is only useful when declaring an external command. It is useless and ignored in the context of an external application.
Secondly, to address your question: you can set a breakpoint in the beginning of OnStartup. If the breakpoint is not hit, your add-in is not being loaded at all. Probably something is wrong with your add-in registration, e.g., in the add-in manifest *.addin file.
Go back to square one, i.e., work through the getting started material and the developer guide instructions on registering and loading a Revit add-in.
If the breakpoint in OnStartup is hit, then your add-in is loading correctly, which means that the add-in manifest *addin file is OK. So, you do not need to worry about that. The VisibilityMode tag is not used for external applications, by the way.
Thanks, Jeremy It worked
Firstly I apologies for adding this as answer( I don't know how to add codes in comment)
It worked when I deleted my Addin file and recreated it may be I had made some mistake in it.
meanwhile I have copied the following code from examples and used it,honestly I did't understand this line of the code.
"public void ExportLog(object sender, DocumentOpenedEventArgs args)"
can you point to a right source that explains this part. I have three questions here :
what object type is sender and args are they of type application?
How do I add a 3rd parameter to this method say I want the user to input a string to name the file the data is copied to.
Can I do this
var newEvent = new EventHandler<Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(ExportLog);
instead of
application.ControlledApplication.DocumentOpened += new EventHandler<Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(ExportLog);
why does all example use += is this to register the event every time a new instance of Revit is opened?
Thanks for your help.
You can see the class of sender yourself by setting a breakpoint at the beginning of ExportLog and looking in the debugger.
No, you cannot modify the signature of the event handler. It is predetermined by the Revit API.
Yes.
It sounds to me as if you might save some time and effort for yourself by learning a bit more about the basics of C# and .NET programming in general before continuing to tackle this task.

Could not load type 'ImageProcessor.Imaging.CropLayer' from assembly 'ImageProcessor'

I'm working on a web-based image processing tool using cropper.js and ImageProcessor. I have an AJAX call that passes the cropper.js data to a C# handler, where that data is then passed to ImageProcessor and - in theory - used to crop the image and save it.
What's actually happening, when I run it locally through Visual Studio, is that the AJAX call returns a 500 exception and I see the following error message in the Diagnostics panel, under the "Events" tab:
Could not load type 'ImageProcessor.Imaging.CropLayer' from assembly 'ImageProcessor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
ImageProcessor was installed correctly, via NuGet Package Manager, in both projects of my solution - the actual web application, and a class library named Middle that's referenced in the web app project. The handler is contained within Middle. The handler does not throw an exception if I remove all the code that references ImageProcessor.
I have no idea what could be causing this. I've tried Googling the issue and none of the answers I could find helped me. This question suggests typing the complete namespaces, but I tried that and it didn't fix the issue. The comments on this one suggest clearing out my bin and obj folders, cleaning and rebuilding the solution, but that didn't work either.
Here's what my C# handler looks like so far (I can't finish it until I know whether what I have so far works, but I can't test it properly due to the issue I'm having).
using ImageProcessor;
using ImageProcessor.Imaging;
using System;
using System.IO;
using System.Web;
namespace Middle
{
public class CropImage : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
var x = float.Parse(context.Request["x"]);
var y = float.Parse(context.Request["y"]);
var width = float.Parse(context.Request["width"]);
var height = float.Parse(context.Request["height"]);
var cropLayer = new CropLayer(x, y, width, height);
var filePath = AppDomain.CurrentDomain.BaseDirectory + context.Request["fileName"];
var imageData = File.ReadAllBytes(filePath);
using (MemoryStream inStream = new MemoryStream(imageData))
{
using (MemoryStream outStream = new MemoryStream())
{
// Initialize the ImageFactory using the overload to preserve EXIF metadata.
using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true))
{
// Load, resize, set the format and quality and save an image.
imageFactory.Load(inStream)
.Crop(cropLayer)
.Save(outStream);
}
}
}
}
}
}
If there's any other information you need, please do let me know.
How can I resolve this issue?

Programmatically Open Word Document Located in the Computer in C#

I'm using WinForms. I have a form that has a button.
Goal: On button click: Open up a word document. Where the file path is hard coded into the program. I don't want the users to have to locate the word document.
Problem: I receive this error message. When I wrote my code, I get a red error line under 'Application'.
private void button1_Click(object sender, EventArgs e)
{
this.Application.Documents.Open(#"C:\Test\NewDocument.docx", ReadOnly:true)
}
Instead of adding interop in your reference, you may also consider to use this:
System.Diagnostics.Process.Start(#"C:\Test\NewDocument.docx");
first add the dll of Microsoft.Office.Interop.Word to your references then add this:
using Microsoft.Office.Interop.Word;
and use the following code:
Application ap = new Application();
Document document = ap.Documents.Open(#"C:\Test\NewDocument.docx");
This Application is not this.Application it's Microsoft.Office.Interop.Word.Application.
So you can use this code:
using System;
using Microsoft.Office.Interop.Word;
class Program
{
static void Main()
{
// Open a doc file.
Application application = new Application();
Document document = application.Documents.Open("C:\\word.doc");
//Do whatever you want
// Close word.
application.Quit();
}
}
There is a good answer above which is:
System.Diagnostics.Process.Start(#"C:\Test\NewDocument.docx");
This should be modified for .Net Core 2 and above to be:
var p = new Process();
p.StartInfo = new ProcessStartInfo(filename)
{
UseShellExecute = true
};
p.Start();

Tracking files of a particular extension

I want to track the files which are opened by the user, and select them by one particular extension. If the opened file has that extension, then I want to assign it's file path to a variable for further processing. Example applications are very cpu demanding. Is there an easy, efficient way to do that?
System wide monitoring of file-->open events (including network drives, thumb drives, etc) would require you to write a FS filter driver.
Since you have access to the machine, and you definitely need system wide access, you could simply write a simple app that will be associated with the Powerpoint extensions, perform the copy, then open Powerpoint using the filepath as a command line argument. It would look similar to the following:
using System;
using System.Windows;
using System.Diagnostics;
using System.IO;
namespace WpfApplication1
{
internal class MainWindow : Window
{
public MainWindow()
{ }
[STAThread()]
static void Main(string[] args)
{
if (args.Length == 0)
{
// [ show error or print usage ]
return;
}
if (!File.Exists(args[0]))
{
// [ show error or print usage ]
return;
}
// Perform the copy
FileInfo target = new FileInfo(args[0]);
string destinationFilename = string.Format("X:\\ExistingFolder\\{0}", target.Name);
File.Copy(target.FullName, destinationFilename);
// You may need to place the filename in quotes if it contains spaces
string targetPath = string.Format("\"{0}\"", target.FullName);
string powerpointPath = "[FullPathToPowerpointExecutable]";
Process powerpointInstance = Process.Start(powerpointPath, targetPath);
// This solution is using a wpf windows app to avoid
// the flash of the console window. However if you did
// wish to display an accumulated list then you may choose
// to uncomment the following block to display your UI.
/*
Application app = new Application();
app.MainWindow = new MainWindow();
app.MainWindow.ShowDialog();
app.Shutdown(0);
*/
Environment.Exit(0);
}
}
}
Hope this helps.

Error from reading with ClearCanvas in DICOM file in C# and how to display the DICOM VCR tag

I'm receiving an error in runtime using the ClearCanvas libraries. Here is the error
Could not find file 'C:\Users\Don jar\documents\visual studio 2010\Projects\DICOMCA\DICOMCA\bin\Debug\Fluro.dcm'.
The error points to this section of the code:
theFile.Load(DicomReadOptions.Default);
I will be grateful for any help. Thanks
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string filename = "Fluro.dcm";
DicomFile theFile = new DicomFile(filename);
theFile.Load(DicomReadOptions.Default);
foreach (DicomAttribute attribute in theFile.DataSet)
{
Console.WriteLine("Tag: {0}, Value: {1}", attribute.Tag.Name, attribute.ToString());
}
}
}
The filename parameter to the constructor to DicomFile sets the absolute path of the DicomFile. Just set this to the path to a DICOM file in your "picture" folder, as you mention.
The exception you are seeing is caused by the fact that you gave a relative path to the DICOM file, and its just trying to load it in the directory where your application is located.

Categories

Resources