locked
C# - Convert a .DOC to a .PDF - or write out html to a .PDF file RRS feed

  • Question

  • User649140458 posted

    Hi all,


    I am trying to create .PDF files on the fly but so far the results are not what I was looking for. Right now I have an application that creates .DOC files by using a stringbuilder and appending data and the write in out the .DOC file. THis works and looks fine when viewed from MS word.

    I have tried using the simular code to output to a .PDF file but the results are just HTML source being displayed in the .PDF file.

    1. Are there open source or a simple way to write out HTML to a .PDF and have it actually render like it does in a .DOC file?


    2. Is there a way to simple convert a .DOC file to a .PDF file in ASP.NET with C#?


    I did use itextsharp.dll a bit but it will show the raw HTML in the .PDF and not format it like it does in the .DOC.


    EXAMPLE CODE:


            Document PDFdoc = new Document(PageSize.LETTER);

    PdfWriter.GetInstance(PDFdoc, new FileStream(strFileSaveData, FileMode.Create));

    PDFdoc.Open();



    strPDFDocument.Append("<table style='width: 430px' cellspacing='5' cellpadding='5'>" + strNL.ToString());
    strPDFDocument.Append("<tr>" + strNL.ToString());
    //strPDFDocument.Append("<td colspan='2' class='name'><strong>" + strName + "</strong></td>" + strNL.ToString());
    //strPDFDocument.Append("<td colspan='2' class='name'><input id='" + strID + "' type='radio' name='Users' value='" + strID + "' /><strong>" + strName + "</strong></td>" + strNL.ToString());


    strPDFDocument.Append("<td class='name' colSpan='2'>" + strNL.ToString());
    strPDFDocument.Append("<input id='" + Session["ows_ID"] + "' value='" + Session["ows_ID"] + "' type='radio' name='Users'>" + strNL.ToString());
    strPDFDocument.Append("<strong><a target='blank' href='http://www.sharepointsite.net/_layouts/userdisp.aspx?ID=" + Session["ows_ID"] + "&Source=http://www.sharepointsite.net/_layouts/people.aspx?MembershipGroupId=0&FilterField1=ContentType&FilterValue1=Person'>" + Session["ows_Title"] + "</a></strong> - " + "n/a" + strNL.ToString());
    strPDFDocument.Append("</td>" + strNL.ToString());

    strPDFDocument.Append("</tr>" + strNL.ToString());
    strPDFDocument.Append("<tr>" + strNL.ToString());
    strPDFDocument.Append("<td style='width: 30px' class='data'><strong>Job Title:</strong></td>" + strNL.ToString());
    strPDFDocument.Append("<td style='width: 400px'>" + Session["ows_JobTitle"] + "</td>" + strNL.ToString());
    strPDFDocument.Append("</tr>" + strNL.ToString());
    strPDFDocument.Append("<tr>" + strNL.ToString());
    strPDFDocument.Append("<td style='width: 30px' class='data'><strong>Email:</strong></td>" + strNL.ToString());
    strPDFDocument.Append("<td style='width: 400px'><a href='mailto:" + Session["ows_EMail"] + "'>" + Session["ows_EMail"] + "</a></td>" + strNL.ToString());
    strPDFDocument.Append("</tr>" + strNL.ToString());
    strPDFDocument.Append("<tr>" + strNL.ToString());
    strPDFDocument.Append("<td style='width: 30px' class='data'><a href='http://www.sharepointsite.net/Lists/Ripple%20Effect%20Contact%20List/DispForm.aspx?ID=" + "strStaffID" + "&Source=http://www.sharepointsite.net/Lists/Ripple%20Effect%20Contact%20List/AllItems.aspx' target='_blank'><img border='0' src='images/yahoo_icon.gif' width='25' height='25'></a></td>" + strNL.ToString());
    strPDFDocument.Append("<td style='width: 400px'><font color='#800080'>" + "n/a" + "</font></td>" + strNL.ToString());
    strPDFDocument.Append("</tr>" + strNL.ToString());
    strPDFDocument.Append("<tr>" + strNL.ToString());
    strPDFDocument.Append("<td style='width: 30px' class='data'><strong>Department:</strong></td>" + strNL.ToString());
    strPDFDocument.Append("<td style='width: 400px'>" + Session["ows_Department"] + "</td>" + strNL.ToString());
    strPDFDocument.Append("</tr>" + strNL.ToString());
    strPDFDocument.Append("<tr>" + strNL.ToString());
    strPDFDocument.Append("<td style='width: 30px' class='data'><strong>Account:</strong></td>" + strNL.ToString());
    strPDFDocument.Append("<td style='width: 400px'>" + Session["ows_Name"] + "</td>" + strNL.ToString());
    strPDFDocument.Append("</tr>" + strNL.ToString());
    strPDFDocument.Append("</table>" + strNL.ToString());





    string text = strPDFDocument.ToString();



    // Add a basic paragraph
    PDFdoc.Add(new Paragraph(text));


    PDFdoc.Close();








    OUTPUT in PDF FILE:
    <table style='width: 430px' cellspacing='5' cellpadding='5'>
    <tr>
    <td class='name' colSpan='2'>
    <input id='' value='' type='radio' name='Users'>
    <strong><a target='blank'

    more............................



    It just shows up as RAW HTML source in the .PDF



    I am interested in Either Writing out PDF's dynamically or Converting from .DOC to PDF.


    If you have any tips or code examples or comments it would be greatly appreciated.

    Thanks







    Wednesday, August 25, 2010 11:27 AM

All replies

  • User1511415904 posted

    I have a similar issue.  Did you ever figure this issue out?

    Tuesday, October 4, 2011 9:29 AM