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).
Related
I'm generating a PDF file with MigraDoc. Now I need to add an image to the PDF, but if the image is bigger than the page, it is cut, as if part of it is outside the page. I tried to set the image width to a smaller size, but It's not working properly :
var sec = doc.AddSection();
var p = sec.AddParagraph();
var img = p.AddImage(imgFile);
img.Width = "10cm";
img.LockAspectRatio = true;
But in the result PDF, it seems its ignoring the width I'm setting, It appears with the same size it was before.
Found the solution, it was dumber then I thougt. I was testing in a small console applcation, but I was generating the pdf to "Result2.pdf" and opening "Result.pdf". Sorry. 😳
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
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.
I have 2 asp.net chart control which i want to convert to pdf. I am using iTextSharp to convert the images to pdf.
The issue is with the position of images, i want images to come next to other.
i tried to setpagesize but it didnt worked.
Document pdfDoc = new Document(PageSize.A4);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
Chart1.SaveImage(stream, ChartImageFormat.Png);
iTextSharp.text.Image chartImage = iTextSharp.text.Image.GetInstance(stream.GetBuffer());
chartImage.ScalePercent(75f);
pdfDoc.Add(chartImage);
Chart2.SaveImage(stream, ChartImageFormat.Png);
iTextSharp.text.Image chartImage1 = iTextSharp.text.Image.GetInstance(stream.GetBuffer());
chartImage1.ScalePercent(75f);
pdfDoc.Add(chartImage1);
The best way to position images next to each other, is to add them to a ´PdfPTable´. I've created a small example in Java: ImagesNextToEachOther
As you can see, we wrap images inside a cell, asking the cell to scale the image so that it fits the width of the cell.
You'll have to make small changes to the code, as I've used iText instead of iTextSharp, but the difference should be minimal.
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);