I'm using visual studio 2015 with VB language Web application, my issue in brief I used Ghostscript to extract pdf first page to png its work fine but if pdf file name contain space its does not extract png and no error show up or if written on other language the error " Page number is not in pages number range!".
I appreciate any help.
test.pdf ---> work fine
new york.pdf --->nothing happen no error
show up
pdf file name not written in English --->error "Page number is
not in pages number range!"
code is
Imports Ghostscript.NET
Imports Ghostscript.NET.Rasterizer
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Drawing
Partial Class Default6
Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim inputPdfPath As String = "d:\test\test.pdf"
Dim outputPath As String = "d:\test\"
Using Rasterizer = New GhostscriptRasterizer
Rasterizer.CustomSwitches.Add("-dUseTrimBox")
Rasterizer.CustomSwitches.Add("-g683x960")
Rasterizer.CustomSwitches.Add("-f")
Rasterizer.Open(inputPdfPath)
Dim pageFilePath As String = Path.Combine(outputPath, "Page1" + ".jpg")
Dim img As Image = Rasterizer.GetPage(100, 1)
img.Save(pageFilePath, ImageFormat.Png)
Console.WriteLine(pageFilePath)
End Using
End Sub
End Class
First posted in the comments section, I'm including information as answer, as it worked for the user.
Here is a link useful to solve the blank spaces trouble:
https://www.vbforums.com/showthread.php?703121-RESOLVED-How-to-Shell-from-a-directory-with-spaces-in-path
For non-english characters, please try with the following approach:
Illegal characters in path (Chinese characters)
If the last solution does not work, another alternative is to loop through files in directory and identify the one you are looking for by another meta-data ou file info. In this case, please check this link:
vb.net how to loop through a directory listing?
Related
I am trying to import a text file into a rich text box for my Visual Basic project (Its a Windows Forms App (.Net Framework)). I copied the text information from the internet, and pasted it into notepad. Then I saved this in a folder I created called txtFiles inside the Bin folder of my project. However, when I used the FileOpen command, it gives me an error.
The code I used:
Dim strTemp As String = ""
rtbReference.Text = Nothing
FileOpen(1, "../txtFiles/Thriller.txt", OpenMode.Input)
Do Until EOF(1) = True
Input(1, strTemp)
rtbReference.Text = strTemp
Loop
FileClose(1)
Picture:
The error that I got when running my program
Any help would be appreciated, thank you so much
There are more easier methods of working with files in the 21st century. I mean the IO namespace provides a handful of methods.
Try any of the following. Add this at the top of your code file
Imports System.IO
Method 0: File.ReadAllText
Since the file you want to load is a plain tet file, I suggest you use this.
RichTextBox1.Text = File.ReadAllText(pathtofile)
Method 1: Richtextbox.LoadFile()
Someone (or something) has already posted the link to an article on this in the comments but i'm pretty sure you've not gone through it. Well here's how to use it
RichTextBox1.LoadFile(pathtofile)
I use this method for only rtf files but it was completely my decision.
Method 2 To n - 1
You need to know how to use a search engine as there are many methods out there that can be used such as the StreamReader and so on.
I am creating a small application to parse HTML of a page into variables so I can generate code for another proprietary application. I am using VB with HTMLAgilityPack for parsing. When I execute the load statement, no error are shown but all other code after that line simply fails to execute, as if it isn't even there.
Private Sub Importer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim strImportType As String
Dim strImportURL As String
strImportType = main.importType
strImportURL = main.importURL
Dim web As New HtmlAgilityPack.HtmlWeb
Dim content As New HtmlAgilityPack.HtmlDocument
content = web.Load(strImportURL)
After using Visual Vincent suggestion to open the Exceptions Settings and force VB to fail it allowed for more information. This issue was simply that the file was not being found due to spaces in the filename. Adding quotes around the filename resolved the issue.
I am using VB Net and telerik controls to build a web application. I have a screen with a few ColumnChart RadHtmlCharts on, and I need to get them into a PowerPoint Presentation in the Code Behind file.
My current approach is taking an existing .PPTX file and stepping through it, replacing text where required and so on. Now I just need to get the charts into the presentation.
Here is the loop that I am making through the slides.
' generate
For Each slide As SlidePart In pCopy.PresentationPart.SlideParts
' THIS IS WHERE THE CHARTS NEED TO BE ADDED TO THE SLIDE
Next
Note: I have written a function that returns the chart, all I am missing now is the steps required to get it into the presentation.
Here are the 'Imports' I have included...
Imports Telerik.Web.UI
Imports System.Data
Imports System.IO
Imports System.Data.SqlClient
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.PowerPoint
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Presentation
Any help will be greatly appreciated
Just as rdmptn has suggested I took the chart and saved that as an image first, and then from there added the images to the slides using templates.
So the basic steps I took were...
1) Create Powerpoint presentation with a holding image in (name the image via the 'Selection Pane')
2) In the Code Behind loop through all of the slideparts, checking if the name of the part (image) is the chart we are looking for
3) Using the following code replace the holding image with the generated image of the chart (GenerateChartImage() returns the filepath to the generated image)
Using imgStream As FileStream = New FileStream(GenerateChartImage(), FileMode.Open)
imagePart.FeedData(imgStream)
End Using
Here is how I saved the chart as an image (where generate chart just builds the chart programatically...
Dim Chart As New RadChart()
Dim FilePath As String = "~/Folder/" & FileName & ".jpg"
Dim Path As String = Server.MapPath(FilePath)
Chart = GenerateChart(Index)
' save the image and return the filepath
Chart.Save(Path, System.Drawing.Imaging.ImageFormat.Jpeg)
return Path
I hope this helps someone else as I understand this topic can be very confusing!
Assuming you know how to put images in the pptx file (I, myself, don't), you "only" need to get the images from the HtmlChart. There several approaches:
start from the following code library on exporting the control: http://www.telerik.com/support/code-library/exporting-radhtmlchart-to-png-and-pdf Using the ClientExportManager control is quite straightforward.
this should provide you with an image of the chart on your server that you can use in your existing code
Other ideas you can consider:
you can use a service to get many images on your server first if you want to embed multiple charts: http://docs.telerik.com/devtools/aspnet-ajax/controls/clientexportmanager/how-to/save-exported-files
tools like PhantomJS can let you run the browser on the server so it can generate the charts, that you can export and embed. Otherwise you will need end user action to load the page with the charts
I writing a pair of programs in visual basics that will consist of a client and receiver. The client is completed making an output text file similar to below.
Dim FileName As String = "Text.txt"
Dim message As String
Dim file As System.IO.StreamWriter
'lblmessage.text says "Call MsgBox("Hello!", 0, "Version Beta 0.3")"
lblmessage.text = message
Dim Drive As String = "C:\hello world\" & FileName
file = My.Computer.FileSystem.OpenTextFileWriter(Drive, True)
file.WriteLine(message)
file.Close()
A sister program that is designed to be a reader will read the generated file.
The program will then take the text located in the selected file and use it as code in the readers programming.
Best example I can show...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\hello world\Text.txt")
fileReader
End Sub
where "fileReader" is suppose to run the generated code from the previous program and use it as code it the Reader.
The point of this program is to create help tickets in the client and have a tool for reviewing these ticket in the same way they were submitted through the second app.
vba and vb.net is different. I'm not sure in which language your doing your stuff.
vba: use the ScriptControl.Eval command to execute a bunch of commands.
vb.net: it's a bit more code. you can use VBCodeProvider Class. s this SO Question: Load VB.net code from .txt file and execute it on fly using System.CodeDom.Compiler
UPDATE
I found a perfekt resource if you are interested in how to do it right and how it works in the background: http://www.codemag.com/article/0211081
How can we delete or move a file when PDF size is 0Byte.
i have placed sample files and sample code.
Collapse | Copy Code
If file.exist(d:\source\test1.pdf) then
File.move(d:\source\test1.pdf, d:\destination\test1.pdf)
End If
or
If file.exist(d:\source\test1.pdf) then
File.delete(d:\source\test1.pdf, d:\destination\test1.pdf)
End If
both code doesn't work when pdf size is 0
Note: We cannot preview this pdf. we can Check after downloading pdf from below location
https://drive.google.com/file/d/0B_nzYHWVJJ7KaVZfQUZJVmsxUXM/view?usp=sharing[^]
If your wanting to delete a file I would recommend using the System.IO namespace... Make sure to import System.IO namespace at the top of your class...
System.IO.File.Delete(d:\source\test1.pdf)
You are also using File.Delete wrong... Not sure if you overlooked this or not?
If file.exist(d:\source\test1.pdf) then
File.Delete(d:\source\test1.pdf, d:\destination\test1.pdf) 'Only should be your file...
End If
Please see this link...
http://www.sevenforums.com/tutorials/79699-undeletable-file-delete.html
You can use
My.Computer.FileSystem.DeleteFile("yourfile.pdf")
or
System.IO.File.Delete("yourfile.pdf")
I've tried both of these methods with your 0 byte pdf file and they both are working perfectly fine.
The System.IO.FileInfo class can be used to retrieve details about a file.
Dim myFile As New System.IO.FileInfo("myFile.txt")
Dim myFilesize As Long = myFile.Length
Then you can use Mr CoDeXeR's answer to delete your file.
If (myFilesize = 0) Then
System.IO.File.Delete("myFile.txt")
End If
Alternately, you could recycle the same FileInfo object:
If (myFilesize = 0) Then
Try
myFile.Delete
Catch e As Exception
MessageBox.Show(e.ToString)
End Try
End If
If you are receiving an IOException error, you may need to ensure that any streams you used to download the file are closed before trying to delete.