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.
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. 😳
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.)
I am using Image.RotateFlip() to fix the orientation of some photos. I read the EXIF information of each photo and then I rotate/flip the image accordingly to make it straight up. My code is something like this:
//get info before processing.
var imgf = img.RawFormat;
//rotate and/or flip.
img.RotateFlip(rfty);
//reset orientation info.
img.RemovePropertyItem(km_iPropertyTagOrientation);
img.RemovePropertyItem(km_iPropertyTagThumnbailOrientation);
//save fixed image to a new file.
string strFilenameNew = Path.ChangeExtension(strFilename, "temp");
img.Save(strFilenameNew, imgf);
//replace original file.
File.Delete(strFilename);
File.Move(strFilenameNew, strFilename);
As you can see, I rotate/flip the image, remove the EXIF information that encoded the old orientation, and lastly I save it in the same format and replace the original file.
When I upload my photos on to http://metapicz.com, this is what I get:
Source photo (rotated 90 degrees, with orientation data): image looks rotated, landscape aspect ratio, orientation shows as Rotate 90 CW. This is what you would expect.
Fixed photo (processed by code above): image looks straight up, portrait aspect ratio, orientation data not reported. This is also what you would expect.
So far, so good. Now, I use Image.GetThumbnailImage() to obtain a thumbnail of the fixed photo, by scaling its size by 20%. The odd thing is, the resulting thumbnail is rotated again! When I upload it on to http://metapicz.com, this is what I get:
Thumbnail photo (obtained from fixed photo): image looks rotated, portrait aspect ratio, orientation data not reported. This is awkward, because the photo is now distorted, since the aspect ratio is preserved from the fixed photo (portrait), but the image in the photo looks rotated as in the source photo!
You can find this set of photos (source, fixed and thumbnail) here: https://verdewek-my.sharepoint.com/personal/cesargon_verdewek_com/_layouts/15/guestaccess.aspx?folderid=0d56479242b154fb7a31ae9a96f7be9cb&authkey=AbT_VQeoKU2EoVDuhvM2RgM
Any idea what I may be missing? Thank you.
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.
I did some searching to try and generate jpg files from an html page and found one solution called IECapt or something similar that requires IE on the server to work...not what I want.
Here's what I'm looking to do: Generate a jpg image from an html page (the html page will just be text) and then put a watermark over the jpg.
Essentially, I'm creating a "sample" that my users can see which will just be an image created from html (again just straight text). That sample should have a watermark on it as mentioned above. Are there any libraries available to do this with c#? What I'd like is to pass in the url of my page that I want converted to a method and maybe the save path of the jpg, then have it work its magic, and convert that url to a jpg image, toss a watermark on it, then say hooray!
Edit 1
adding some code from the answer below..can't get my head around this:
InitialContainer c = new InitialContainer("<html><body><div align=\"center\">This is my html, does it work here?</div></body></html>");
Bitmap m_Bitmap = new Bitmap(400, 700);
c.Paint(Graphics.FromImage(m_Bitmap));
m_Bitmap.Save(#"C:\test\Test.bmp");
Edit 2
this DOES work.
Bitmap m_Bitmap = new Bitmap(400, 600);
PointF point = new PointF(0,0);
HtmlRenderer.Render(Graphics.FromImage(m_Bitmap), "<html><body><div align=\"center\">This is my html, does it work here?</div></body></html>",point, 500);
m_Bitmap.Save(#"C:\test\Test.bmp");
You can use this HtmlRenderer class.
I haven't tried this, but you can try using Control.DrawToBitmap().
To draw the watermark you can go like this:
Image img; //the html image.
Image watermark; //the watermark image.
Point location; //where to draw the watermark on the html image.
Graphics g = Graphics.FromImage(img);
g.DrawImage(watermark, location);