PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Text file to pdf error
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3415
Page 1 of 1

Author:  jaychabria [ Sun Aug 07, 2016 12:22 pm ]
Post subject:  Text file to pdf error

Hi,

I am reading a text file (file size 260kb) and creating the document but the pdf file (size created is 27kb) gives the error 'error on page, please contact creator'. Also my txt file has 3000 lines but the pdf output file size is very small. Can we also specify A4 or A3 paper size and orientation?

My code is as follows:

Dim theText As String
theText = File.ReadAllText("C:\Viacom\txtToPdf\DAY_6.txt")
'theText = File.ReadAllText(fileToConvert)
Try
Dim document As New PdfDocument()
document.Info.Title = "Created with PDFsharp"
document.Info.Author = "AutoPdf Test"
Dim page As PdfPage = document.AddPage()
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
Dim font As XFont = New XFont("Verdana", 10, XFontStyle.Regular)
gfx.DrawString(theText, font, XBrushes.Black, New XRect(0, 0, page.Width, page.Height), XStringFormats.TopLeft)
Dim pdfFilename As String = "C:\Viacom\txtToPdf\DAY_6.pdf"
'Dim pdfFilename As String = foutput & ".pdf"
document.Save(pdfFilename)
document.Close
Catch ex3 As Exception
Dim objdelg3 As New delg_TxtChange(AddressOf ChangeTxtText)
objdelg3.BeginInvoke(txtStatus, "Error: " & ex3.ToString, Nothing, Nothing)
End Try

There are also no samples of migradoc in vb.net in the source zip file.

Thanks,
Jai

Author:  TH-Soft [ Sun Aug 07, 2016 5:57 pm ]
Post subject:  Re: Text file to pdf error

Hi!
jaychabria wrote:
There are also no samples of migradoc in vb.net in the source zip file.
There are only C# developers in the team.
The namespaces are the same, the classes are the same, with VB.NET the syntax is slightly different.
I can read VB.NET samples, but cannot write state-of-the-art VB.NET samples. I hope you can read C# samples.

jaychabria wrote:
My code is as follows
Your code will add the whole text onto a single page, maybe even a single line.

If you want to stick to PDFsharp, look at the XTextFormatter class. You'll need extra code that takes care of page breaks.
http://pdfsharp.net/wiki/TextLayout-sample.ashx

Or try MigraDoc to get automatic page breaks as needed. I'd recommend MigraDoc.

jaychabria wrote:
Can we also specify A4 or A3 paper size and orientation?
Yes, sure. Check the properties of PdfPage (PDFsharp) or Section.PageSetup (MigraDoc).
http://pdfsharp.net/wiki/PageSizes-sample.ashx

Author:  jaychabria [ Mon Aug 08, 2016 7:25 pm ]
Post subject:  Re: Text file to pdf error

I would like to use Migra Doc. So can I use the below code to add the entire text file as string in paragraph?

Not that good at reading the C samples.

New code:

'vb part
Dim theText As String
theText = File.ReadAllText("C:\Viacom\txtToPdf\DAY_6.txt")

'C part
Document document = new Document();
Section section = document.AddSection();
section.PageSetup = document.DefaultPageSetup.Clone();
section.PageSetup.PageWidth = "32cm";
section.PageSetup.PageHeight = "22cm";
section.AddParagraph(theText);
section.AddParagraph();
Paragraph paragraph = section.AddParagraph();
paragraph.Format.Font.Color = Color.FromCmyk(100, 30, 20, 50);
paragraph.AddFormattedText("Hello, World!", TextFormat.Underline);
FormattedText ft = paragraph.AddFormattedText("Small text",TextFormat.Bold);
ft.Font.Size = 6;
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(false,
PdfFontEmbedding.Always);
pdfRenderer.Document = document;
pdfRenderer.RenderDocument();
string filename = "HelloWorld.pdf";
pdfRenderer.PdfDocument.Save(filename);

-----------------

Will test the code. Also just wanted to know why the pdf file gave a not opening error.

Thanks,
Jai

Author:  TH-Soft [ Mon Aug 08, 2016 10:03 pm ]
Post subject:  Re: Text file to pdf error

Hi!

To add a string to a section, just call
Code:
section.AddParagraph(text);

Maybe you get better results when you add each line of the file as a separate paragraph.

jaychabria wrote:
Also just wanted to know why the pdf file gave a not opening error.
Without the text file and without the PDF file I could speculate only.
Could be control characters in the text file, could be a bug in PDFsharp, could be something completely different.

C# is case-sensitive.
"Document document = new Document();" becomes something like
"Dim myDocument as Document = New Document()".
Skip Color and AddFormattedText to get a basic start.

See this for a VB.NET sample:
viewtopic.php?p=9426#p9426

Author:  jaychabria [ Wed Aug 10, 2016 12:49 pm ]
Post subject:  Re: Text file to pdf error

Hi,

I tried the code and the pdf is generating however the text formatting is lost and the margins are also big.

Code:

Document document = new Document();
Section section = document.AddSection();
section.PageSetup = document.DefaultPageSetup.Clone();
//section.PageSetup.PageWidth = "42cm" 'A3
//section.PageSetup.PageHeight = "30cm" 'A3
section.PageSetup.PageWidth = "32cm"; //A4
section.PageSetup.PageHeight = "22cm"; //A4
section.AddParagraph(theText);
section.AddParagraph();
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(false, PdfFontEmbedding.Always);
pdfRenderer.Document = document;
pdfRenderer.RenderDocument();
string pdfFilename = "C:\\Viacom\\txtToPdf\\05080216.pdf";
pdfRenderer.PdfDocument.Save(pdfFilename);


Attached the text file and pdf file.

Thanks,
Jai

Attachments:
pdf output.jpg
pdf output.jpg [ 183.01 KiB | Viewed 9677 times ]
txt source.jpg
txt source.jpg [ 148.55 KiB | Viewed 9677 times ]

Author:  Thomas Hoevel [ Wed Aug 10, 2016 1:01 pm ]
Post subject:  Re: Text file to pdf error

jaychabria wrote:
I tried the code and the pdf is generating however the text formatting is lost and the margins are also big.
Ah, formatting using the typewriter style.

For that type of formatting, you have to use a fixed-width font (like Courier New) and you have to replace all spaces with non-breaking spaces (U+00A0).
When you use this hack, MigraDoc will not break lines for you. Make sure the page width is large enough (or the font size small enough).

The margins have the default values (25 mm IIRC) until you set different margins. Set them to 0 or to 5 mm or whatever you like.

Author:  jaychabria [ Wed Aug 10, 2016 6:25 pm ]
Post subject:  Re: Text file to pdf error

Thanks,

Will check this over the week end

Author:  jaychabria [ Fri Aug 19, 2016 6:32 am ]
Post subject:  Re: Text file to pdf error

Thanks, was on vacation. After replacing to non breaking spacing char(160) and courier font the alignment is coming properly.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/