Generating PDF files from .NET CF

jrthurler
jrthurler used Ask the Experts™
on
I have a program developed in VS.2008, C#, .NET 3.5.
I´m using Resco Mobile Forms Toolkit controls too.

I need now to generate a PDF report directly from my WM 6.5 HTC HD2 device. Is it possible to generate PDF files from my device ?

Regards,

JrT
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
it depends on your needs and money :)

eg. http://www.componentsource.com/products/pdf4net/index-gbp.html would give you that for some £300.

alternatively, you have PDFSharp: http://sourceforge.net/projects/pdfsharp/

or itextsharp (not sure if both support cf.net, but you can get the code so it's possible)
http://sourceforge.net/projects/itextsharp/?source=recommended

Author

Commented:
Hi Alexey. I tried to do it using PDF4NET but besides the price, in the Compact Framework edition the resources listed in the Features list from the producer I could´nt achieve what I was want.

I post a new question in the EE asking about using a Word document and change the value of some fields in this word document instead of print in PDF format. If you want to see my detailed question, here is the link:

http://www.experts-exchange.com/Programming/Handhelds_-_PDAs/Windows_Mobile/Q_27681476.html

Regards,

JR
how about PDFSharp or iTextSharp?
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Author

Commented:
PDFSharp doesn´t have any comments about  Compact Framework on the web site. I will try to send an email to ask this question.
just get the source and try to add CF configs - I wouldn't expect it to compile straight away, but it shouldn't be hard to make necessary changes I guess

Author

Commented:
In the PDFSharp forum I found some information about using PDFSharp on CF but the guy who try to do it had some troubles of NullExceptions error. No one have posted a solution on the forum for this issue.

The problem with PDFSharp is the technical support that seems to no exists.

I will continue to search for component that could fix my problem.

Regards,

Jr
hjgodeTier 3 Senior Technical Support Engineer

Commented:
Hi JrT

I already migrated iTextSharp to Compatc Framework. See my article at: http://www.hjgode.de/wp/2009/10/31/itextsharp-running-on-compact-framework-windows-mobile/

I had to remove some xml stuff (I dont needed it) and it is based on iTextSharp version 4.01, but that should be sufficiant for your needs. Full source and demo available at the above article.

Regards

Josef

Author

Commented:
Josef, thanks for your help.

Do you believe that I can make the report (in the attached file) in my device ? I don´t know if it will be possible because this report was made in my desktop application.
Comissionamento-S-LT-PX-MDG00-C4.pdf
hjgodeTier 3 Senior Technical Support Engineer

Commented:
Of course you can make this report using iTextSharpCF. But it will be lot of calculating and positioning texts and lines. When you create a pdf, you have to drwa on the 'pages' as you would use grpahics class members like DrawText, DrawLine, DrawRectangle etc.
Possibly you should start with the text, maybe using a fixed space font like courierNew. Using a variable spaced font would need additional calculating of the draw text widths.

If you are good in doing these calaculations, it will be a not to hard coding.

regards

Josef
hjgodeTier 3 Senior Technical Support Engineer

Commented:
Hello again

I have to correct my last answer, it is very simple to create your pdf doc, if you are able to encode it to html first. Then let iTextSharp convert the html to PDF.

here is a simple sample:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace html2pdf
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			Console.WriteLine ("Hello World!");
			testHtml();
		}
		public static void testHtml(){
			String htmlText = "<font  " +
			" color=\"#0000FF\"><b><i>Title One</i></b></font><font   " +
			" color=\"black\"><br><br>Some text here<br><br><br><font   " +
			" color=\"#0000FF\"><b><i>Another title here   " +
			" </i></b></font><font   " +
			" color=\"black\"><br><br>Text1<br>Text2<br><OL><LI><DIV Style='color:green'>Pham Duy Hoa</DIV></LI><LI>how are u</LI></OL><br/>"+
			"<table border='1'><tr><td style='color:red;text-align:right;width:20%'>123456</td><td style='color:green;width:60%'>78910</td><td style='color:red;width:20%'>ASFAFA</td></tr><tr><td style='color:red;text-align:right'>123456</td><td style='color:green;width:60%'>78910</td><td style='color:red;width:20%'>DAFSDGAFW</td></tr></table><br/>"+
			"<div><ol><li>123456</li><li>123456</li><li>123456</li><li>123456</li></ol></div>";
	 
	        HTMLToPdf(htmlText, "./PDFfile.pdf");
		}
		public static void HTMLToPdf(string HTML, string FilePath)
	    {
	        Document document = new Document();
	 
	        PdfWriter.GetInstance(document, new FileStream(FilePath, FileMode.Create));
	        document.Open();
	        iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
	        iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
	        hw.Parse(new StringReader(HTML));
	        document.Close();
	    }
	}
}

Open in new window

the above code will output the attached pdf.

Let me know, if you have problems creating your doc as html.

regards

Josef
PDFfile.pdf

Author

Commented:
Hi Josef!

Your solution opened my mimd to an alternative. I have the report on my desktop computer. I opened this report using #Fields in the HTML code where I need to replace the #Fields with values provided by my app. After that, I exported this file to HTML code to be a Report Model.

With this report model, I change the marked fields by values passed by my device app. After that, the file was copied to a new file and my problem will be fixed!

Thanks for your help!

Regards,

JR
Tier 3 Senior Technical Support Engineer
Commented:
Hey

that sounds good, I found that iTextSharp has problems with colspan and rowspan in html tables if you go on and let iTextSharp (4.01) convert the html to a pdf.

The attached html is what i have generated so far using a self-made html class. Then compare this to the attached pdf, which was generated on a WinMo 6.5.3 device. It is not the same.

regards

Josef
aaa.htm
aaa.pdF

Author

Commented:
Josef,

I didn´t have time to implement the solution I told you before, but I think this way of replace the values directly on the HTML file will works good.

If the generated HTML works fine to be printed after copied from my smartphone, I don´t need to generate the PDF file!

Thank so much for your help. Today I guess I´ll have time to try to implement this solution. I let you know the results later!

Regards

JR

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial