Link to home
Start Free TrialLog in
Avatar of jrthurler
jrthurlerFlag for Afghanistan

asked on

Generating PDF files from .NET CF

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
Avatar of alexey_gusev
alexey_gusev
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of jrthurler

ASKER

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:

https://www.experts-exchange.com/questions/27681476/Change-fields-in-a-Word-Doc-in-a-WM-6-5-app.html

Regards,

JR
how about PDFSharp or iTextSharp?
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
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
Avatar of hjgode
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
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
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
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
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
ASKER CERTIFIED SOLUTION
Avatar of hjgode
hjgode
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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