How can I create new PDF files based on a template where some text will be replaced? - asp.net

I'm developing a CMS aplication in ASP .Net using WebForms and I'm looking for a way to create new PDF files based on a template.
This feature will be used to generate contracts where some placeholders will be replaced with the customer data.
What's the best approach to do that?
Edited: The templates will be static, the main content will never change from customer to customer, only some text in the beginning that will contain the placeholders to recive the customer data. The catch is that I must allow the owner of the application to upload new templates in PDF, with the predetermined placeholders in it to allow the replacement to occur.

I can think of two approaches depending on what type of template you are looking at:
1) Static Template - Say the template does not change with the data (Ex.some standard compliance form etc)
You can try something like iTextSharp, where you have your templates defined in your .net code, and you just "plug in" the relevant data and render the PDF via iTextSharp.
2) Dynamic Templates - Say your template isn't standard and is user customizable. In this case, I'd say go for HTML for designing and "print" the for to PDF. There are many components available.
You may also want to try out components like crystal reports.

I'd go with static HTML templates containing substitution tokens e.g. {FullName} which you can then replace with your data. Once you have created an HTML file like this, say in a StringBuilder, you can use PrinceXML or ABCPdf.net (www.websupergoo.com) to transform your HTML into a PDF.

I hope I am understaning this correctly but wouldn't using standard PDF Form Fields give you the functionality and features you require. Load the PDF template into Adobe Acrobat Full or Foxit Phantom and define standard PDF Form Fields on top of the PDF. Each field has a name, position, font etc.
Then just use a standard PDF library to fill in the PDF form fields and 'flatten' the form fields if that is required.
This solution allows the customer to design new PDF layouts and so long as they define form fields with names that match up then you will be able to drop in the replacement form with a simple file copy.
The iText or iTextSharp library should be able to do all of this quite easily. If not them there are many PDF libraries out there that can.

Related

How to see tags in a Powerpoint presentation

I am working for an office-add-in for PowerPoint. I need to assign some unique identifier to my files, so that files can be identified in any dot net application. I did similar work for Word using custom properties. But for PowerPoint there is no way to read/ write custom property using office.js.
The only way I found using tags:
https://learn.microsoft.com/en-us/office/dev/add-ins/powerpoint/tagging-presentations-slides-shapes
but when I add tags to the presentation, I am not able to see those tags in presentation directly, I am able to read/ write through code only. Also I am not getting a way to read these tags from dot net application.
Any help will be great.
I am storing my files to azure blob. And reading files in my dot net core application to identify whether it has been saved from an office-add in or not. I am using syncfusion library in dot net core application to work with files.
Try this:
Open a PPT file and add a few tags "wwwww", "yyyyy", "zzzzz".
Close and save the file.
Add ".zip" onto the end of the filename.
Use any unzipper program to unzip the file.
Search the folder of unzipped files for "wwwww", "yyyyy", "zzzzz".
This should tell you where/how tags are stored in the OOXML.
Your .NET app should be able to use the OOXML SDK to read the tags of a PPT file.
At present, Syncfusion Presentation library do not have support to read and edit the tags of PowerPoint elements. Please track the status of this feature from below link,
https://www.syncfusion.com/feedback/1800/create-and-edit-tags-for-powerpoint-elements
However, kindly try the below suggested workaround solutions to achieve your requirement,
Using Shape.Name property:
Add a shape with unique name while generating a document from Office-Addin. You can use Shape.Name property for this.
In .NET Core application, identify the corresponding shape by using Shape.ShapeName property of Syncfusion Presentation library and decide whether its generated by Office-Addin or not. You can refer below UG documentation for more details,
https://help.syncfusion.com/file-formats/presentation/working-with-shapes#specifying-shape-properties
Using custom meta-data on Presentation:
PowerPoint Javascript API documentation states that, when we apply the tags for Presentation object it’s maintained as a custom property of PowerPoint document.
Screenshot
If so, you can able to iterate the custom property of PowerPoint document using Presentation.CustomDocumentProperties API of Syncfusion Presentation library. You can refer below UG documentation for more details,
https://help.syncfusion.com/file-formats/presentation/working-with-powerpoint-presentation#adding-custom-document-properties
Note: This update is from Syncfusion team

Reading a HTML resource file using asp .net

I am trying to write a windows services that will generate email based on HTML templates. I want my HTML templates to be included in my project as resources. Do i have read these these template files as using filestreams or are there built in C# functions to read these files as resources.
Is there better way to use HTML files as templates for generating emails in asp .net
Thanks
SCS
You just need to create resource file (.resx), then fill it in key/value manner like key = "email_template", value = "your html template". And after you will have the class with the field called email_template, and it will provide you your html template without any additional coding.
UPD
O, also you could save your template like asp.net user control (*.ascx) and then render it to string with technique described in this article
The same variant possible in MVC world. It could be done this way

How to convert a html table to downloadable excel file ASP.NET C#

I need a function to convert any HTML table to a excel file!
I like to add a button to my page and once that button is clicked I pass a complete HTML table in to such a function that will generate an excel file and past the html table into the file with the same layout and design!
I appreciate any help with this request.
I am using c# and asp.net
This is a nasty trick, but it works in recent version of excel.
You just save the html table as a normal html file, then just rename it to xls. If your application is a web application use response.setHeader("Content-Disposition", "attachment; filename=order.xls").
Recent version of excel support Microsoft Office XML Formats, so if the previous solution doesn't work, you may want to try to generate the xml from the html table.
See XML Spreadsheet Reference.
What I do, is to generate the xml from excel in the xml format, then hack into it. You can implement also advanced features like automatic filters, and cool layouts. I used a template engine to generate the xml (Freemarker, it's Java stuff, but I'm sure there are template engines also for .NET)

A "shortcut" method for exporting an ASP.Net Page to pdf/xls

I want to export a few Pages to pdf/xls. By Pages I mean as the eye sees it - a screenshot of the Page's contents. I know how to build pdf/xls documents using 3rd party tools but is there any way to quickly export the rendered contents of say a Panel?
edit: maybe a tool that can render the page's output as a browser would, and save it as an image file?
There is an open source console program named wkhtmltopdf which you could call from asp.net to convert the page. It can convert to PDF or an image with wkhtmltoimage (JPG, PNG, etc.) using the webkit rendering engine.
Check my answer to this question to see an example of how to convert from a html to a pdf using C#:
Easiest way of porting html table data to readable document
I can recommend http://www.screengrab.org/ for firefox.

Telerik RadEditor memorystream/string save to RTF

The required functionality I am aiming for is to pull out RTF content from a database, edit it through a web interface (with a WYSIWYG editor) and then place the modified text back in to the database (in RTF format).
The control that I am using to do this is Telerik RadEditor (we have a license already for these controls). In the most recent version there appears to be functionality to load in RTF content from a string or a stream, but the only method I can see that is exposed for getting RTF back out is exportToRTF(); this method modified the headers and allows you to save a RTF version of the content you have just edited as a file.
The functionality to convert from HTML to RTF must exist somewhere within their library as you can export a RTF file, but I can not find any publicly exposed methods to pass this in to a stream or a string.
Does anybody know of a way that I can convert the HTML back to RTF using the Telerik libraries without saving out to a file?
Thanks
I think the methods that do the actual conversion are not publicly available in the Telerik assembly, so your only choice is to use the built-in export functionality. You can add a handler for the ExportContent event. There you will have access to the content that will be saved to the file before the actual operation.
Since the export call changes the response of the current page request, you can create a new dummy page, add an editor there, set its .Content property to the content you wish to export, attach a handler to ExportContent and finally call the export method of the editor.

Resources