How Can I Create a PDF Quotation File from Winform? - c#

alt text http://img517.imageshack.us/img517/8124/56267347.jpg
Assuming i have a winform that has a button called "Create a Quote". Once user clicks it,
a PDF File gets created on desktop. The PDF File has a fixed design/structure e.g. the logo is at the top left corner, the headers are fixed, etc. However, the data it self is pulled from a database.
As you see above, Quotation #, Description, Qty, U.P (USD), T.P (USD) records should be fetched from the db and dumped in the pdf template then create the pdf for user.
I haven't done this before. I hope my question is clear enough. Feel free to put useful web links that help me achieve it.
Any help will be appreciated.

You should look at two of the most popular open-source PDF libraries for C#, namely:
ITextSharp and PDFSharp
Both allow you to programatically create PDF files. Here's a quick 'hello word' example with PDFSharp:
// Create a new PDF document
PdfDocument document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";
// Create an empty page
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
// Create a font
XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
// Draw the text
gfx.DrawString("Hello, World!", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height),
XStringFormats.Center);
// Save the document...
const string filename = "HelloWorld.pdf";
document.Save(filename);
// ...and start a viewer.
Process.Start(filename);

Related

How to get the remaining page size using syncfusion pdf

i am creating pdf using synfusion pdf plugin for xamarin forms and what happenes if i print my pdf then there is many remaining space on the page so how i can get that remaining space of pdf page using syncfusion pdf
We can get the blank space region in the page of PDF document by using PdfLayoutResult. Please refer the below code snippet for more details,
C#:
//Create a text element with the text and font
PdfTextElement textElement = new PdfTextElement(text, font);
PdfLayoutFormat layoutFormat = new PdfLayoutFormat();
layoutFormat.Layout = PdfLayoutType.Paginate;
//Draw the paragraph
PdfLayoutResult result = textElement.Draw(page, new RectangleF(0, 0,
page.GetClientSize().Width, page.GetClientSize().Height), layoutFormat);
//Get the blank space of PDF using PdfLayoutResult and page height
float blankSpaceHeight = page.GetClientSize().Height - result.Bounds.Bottom;
We have created the sample for the same which can be downloaded from below link,
Sample to get the remaining page size of PDF document

iText7, Is there a way to control carriage return inside rectangle?

I'm using iText 7 with C# and I have to write a long line.
With canvas.BeginText().ShowText("My text") I can't find a way to make the text pass on a second line, \n is not recognized.
So I used rectangles and document renderer but I have the same problem, I can't control where I want my text to create a new line.
I use an existing PDF (a model) where I have to write some texts (as short as a single line) and some paragraphs (composed of several lines). Those elements are defined in an xml where I can have some carriage returns to delimit new lines in paragraphs. Is short, the document is composed dynamically and it's content and element's placement are defined inside an xml file.
Adding content with low-level methods such as BeginText(), ShowText(), EndText() and so on, requires a sound knowledge of the PDF specification (ISO 32000). The fact that you are surprised at the fact that \n is ignored tells me that you aren't that well versed in PDF.
iText was written for people who don't want to deal with the low-level syntax of PDF. For instance: if you want to add text inside a rectangle with iText, you just have to create a Canvas object to which you pass a Rectangle object:
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfPage page = pdf.AddNewPage();
PdfCanvas pdfCanvas = new PdfCanvas(page);
Rectangle rectangle = new Rectangle(36, 650, 100, 100);
Canvas canvas = new Canvas(pdfCanvas, pdf, rectangle);
PdfFont font = PdfFontFactory.CreateFont(FontConstants.TIMES_ROMAN);
PdfFont bold = PdfFontFactory.CreateFont(FontConstants.TIMES_BOLD);
Text title =
new Text("The Strange Case of Dr. Jekyll and Mr. Hyde").SetFont(bold);
Text author = new Text("Robert Louis Stevenson").SetFont(font);
Paragraph p = new Paragraph().Add(title).Add(" by ").Add(author);
canvas.Add(p);
pdf.Close();
This example can be found in chapter 2 of the online iText 7 tutorial.
The screenshot shows how a long sentence was added inside a Rectangle, and how that sentence got distributed over different lines (introducing new lines automatically). The concept of the \n character doesn't exist in PDF (check ISO 32000 when in doubt). If you want to introduce a newline, it's sufficient to put one part of the content in one Paragraph and the other part in another Paragraph.

EVO PDF PDF contents have small font

I am trying to create a PDF file by using EVOHTMLTOPDF. But the pdf generated is wrong and the contents has a font size so small that i cant even read it.
This is the current code I use inside a webservice
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";
htmlToPdfConverter.HtmlViewerWidth = int.Parse("1024");
htmlToPdfConverter.PdfDocumentOptions.PdfPageSize = EvoPdf.PdfPageSize.Letter;
htmlToPdfConverter.PdfDocumentOptions.PdfPageOrientation = EvoPdf.PdfPageOrientation.Portrait;
htmlToPdfConverter.PdfDocumentOptions.TopMargin = 15f;
htmlToPdfConverter.PdfDocumentOptions.RightMargin = 15f;
htmlToPdfConverter.PdfDocumentOptions.BottomMargin = 15f;
htmlToPdfConverter.PdfDocumentOptions.LeftMargin = 15f;
byte[] outPdfBuffer = null;
outPdfBuffer = htmlToPdfConverter.ConvertHtml(DocContent, baseUrl);
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + filename);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
File.WriteAllBytes(sourcepath, outPdfBuffer);
I have also attached a screenshot
Some time i get this error which reads "PDF is too big"
Please anybody provide a solution as i want to create a pdf and download the pdf to the users computer.
Could you try to see which is the minimum width of the browser window that can display the content of the HTML page you convert?
If the content width is very large it will be scaled down to fit the PDF page width. The solution would be to modify your HTML page to allow the content reflow at different browser window widths.
You can find a live demo where to test various scaling options at HTML to PDF Scaling Options Demo . For example you can check on 'Clip HTML Content Width' option to truncate the HTML content if it is too wide.
Regarding the "PDF is too big", when do you get this error? When you create the PDF, when you open the PDF in a viewer, etc. This does not seem to be an error from library.
To have font which set in html I made a formula:
htmlToPdfConverter.HtmlViewerWidth = 800 - (int)((htmlToPdfConverter.PdfDocumentOptions.RightMargin + htmlToPdfConverter.PdfDocumentOptions.LeftMargin) * 1.33);
After that font size in pdf from Evo converter is correct and equal to font size from pdf document which created with MS Word (export to pdf).

iTextSharp A4 size paper printing to 2 pages

So a ITextSharp created 1 page PDF is printing to two pages using ITextSharp and I don't know why.
This is how I declare my document using ITextSharp:
var document = new Document(PageSize.A4, 0, 0, 0, 0);
Whether there is content added or not, if you go to print the PDF it prints two pages but whether you view in Adobe Acrobat, Adobe Reader or Chrome PDF viewer it is a one page document.

PDF stamping is not working in pages where does not have header info or body

Using the below code pdf stamping is working fine but if the pdf page does not have header info or body is empty then stamping is not working I mean pdfData.ShowText(strCustomMessage) is not working.
//create pdfreader object to read sorce pdf
PdfReader pdfReader=new PdfReader(Server.MapPath("Input") + "/" + "input.pdf");
//create stream of filestream or memorystream etc. to create output file
FileStream stream = new FileStream(Server.MapPath("Output") + "/output.pdf", FileMode.OpenOrCreate);
//create pdfstamper object which is used to add addtional content to source pdf file
PdfStamper pdfStamper = new PdfStamper(pdfReader,stream);
//iterate through all pages in source pdf
for (int pageIndex = 1; pageIndex <= pdfReader.NumberOfPages; pageIndex++)
{
//Rectangle class in iText represent geomatric representation... in this case, rectanle object would contain page geomatry
Rectangle pageRectangle = pdfReader.GetPageSizeWithRotation(pageIndex);
//pdfcontentbyte object contains graphics and text content of page returned by pdfstamper
PdfContentByte pdfData = pdfStamper.GetUnderContent(pageIndex);
//create fontsize for watermark
pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 40);
//create new graphics state and assign opacity
PdfGState graphicsState = new PdfGState();
graphicsState.FillOpacity = 0.4F;
//set graphics state to pdfcontentbyte
pdfData.SetGState(graphicsState);
//set color of watermark
pdfData.SetColorFill(BaseColor.BLUE);
//indicates start of writing of text
pdfData.BeginText();
//show text as per position and rotation
pdfData.SetTextMatrix(pageRectangle.Width/2,pageRectangle.Height/2); pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 9);
string strCustomMessage="PDF Stamping Test"
pdfData.ShowText(strCustomMessage);
//call endText to invalid font set
pdfData.EndText();
}
//close stamper and output filestream
pdfStamper.Close();
stream.Close();
I think your question is wrong. Would do you mean when you say "the pdf page does not have header info or body is empty"? Because that doesn't make sense.
A few observations: I think you're using an obsolete version of iTextSharp. Read the FAQ to find out why this is a bad idea.
Newer versions of iTextSharp will throw an exception because of this error:
pdfData.BeginText();
//show text as per position and rotation
string strCustomMessage="PDF Stamping Test"
pdfData.ShowText(strCustomMessage);
//call endText to invalid font set
pdfData.EndText();
In these lines, you are violating the PDF reference: you are showing text within a BT/ET sequence without defining a font and size. Had you used a newer version of iTextSharp, an exception would have pointed that out. You need to move the line with the SetFontAndSize() method inside the BeginText()/EndText() sequence. It's illegal to use it outside a text object.
Also: why are you using BeginText()/EndText() to add text? That is code for PDF specialists. Read the documentation to find out how to use ColumnText.ShowTextAligned(). The ShowTextAligned() method is a convenience method written for developer who aren't fluent in PDF syntax.
Whether or not the text added with ShowText() is visible will depend on:
the opacity of the page: Suppose that your page consists of a scanned image, you won't see any text, because you're adding the text under the image. Use the GetOverContent() method instead of the GetUnderContent() method to avoid this.
the position on the page: I see that you get the size of the page using the GetPageSizeWithRotation() method, but I don't see you doing anything with that info. I don't see at which coordinate you are showing the text. That is also wrong. You need to make sure that you add the text inside the visible area of the page.
This answer lists several problems with your code. It would be better if you simplify your code by using the ShowTextAligned() and correct X,Y coordinates.

Categories

Resources