Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I'm working on small project and one of the things that application needs to do, is to generate one document which would need to contain lot's of text, also some graphs, couple of images and some tables.

Main concern is that application can save that document in PDF and in some other editable format(doc, rtf, OpentText).

I first found that Word Automation could serve purpose, but problem is that I can't know would Word be installed on all computers where application is running so basically I can't use that solution.

Then I found this solution:
http://aodl.sourceforge.net/download.html[^]

And also this one:
http://www.pdfsharp.net/wiki/MigraDocArticles.ashx[^]

So what do you think, which one is better and simpler to use for my purpose?

Thanks in advance!

Josip
Posted
Updated 8-Dec-14 11:26am
v2
Comments
Andreas Gieriet 8-Dec-14 17:41pm    
What do you think is best?
Why would you want to integrate a fully blown text processor into your application? Why do all that hassle and not ask for the document only to process (whatever you want to "process")? The user could edit in his editor of choice and you do the reset...
Cheers
Andi

you can use iTextsharp pdf creator. just download the library ie itextsharp dll and add it to your references from sourceforge and use this code

note u have to edit where not applicable like connection strings, tablenames, variables, namespace, classnames. also note this code was implemented using C# in asp.net

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.rtf;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Collections;
using System.IO;



namespace report
{
   
    public partial class launch : System.Web.UI.Page
        
    {
        string shifty,checky;
        conex conexion = new conex();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            

        }
        
        protected void Button1_Click(object sender, EventArgs e)
        {


        }

        protected void cmdcancel_Click(object sender, System.EventArgs e)
        {
            Response.End();
            this.Dispose();
        }
        // footer class code
    public partial class Footer : PdfPageEventHelper

    {

    public override void OnEndPage(PdfWriter writer, Document doc)

    {

        Paragraph footer = new Paragraph("MY FOOTER DESCRIPTION HERE", FontFactory.GetFont(FontFactory.TIMES, 10, iTextSharp.text.Font.NORMAL));

    footer.Alignment = Element.ALIGN_JUSTIFIED;

    PdfPTable footerTbl = new PdfPTable(1);

    footerTbl.TotalWidth = 600;

    footerTbl.HorizontalAlignment = Element.ALIGN_JUSTIFIED;

    PdfPCell cell = new PdfPCell(footer);

    cell.Border = 0;

    cell.PaddingLeft = 10;

    footerTbl.AddCell(cell);

    footerTbl.WriteSelectedRows(0, -1, 10, 30, writer.DirectContent);

        }

    }
    // end of footer class code


// pdf creation function


        private void chkcreate()
        {
            var titleFont = FontFactory.GetFont("Arial", 12, Font.NORMAL);
            var titleFontx = FontFactory.GetFont("Arial", 12, Font.NORMAL,Color.RED);
            var titleFont2 = FontFactory.GetFont("Arial", 12, Font.BOLD);
            var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/imagename.gif"));
            logo.SetAbsolutePosition(400, 795);
            
            
            


            Document pdfDoc = new Document(PageSize.A4, 50, 50, 35, 25);
            
            // tasks table creation            
            PdfPTable table = new PdfPTable(5);

            //actual width of table in points

            table.TotalWidth = 535f;

            //fix the absolute width of the table

            table.LockedWidth = true;



            //relative col widths in proportions - 1/3 and 2/3

            float[] widths = new float[] { 2f, 13f,1.5f,3f,3f};

            table.SetWidths(widths);

            table.HorizontalAlignment = 0;

            //leave a gap before and after the table

            table.SpacingBefore = 5f;

            table.SpacingAfter = 5f;



            PdfPCell cell = new PdfPCell(new Phrase());

            cell.Colspan = 0;

            cell.Border = 0;

            cell.HorizontalAlignment = 1;

            table.AddCell("TaskID");
            table.AddCell("TASK DESCRIPTION");
            
            table.AddCell("TIME");
            table.AddCell("MAKER");
            table.AddCell("Comments*");

		////Get the data from database into datatable
            string connect = "Data Source=MACHINEname\\instancename;Initial Catalog=DatabaseName;User ID=yourloginID;Password=yourPassword";
           
            using (SqlConnection conn = new SqlConnection(connect))
            {

                string query = "select  bbbbb,ffffffff,CONVERT(VARCHAR(5),time),eeeee,xxxxx FROM mytablename ORDER BY xxxxxx";

                SqlCommand cmd = new SqlCommand(query, conn);

                try
                {

                    conn.Open();

                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {

                        while (rdr.Read())
                        {

                            table.AddCell(rdr[0].ToString());
                            table.AddCell(rdr[1].ToString());
                            table.AddCell(rdr[2].ToString());
                            table.AddCell(rdr[3].ToString());
                            table.AddCell(rdr[4].ToString());

                        }

                    }

                }

                catch (Exception ex)
                {

                    Response.Write(ex.Message);

                }
            }

                
                PdfPTable tab2 = new PdfPTable(3);

            //actual width of table in points

            tab2.TotalWidth = 530f;

            //fix the absolute width of the table

            tab2.LockedWidth = true;



            //relative col widths in proportions - 1/3 and 2/3

            float[] width2 = new float[] { 1.2f, 14f,3f};

            tab2.SetWidths(width2);

            tab2.HorizontalAlignment = 0;

            //leave a gap before and after the table

            tab2.SpacingBefore = 5f;

            tab2.SpacingAfter = 5f;



            PdfPCell cell2 = new PdfPCell(new Phrase());

            cell2.Colspan = 0;

            cell2.Border = 0;

            cell2.HorizontalAlignment = 1;

            tab2.AddCell("Task");
            tab2.AddCell("TASK DESCRIPTION");
            tab2.AddCell("Attachment");



            ////Get the data from database into datatable

            using (SqlConnection connx = new SqlConnection(connect))
            {

                string queryx = "select xxxx,aaaaa,bbbbb,vvvvv FROM tablename ORDER BY vvvvvvvv";

                SqlCommand cmdx = new SqlCommand(queryx, connx);

                try
                {

                    connx.Open();

                    using (SqlDataReader rdrx = cmdx.ExecuteReader())
                    {

                        while (rdrx.Read())
                        {

                            tab2.AddCell(rdrx[0].ToString());
                            tab2.AddCell(rdrx[1].ToString());
                            tab2.AddCell(rdrx[2].ToString());
                            

                        }

                    }

                }

                catch (Exception ex)
                {

                    Response.Write(ex.Message);

                }
            }

            
            Response.ContentType = "application/pdf";
            
            Response.AddHeader("content-disposition", "attachment;filename=myfilename.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            
            StringReader sr = new StringReader(sw.ToString());
            
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
           
            writer = PdfWriter.GetInstance(pdfDoc, new FileStream( Server.MapPath ("~\\C:\\myfoldername\\myfilenane.pdf"), FileMode.Create));
            

           

            pdfDoc.Open();
            pdfDoc.Add(logo);
            pdfDoc.Add(new Paragraph("Title        :" + shifty +" myfilename", titleFont));
            pdfDoc.Add(new Paragraph("Business Date :" + txtdate.Text, titleFont));
            pdfDoc.Add(new Paragraph(" ", titleFont));
            pdfDoc.Add(table);
            pdfDoc.Add(new Paragraph("TITLE 1:", titleFont2));
            pdfDoc.Add(tab2);
            pdfDoc.Add(new Paragraph("[  CHECKED BY...... " + checky +"]", titleFontx));
            //now inserting footer
            writer.PageEvent = new Footer();
            pdfDoc.Add(logo);
            // end of footer
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();
          
                   
            

            
        }

        protected void cal1_SelectionChanged(object sender, System.EventArgs e)
        {
            
  
        }

        protected void cmdcreate_Click(object sender, EventArgs e)
        {
            chkcreate();
        }

      

        
    }
}
 
Share this answer
 
v2
Comments
Andreas Gieriet 8-Dec-14 19:28pm    
Just put the code into a pre-formatted block.
Andi
Andreas Gieriet 8-Dec-14 19:35pm    
Be aware (beware! ;-)) of the AGPL license terms!
Cheers
Andi
If you have the possibility to first generate an XML document, you could then use XSLT to transform the data into various other formats, such as HTML, PDF, Word, plain text and into another XML format.

It is a bit of a threshold to learn XSLT so it depends on your time frame, but it might be worth it in the long run as you have good control of what you are doing and, as far as I can tell, no licence issues.

Here is a good page to get an introduction to XSLT: XSLT Tutorial[^]

XSLT can be used to transform XML directly to HTML or plain text.
In order to create Word and PDF documents, you need to first use XSLT to transform the XML into XSL-FO and then use a tool to render the desired format.

Here you can get started with XSL-FO: XSL-FO Tutorial[^]

For a rendering tool you can look here:
The Apache™ FOP Project[^]
or here
Codeplex FO.NET[^]
 
Share this answer
 
Thank you both on answers. You gave me materials to study and make an final conclusion.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900