xyzio

Posts Tagged ‘itextsharp

Return pdf to browser in a C# razor page using itext7

leave a comment »

Sample code to return a PDF file to the browser in a Razor c# page using itext7.


using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using System.IO;

public IActionResult OnPostManipulatePdf(String dest)
{
    MemoryStream ms = new MemoryStream();

    PdfWriter writer = new PdfWriter(ms);
    PdfDocument pdfDoc = new PdfDocument(writer);
    Document doc = new Document(pdfDoc);
    writer.SetCloseStream(false);

    Paragraph header = new Paragraph("header");

    doc.Add(header);
    doc.Close();

    byte[] byteInfo = ms.ToArray();
    ms.Write(byteInfo, 0, byteInfo.Length);
    ms.Position = 0;

    FileStreamResult fileStreamResult = new FileStreamResult(ms, "application/pdf");

    //Uncomment this to return the file as a download
    //fileStreamResult.FileDownloadName = "Output.pdf";

    return fileStreamResult;
}

Source:
https://stackoverflow.com/questions/1510451/how-to-return-pdf-to-browser-in-mvc

Written by M Kapoor

September 23, 2020 at 2:49 pm

Posted in c#, Programming

Tagged with , , ,

WinHost ASP.NET hosting review – Boring is Good

with 4 comments

It has been a year since I moved my website off Mono at Linode to WinHost and I’m being honest here – the hosting has been absolutely boring and that is a great thing!

After spending some time looking at ASP.NET hosts it came down to DiscountASP.NET, GoDaddy, and WinHost.  I ended up choosing WinHost because they offered database hosting in their base package, promise not to oversell, and I couldn’t find any bad reviews about them.  Looking at my other options – The web is littered with bad GoDaddy reviews and DiscountASP.NET doesn’t offer a MS SQL database (or any database) at the same WinHost price point.

Unlike hosting with Mono – I do not have dink around with arcane text files, there is a nice GUI interface to manage my site.  I can select my ASP.NET version, manage my databases, and do everything I need to manage my site through their interface.  My website and iTextSharp based BingoCard maker just work!  In addition, I’ve had no downtime or hosting issues since I’ve started hosting with them.  Perfectly boring!

Of course nothing is perfect – I’ve had a few a couple issues with them:
Their auto installer would only install WordPress in domain.com/wordpress.  I couldn’t choose another sub-directory like /blog or /diary.  However, I was able to manually install WordPress in a different directory on my own.

Another problem I’ve had is that I had trouble connecting to WordPress Jetpack through my install.  However it seems that they’ve now fixed the issue.

One last annoyance is that they don’t include Scheduled Tasks in their basic $5 package.  This means that I can’t set up tasks a-la-Cron like a Mono-on-Linux install would allow.

Overall I’m happy with my experience, perfectly boring ASP.NET hosting with database for just $5/month where everything Just Works!

Have other questions? Check out the WinHost forums!

Update 12/29/2014:

I’ve switched to using Mono at DigitalOcean on their base droplet.  I get 20GB of disk space and greater ability to run scripts or set up a database for just $5/month.  Also, WinHost changed their monthly price to $5.95/month with a 3-month pre-pay so DigitalOcean with Mono is cheaper, faster, and comes with more features.

I still highly recommend WinHost for beginners and for those that want no-hassle hosting.  Maintaining your own server does take time and sometimes requires technical troubleshooting.

Written by M Kapoor

June 27, 2013 at 7:14 pm

Posted in ASP.NET, hosting, review

Tagged with , , ,

Generating PDFs using iTextSharp on Mono

leave a comment »

I was looking for a .NET pdf generator so that I could add online bingocard generation to my side project, BingoWords.  The code and website are built using .NET and I run the site under Mono on a VPS at Linode.  So, it was important that the PDF generator run under Mono without issues.  Being cheap and full featured were two other desired features.  After some research, I found that there were two major players that satisfied this criteria,  PDFSharp and  iTextSharp.  Both are open source, free, and are fully featured.

Although some posts on the web indicated that PDFSharp would run under Mono, it was a no-go from  the beginning.  It failed the mono compatibility tool – Mono Migration Analyzer or M0MA almost right away.  I still decided to try out PDFSharp with Mono after reading a few posts stating that it may work if my code avoided the non-Mono compatible portions of the code but that was a total failure.  

So, I had no choice but to use iTextSharp.  iTextSharp passed under MoMA and it worked great on my Linode VPS under Mono.  With iTextSharp, I can generate PDFs that comply with the PDF standard and are viewable on every major platform.  So, if you are planning to generate PDFs under Mono, I would highly recommend iTextSharp.  Or to get a better sense of the features, try out my free Online Bingo Card Generator!

 I noticed that weren’t many code samples available while doing my research and coding the Bingo Card Generator.  So, here are some samples from my code for those that are just getting started:

Creating a new PDF document:

MemoryStream ms = new MemoryStream();
Document document = new Document(PageSize.LETTER);
PdfWriter writer = PdfWriter.GetInstance(document, ms);
document.Open();

Creating a BaseFont.  A BaseFont contains the parameters of the font that is written to the PDF document:

BaseFont bf = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

Writing center-aligned text to 400pts from the left and 400pts from the top:

cb.BeginText();
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, “Title Text”, (int)(Math.Abs(400, 400, 0);
cb.EndText();

Getting the width of a string in points:

titleWidth = (int)cb.GetEffectiveStringWidth(“Hello World!”, false);
Setting the font size of a BaseFont:
cb.SetFontAndSize(bf, titleFontSize);

Draw a line from the top left to the bottom left of the document:

cb.MoveTo(document.Left, document.Top);
cb.LineTo(document.Left, document.Bottom);
cb.Stroke();

Editing the document’s properties:

document.AddTitle(“Designed by BingoWords.com”);
document.AddAuthor(“Author goes here!”);
document.AddSubject(“Subject Goes Here!”);
document.AddKeywords(“Keyword1, keyword2, …”);

Closing your document:

document.Close();

Put this after the “document.Close()” to return your document with the name “file.pdf” when a user clicks on a link:

Response.ContentType = “application/pdf”;
Response.AddHeader(“content-disposition”, “attachment;filename=file.pdf”);
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();

I hope this comes in handy.  Feel free to contact me if you have any questions or comments.

Written by M Kapoor

June 20, 2010 at 5:52 am