New to aspose.pdf

Hi,

I am new to your pdf product, have downloaded it and trying to save a file and see what happens when I open the pdf file. Used the following code and got error message when trying to open pdf document.

Dim pdf As Pdf = New Pdf
Dim section As Section = New Section(pdf)
pdf.Sections.Add(section)
section.PageInfo.PageHeight = 1500
section.PageInfo.PageWidth = 1200

pdf.Save(“c:\pdftest.pdf”)

The error message I am getting is “There was an error opening this document. There was a problem reading this document (130)”
Any insight as to why this is happening would be appreciated.

The test I would like to perform with your product is read a text file and convert to pdf. Can you point me to some code so that I could test this on my client vb.net project. Without trying to figure out your object model.


Thanks,
Bob Modrich

Dear Bob,

Thanks for your consideration.

1. You code generated a bad pdf file because you use an empty section. I will added support for enpty document and empty section in the next fix.

2. Here is an example of converting text file to pdf:

[C#]
FileStream fstream = new FileStream(@“E:\Projects\CSharp\Pdf-bak\test.txt”,FileMode.Open);
StreamReader sreader = new StreamReader(fstream);

Pdf pdf1 = new Pdf(@“e:\projects\CSharp\customer\Aspose.Pdf.lic”);
Section sec1 = pdf1.Sections.Add();

sec1.Paragraphs.Add(new Text(""));

string curLine = sreader.ReadLine();
while(curLine != null)
{
sec1.Paragraphs.Add(new Text(curLine));
curLine = sreader.ReadLine();
}

pdf1.Save(“e:/temp/test.pdf”);
sreader.Close();
fstream.Close();

[VisualBasic]
Dim fstream As FileStream = New FileStream(“E:\Projects\CSharp\Pdf-bak\test.txt”, FileMode.Open)
Dim sreader As StreamReader = New StreamReader(fstream)

Dim pdf1 As Pdf = New Pdf(“e:\projects\CSharp\customer\Aspose.Pdf.lic”)
Dim sec1 As Section = pdf1.Sections.Add()

sec1.Paragraphs.Add(New Text(""))

Dim curLine As String = sreader.ReadLine()
While Not curLine Is Nothing
sec1.Paragraphs.Add(New Text(curLine))
curLine = sreader.ReadLine()
End While

pdf1.Save(“e:/temp/test.pdf”)
sreader.Close()
fstream.Close()


Hi Tommy,

Thanks, that worked.

Also my text files have a chr(12) in them which indicates a new page, how would I get this to translate to a new page in your object model

Also is there a way to set the document to say 200% 150% etc so that when the pdf document is opened it will display that way.

Dear Bob,

Thanks for your consideration.

1.Each paragraph has a property named IsFirstParagraph. If that property is set to true, the paragraph will be forced to displayed in a new page. So you can change your code like the following:

[C#]

char[] spliter = new Char[]{(char)12};
string[] strs;
FileStream fstream = new FileStream(@“E:\Projects\CSharp\Pdf-bak\test.txt”,FileMode.Open);
StreamReader sreader = new StreamReader(fstream);

Pdf pdf1 = new Pdf(@“e:\projects\CSharp\customer\Aspose.Pdf.lic”);
Section sec1 = pdf1.Sections.Add();

sec1.Paragraphs.Add(new Text(""));

string curLine = sreader.ReadLine();
while(curLine != null)
{
strs = curLine.Split(spliter);
if(strs.Length > 1)
{
sec1.Paragraphs.Add(new Text(strs[0]));
for(int i = 1; i < strs.Length; i++)
{
Text newText = new Text(strs[i]);
newText.IsFirstParagraph = true;
sec1.Paragraphs.Add(newText);
}
}
else
sec1.Paragraphs.Add(new Text(curLine));
curLine = sreader.ReadLine();
}

pdf1.Save(“e:/temp/test.pdf”);
sreader.Close();
fstream.Close();

[VisualBasic]
Dim spliter() As Char = New Char() {Microsoft.VisualBasic.ChrW(12)}

Dim strs() As String
Dim fstream As FileStream = New FileStream(“E:\Projects\CSharp\Pdf-bak\test.txt”, FileMode.Open)
Dim sreader As StreamReader = New StreamReader(fstream)

Dim pdf1 As Pdf = New Pdf(“e:\projects\CSharp\customer\Aspose.Pdf.lic”)
Dim sec1 As Section = pdf1.Sections.Add()

sec1.Paragraphs.Add(New Text(""))

Dim curLine As String = sreader.ReadLine()
While Not curLine Is Nothing
strs = curLine.Split(spliter)
If strs.Length > 1 Then
sec1.Paragraphs.Add(New Text(strs(0)))
Dim i As Integer
For i = 1 To strs.Length - 1 Step 1
Dim NewText As Text = New Text(strs(i))
NewText.IsFirstParagraph = True
sec1.Paragraphs.Add(NewText)
Next
Else
sec1.Paragraphs.Add(New Text(curLine))
End If
curLine = sreader.ReadLine()
End While

pdf1.Save(“e:/temp/test.pdf”)
sreader.Close()
fstream.Close()
  1. As to you second question, setting the zoom rate is not supported. But you can try to use the document’s DestinationType which can set the document to fit width,fit page,etc…
    (I have just fixed a bug about DestinationType, so please download hotfix 1.6.1 if you want to use it.)

Hi Tommy,
The code segments you have provided are very helpfull. I have been trying to get the font size set to 12 and courier new with no luck. I am using the following code with no success.

While Not curLine Is Nothing
strs = curLine.Split(spliter)
If strs.Length > 1 Then
sec1.Paragraphs.Add(New Text(strs(0)))
Dim i As Integer
For i = 1 To strs.Length - 1 Step i + 1
Dim NewText As Text = New Text(strs(i))
NewText.IsFirstParagraph = True
NewText.TextInfo.FontSize = 12
NewText.TextInfo.FontName = “Courier New”
sec1.Paragraphs.Add(NewText)
Next
Else
Dim NewText As Text = New Text(curLine)
NewText.TextInfo.FontSize = 12
NewText.TextInfo.FontName = “Courier New”
sec1.Paragraphs.Add(NewText)
'sec1.Paragraphs.Add(New Text(curLine))
End If
curLine = sreader.ReadLine()
End While

I also want text to be left justified and need to set width and height so that when paging in Acrobat the next page appears properly. I could email you the file if it would be easier so we would not be going back and forth. How would I email you a zipped file? The initial consideration for the purchase of your pdf product is to transfer our print files into pdf format.
Thanks,
Bob

Dear Bob, please send it to pdf@aspose.com.

Dear Bob,

Thanks for your consideration.

Here is the new code:

Dim spliter() As Char = New Char() {Microsoft.VisualBasic.ChrW(12)}

Dim strs() As String
Dim fstream As FileStream = New FileStream(“E:\Projects\CSharp\customer\Bob\bobm_13018_19872_F5_9723_P\bobm_13018_19872_F5_9723_P”, FileMode.Open)
Dim sreader As StreamReader = New StreamReader(fstream)

Dim pdf1 As Pdf = New Pdf(“e:\projects\CSharp\customer\Aspose.Pdf.lic”)
pdf1.DestinationType = DestinationType.FitHeight
Dim sec1 As Section = pdf1.Sections.Add()
sec1.PageInfo.PageWidth = 1200
sec1.PageInfo.PageHeight = 1500

Dim info As TextInfo = New TextInfo()
info.FontSize = 12
info.FontName = “Courier New”
info.Alignment = AlignmentType.Justify

Dim curLine As String = sreader.ReadLine()
While Not curLine Is Nothing
strs = curLine.Split(spliter)
If strs.Length > 1 Then
sec1.Paragraphs.Add(New Text(strs(0)))
Dim i As Integer
For i = 1 To strs.Length - 1 Step 1
Dim NewText As Text = New Text(strs(i), info)
NewText.IsFirstParagraph = True
sec1.Paragraphs.Add(NewText)
Next
Else
sec1.Paragraphs.Add(New Text(curLine, info))
End If
curLine = sreader.ReadLine()
End While

pdf1.Save(“e:/temp/test.pdf”)
sreader.Close()
fstream.Close()

Hi Tommy,
Just what I was looking for, works great.
I am sure other things will come up as I continue evaluation.
Thanks for your prompt help,
Bob