PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 2:18 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Text file to pdf error
PostPosted: Sun Aug 07, 2016 12:22 pm 
Offline

Joined: Sun Aug 07, 2016 12:12 pm
Posts: 5
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


Top
 Profile  
Reply with quote  
PostPosted: Sun Aug 07, 2016 5:57 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 08, 2016 7:25 pm 
Offline

Joined: Sun Aug 07, 2016 12:12 pm
Posts: 5
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


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 08, 2016 10:03 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Wed Aug 10, 2016 12:49 pm 
Offline

Joined: Sun Aug 07, 2016 12:12 pm
Posts: 5
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 8313 times ]
txt source.jpg
txt source.jpg [ 148.55 KiB | Viewed 8313 times ]
Top
 Profile  
Reply with quote  
PostPosted: Wed Aug 10, 2016 1:01 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Aug 10, 2016 6:25 pm 
Offline

Joined: Sun Aug 07, 2016 12:12 pm
Posts: 5
Thanks,

Will check this over the week end


Top
 Profile  
Reply with quote  
PostPosted: Fri Aug 19, 2016 6:32 am 
Offline

Joined: Sun Aug 07, 2016 12:12 pm
Posts: 5
Thanks, was on vacation. After replacing to non breaking spacing char(160) and courier font the alignment is coming properly.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 128 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group