ABC PDF anti-aliasing images when asked not to - c#

Adding a QR code as a bitmap to an ABCPDF document:
Doc pdf = new Doc();
pdf.Rendering.AntiAliasImages = false;
...
pdf.AddImageBitmap(bmp, true);
When rendered to a PDF file the image appears anti-aliased:
When printed direct to a printer the same the QR code is fine:
My question is: what am I doing wrong?

You need to size Doc.Rect based on the image size and resolution e.g.
// Set PDF image size from image size and resolution (PDF coord space is 72dpi)
doc.Rect.Height = bmp.Height * 72 / bmp.VerticalResolution;
doc.Rect.Width = bmp.Width * 72 / bmp.HorizontalResolution;
doc.AddImageBitmap(bmp, true);
(And the Rendering class properties only apply when exporting from a PDF to an image.)

Related

How to set image width when adding to a MigraDoc document?

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. 😳

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

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).

How do I rotate a background watermark image to landscape with iTextSharp?

I'm building a PDF class with iTextSharp to handle my company's digital records. We use expensive paper that has columns and has our company's logo at the top. I've scanned a copy as a PDF and another as a JPG. I've read elsewhere that the IMG.Rotate() command should rotate the image. I've also read that Image.SetRotationDegrees(90) is supposed to work, but that line of code doesn't work for me. SetRotationDegrees isn't a method of Image. Here's what I've got so far:
string imageFilePath = _watermark;
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
//Rotate it for landscape
Image jpg.Rotate(); Image.GetInstance(System.Drawing.Image.FromStream(fs), ImageFormat.Jpeg);
//Scale image
jpg.ScalePercent(35f);
//Set position
jpg.ScaleToFit(770, 3000);
jpg.WidthPercentage = 100;
jpg.Rotate();
//Allignment
jpg.Alignment = iTextSharp.text.Image.UNDERLYING;
...
PDFReport.Add(jpg);
`
Anyone have any ideas? The image is being placed on the page but it places it portrait instead of landscape.

how to smooth the edge of the image from disk/exporting to pdf(c#)

im using itextsharp for exporting the image to pdf.
---- i want to make the edges of image smooth (curved edges),
---- and by itextsharp image property to get the image width and height(while getting the image from disk)
---- and also how to set the background color of the pdf page
following is for getting image and adding to pdf:
pdfDoc.Open();
//pdfDoc.Add(new iTextSharp.text.Paragraph("Welcome to dotnetfox"));
iTextSharp.text.Image gif = iTextSharp.text.Image.GetInstance(#"C:\Users\Admin\Desktop\logoall.bmp");
// gif.ScaleToFit(500, 100);
pdfDoc.Add(gif);
following is making grid to image and saving to disk:
Grid companysnapshot = values[0] as Grid; //companysnap shot
companysnapshot.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));
int companywidth = (int)Math.Round(companysnapshot.ActualWidth);
int companyheight = (int)Math.Round(companysnapshot.ActualHeight);
companywidth = companywidth == 0 ? 1 : companywidth;
companyheight = companyheight == 0 ? 1 : companyheight;
RenderTargetBitmap rtbmp = new RenderTargetBitmap(companywidth, companyheight, 96d, 96d, PixelFormats.Default);
rtbmp.Render(companysnapshot);
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtbmp));
FileStream fs1 = File.Create(#"C:\Users\Admin\Desktop\companyss.bmp");
encoder.Save(fs1);
fs1.Close();
please code me out for this!!
You have combined multiple question into one post. That's not the way you should post questions.
Anyway:
Question 1: What is the size of an image?
You have an Image instance gif. The witdh of this image is gif.ScaledWidthand jpg.ScaledHeight. There are other ways to get the width and the height, but this way always gives you the size in user units that will be used in the PDF.
If you do not scale the image, ScaledWidth and ScaledHeight will give you the original size of the image in pixels. Pixels will be treated as user units by iText. In PDF, a user unit corresponds with a point by default (and 72 points correspond with 1 inch).
Question 2: How do you display the image with rounded corners?
Some image formats (such as PNG) allow transparency. You could create an image in such a way that the effect of rounded corners is mimicked by making the corners transparent.
If this is not an option, you should apply a clipping path. This is demonstrated in the ClippingPath example in chapter 10 of my book.
Ported to C#, the example would be something like this:
Image img = Image.GetInstance(some_path_to_an_image);
float w = img.ScaledWidth;
float h = img.ScaledHeight;
PdfTemplate t = writer.DirectContent.CreateTemplate(w, h);
t.Ellipse(0, 0, w, h);
t.Clip();
t.NewPath();
t.AddImage(img, w, 0, 0, h, 0, -600);
Image clipped = Image.GetInstance(t);
Of course: this clips the image into an ellipse as shown in the resulting PDF. You need to replace the Ellipse() method from the example with the RoundRectangle() method.
Question3: How to give each page a background color?
This is a duplicate question. Please read the answers to the following questions:
add header, footer and Background color to pdf cells in iTextSharp
How to draw border for whole pdf pages using iText library 5.5.2
Why this pageEvent cover the content of page using iText library?
Adding a background color is done using page events and you'll find the code on how to do this in the questions mentioned above.

Categories

Resources