Overlay image onto PDF using PDFSharp - c#

Can't seem to find much out there for this. I've a PDF, onto which I'd like to overlay an image of an electronic signature. Any suggestions on how to accomplish that using PDFSharp?
Thanks

Try the following
private void GeneratePDF(string filename, string imageLoc)
{
PdfDocument document = new PdfDocument();
// Create an empty page or load existing
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
DrawImage(gfx, imageLoc, 50, 50, 250, 250);
// Save and start View
document.Save(filename);
Process.Start(filename);
}
void DrawImage(XGraphics gfx, string jpegSamplePath, int x, int y, int width, int height)
{
XImage image = XImage.FromFile(jpegSamplePath);
gfx.DrawImage(image, x, y, width, height);
}
This will generate a new PDF with the specified image near the top of the page. If you need to use an existing document change the PdfDocument constructor to
PdfDocument document = new PdfDocument(filename);
where filename is the name of the file to load and change the PdfPage line to
PdfPage page = document.Pages[pageNum];
where pageNum is the number of the page on which you need to add the image.
After that, it just a matter of getting the positioning on the page by altering the parameters for DrawImage to suit.
DrawImage(gfx, imageLoc, 50, 50, 250, 250);
Good luck!

This will help you:
PdfDocument document = pdf;
// Create a new page
PdfPage page = document.Pages[0];
page.Orientation = PageOrientation.Portrait;
XGraphics gfx = XGraphics.FromPdfPage(page, XPageDirection.Downwards);
// Draw background
gfx.DrawImage(XImage.FromFile("pdf_overlay.png"), 0, 0);
Just add the path to the image you want, and specify the position of the image.

To roughly maintain the aspect ratio, I used #Kami's answer and "roughly" centered it.
The assumption made is that the pdf width is 600 and pdf height is 800, we will make use of the middle 500 and 700 of the page only, leaving the 4 sides have at least 50 in length as margin.
public static void GeneratePdf(string outputPath, string inputPath)
{
PdfSharp.Pdf.PdfDocument document = new PdfSharp.Pdf.PdfDocument();
// Create an empty page or load existing
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
DrawImage(gfx, inputPath);
// Save and start View
document.Save(outputPath);
Process.Start(outputPath);
}
public static void DrawImage(XGraphics gfx, string imagePath)
{
XImage image = XImage.FromFile(imagePath);
var imageHeight = image.PixelHeight;
var imageWidth = image.PixelWidth;
int height;
int width;
int x;
int y;
width = 500;
height = (int) Math.Ceiling((double) width * imageHeight / imageWidth);
x = 50;
y = (int) Math.Ceiling((800 - height) / 2.0);
if(height > 700)
{
height = 700;
width = (int) Math.Ceiling(imageWidth * (double) height / imageHeight);
y = 50;
x = (int) Math.Ceiling((600 - width) / 2.0);
}
gfx.DrawImage(image, x, y, width, height);
}

Related

ItextSharp add image on center of page with the text under it

I'm trying add picture on centre(middle) of page PDF file with text, but I can't do it right. I use for image SetAbsolutePosition, but text don't stand under picture.
I need in my Pdf file next format of page:
I use next code:
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
ITextEvents ev = new ITextEvents();
writer.PageEvent = ev;
doc.Open();
var paragraph = new Paragraph();
var paragraph1 = new Paragraph();
var chunk = new Chunk("Text under picture", f14nb);
var chunk1 = new Chunk("Code of picture", f14);
img = ScaleImg(Image.GetInstance(imgNane_2));
img.SetAbsolutePosition((PageSize.A4.Width - img.ScaledWidth) / 2,
((PageSize.A4.Height - img.ScaledHeight) / 2));
paragraph.Add(img);
paragraph1.Add(chunk);
paragraph1.Add(chunk1);
doc.Add(paragraph);
doc.Add(paragraph1);
doc.Close();
private Image ScaleImg(Image img)
{
if (img.Height > img.Width)
{
//Maximum height is 800 pixels.
float percentage = 0.0f;
percentage = 640 / img.Height;
img.ScalePercent(percentage * 100);
}
else
{
//Maximum width is 600 pixels.
float percentage = 0.0f;
percentage = 500 / img.Width;
img.ScalePercent(percentage * 100);
}
return img;
}
I think, that I should use another way for solve my problem, but I don't know which.
Thank you.
I understood how do it.
This is for image with text:
public Image getWatermarkedImage(Document Doc, Image img, String watermarkText)
{
float width = img.ScaledWidth;
float height = img.ScaledHeight;
PdfTemplate template = cb.CreateTemplate(width, height);
template.AddImage(img, width, 0, 0, height, 0, 0);
ColumnText.ShowTextAligned(template, Element.ALIGN_RIGHT,
new Phrase(watermarkText, fontBold_14), width - 10, 10, 0);
return Image.GetInstance(template);
}
This is for adding text under image(main code):
var codeOfPicture = "*Code of picture* - *Код картинки*";
var chunk = new Chunk("Text under picture", font_14);
img = ScaleImg(Image.GetInstance(imgNane_1));//imgNane_2));
var tmpImg = getWatermarkedImage(doc, img, codeOfPicture);
var textY = ((PageSize.A4.Height - img.ScaledHeight) / 2) - 15;
var textX = PageSize.A4.Width / 2;
tmpImg.SetAbsolutePosition((PageSize.A4.Width - img.ScaledWidth) / 2,
((PageSize.A4.Height - img.ScaledHeight) / 2));
doc.Add(tmpImg);
ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, new Phrase(chunk), textX, textY, 0);

center image in pdf using itextsharp

What I'm trying to do here is to add an image to a blank pdf. So far I've done it, but I want the image to be centered. How can I do this?
Here is my C# code:
using (MemoryStream ms = new MemoryStream())
{
Document doc = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(System.IO.Path.Combine(filepath, strFilename), FileMode.Create));
doc.AddTitle("Document Title");
doc.Open();
iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance(#"C:\Users\Desktop\Winniethepooh.png");
image1.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
if (image1.Height > image1.Width)
{
//Maximum height is 800 pixels.
float percentage = 0.0f;
percentage = 700 / image1.Height;
image1.ScalePercent(percentage * 100);
}
else
{
//Maximum width is 600 pixels.
float percentage = 0.0f;
percentage = 540 / image1.Width;
image1.ScalePercent(percentage * 100);
}
//image1.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
doc.Add(image1);
doc.Close();
}
And this is the output:
https://drive.google.com/open?id=0BzaejXGgqBOAMzd0UlY2QWFXNms
What I want is that the image is centered on the page. Currently the image is on the top of the page.
I even set the image alignment, but why doesn't it center the image on the page?
You need to use SetAbsolutePosition() in order to center the image.
Just add the following to your code before you call doc.Add(image1);:
...
...
image1.SetAbsolutePosition((PageSize.A4.Width - image1.ScaledWidth) / 2, (PageSize.A4.Height - image1.ScaledHeight) / 2);
doc.Add(image1);
...
...
Hope this helps.

How to calculate FixedPage dimensions

This snippet is part of some code used to generate an XPS document. XPS document generation is no joke. I wish to avoid pasting any of that XPS code here if at all possible. Instead, this code focuses on the WPF portion of the problem.
The problem I am asking for you to help with is here. I have hard coded the dimensions to work for a test image:
double magicNumber_X = 3.5;//trial and error...3 too small 4 too big
fixedPage.Arrange(new Rect(new Point(magicNumber_X, 0), size));
Instead, how can I fix this code to calculate the coordinates?
Full Method:
private PageContent AddContentFromImage()
{
var pageContent = new PageContent();
var fixedPage = new FixedPage();
var bitmapImage = new BitmapImage(new Uri(hardCodedImageSampleFilePath, UriKind.RelativeOrAbsolute));
var image = new Image();
image.Source = bitmapImage;
fixedPage.Children.Add(image);
((IAddChild)pageContent).AddChild(fixedPage);
double pageWidth = 96 * 8.5;//XPS documents are 96 units per inch
double pageHeight = 96 * 11;
fixedPage.Width = pageWidth;
fixedPage.Height = pageHeight;
var size = new Size(8.5 * 96, 11 * 96);
fixedPage.Measure(size);
double magicNumber_X = 3.5;//trial and error...3 too small 4 too big
double magicNumber_Y = 0;
fixedPage.Arrange(new Rect(new Point(magicNumber_X, magicNumber_Y), size));
fixedPage.UpdateLayout();
return pageContent;
}
I'm a little surprised FixedPage.Measure(size) does not correct the issue by itself. I tried passing no params, e.g. fixedPage.Arrange(new Rect(), size)) still no go.
FWIW, this calculation worked fine when I was using PrintDocument.
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
RectangleF marginBounds = e.MarginBounds;
RectangleF printableArea = e.PageSettings.PrintableArea;
int availableWidth = (int)Math.Floor(printDocument.OriginAtMargins ? marginBounds.Width : (e.PageSettings.Landscape ? printableArea.Height : printableArea.Width));
int availableHeight = (int)Math.Floor(printDocument.OriginAtMargins ? marginBounds.Height : (e.PageSettings.Landscape ? printableArea.Width : printableArea.Height));
Rectangle rectangle = new Rectangle(0,0, availableWidth -1, availableHeight - 1);
g.DrawImage(_image, rectangle);
I hooked into FixedPage.Loaded event because FixedPage.ActualHeight is required in order to perform the calculation and will not be set until the control has loaded. This also means that with this mechanism FixedPage has to be displayed to correctly perform an automated print.
void fixedPage_Loaded(object sender, RoutedEventArgs e)
{
var fixedDocument = sender as FixedPage;
CalculateSize(fixedDocument);
}
private void CalculateSize(FixedPage fixedPage)
{
PrintQueue printQueue = LocalPrintServer.GetDefaultPrintQueue();
PrintCapabilities capabilities = printQueue.GetPrintCapabilities();
//get scale of the print wrt to screen of WPF visual
double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / fixedPage.ActualWidth, capabilities.PageImageableArea.ExtentHeight / fixedPage.ActualHeight);
//Transform the Visual to scale
fixedPage.LayoutTransform = new ScaleTransform(scale, scale);
//get the size of the printer page
var sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
//update the layout of the visual to the printer page size.
fixedPage.Measure(sz);
double x = capabilities.PageImageableArea.OriginWidth;
double y = capabilities.PageImageableArea.OriginHeight;
fixedPage.Arrange(new Rect(new Point(x, y), sz));
fixedPage.UpdateLayout();
}

I'm using PdfSharp and can't find the class BeginBox where is it?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Drawing;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
namespace Lightnings_Extractor
{
class PDF
{
public PDF()
{
// 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 = #"d:\HelloWorld.pdf";
document.Save(filename);
// ...and start a viewer.
Process.Start(filename);
}
private void DrawImage(XGraphics gfx, int number)
{
}
}
}
I'm taking the sample from this link: http://www.pdfsharp.net/wiki/Graphics-sample.ashx
There is a sample there: Draw an image in original size
void DrawImage(XGraphics gfx, int number)
{
BeginBox(gfx, number, "DrawImage (original)");
XImage image = XImage.FromFile(jpegSamplePath);
// Left position in point
double x = (250 - image.PixelWidth * 72 / image.HorizontalResolution) / 2;
gfx.DrawImage(image, x, 0);
EndBox(gfx);
}
What am I missing here?
BeginBox and EndBox are helper methods defined at the bottom of the sample page.
Note that these methods are only to create the surrounding box and is probably not what you need to achieve the functionality you really want (in this case, to draw an image in original size). So I would just remove those two lines from the code.
public void BeginBox(XGraphics gfx, int number, string title)
{
const int dEllipse = 15;
XRect rect = new XRect(0, 20, 300, 200);
if (number % 2 == 0)
rect.X = 300 - 5;
rect.Y = 40 + ((number - 1) / 2) * (200 - 5);
rect.Inflate(-10, -10);
XRect rect2 = rect;
rect2.Offset(this.borderWidth, this.borderWidth);
gfx.DrawRoundedRectangle(new XSolidBrush(this.shadowColor), rect2, new XSize(dEllipse + 8, dEllipse + 8));
XLinearGradientBrush brush = new XLinearGradientBrush(rect, this.backColor, this.backColor2, XLinearGradientMode.Vertical);
gfx.DrawRoundedRectangle(this.borderPen, brush, rect, new XSize(dEllipse, dEllipse));
rect.Inflate(-5, -5);
XFont font = new XFont("Verdana", 12, XFontStyle.Regular);
gfx.DrawString(title, font, XBrushes.Navy, rect, XStringFormats.TopCenter);
rect.Inflate(-10, -5);
rect.Y += 20;
rect.Height -= 20;
this.state = gfx.Save();
gfx.TranslateTransform(rect.X, rect.Y);
}
public void EndBox(XGraphics gfx)
{
gfx.Restore(this.state);
}
You can find the complete solution here at PDFsharp-samples.
Both "BeginBox(XGraphics gfx, int number, string title)" & "EndBox(XGraphics gfx)" is implemented in the Base class and the Base class is inherited by all the other classes (LinesAndCurves, Shapes, Paths, Text, Images).

C# .NET: Resizing a image inside a container div element

I have following markup codes that has a container div element and an img tag nested inside the div. The container div has width, height, top and left CSS style properties.
An originally uploaded image has arbitrary width and height that may be bigger or smaller than the container div. So definitely, the originally uploaded image must be resized and well scaled, and saved as an thumbnail image to sit within the borders of the container div. That resized thumbnail image will be shown as the source (src) in the following markups:
<div id="divContainer" style="width: 600px; height: 450px; top: 50px; left: 20px;">
<img src="[my resized and well scaled thumbnail source]..." id="imgResizedThumnail" />
</div>
In a another .NET form page, there is a file tag letting users upload an original image from local hard disk. That uploaded image needs to be resized and saved as another thumbnail image with best scaling. "Best scalling" means the thumbnail image has a proportional ratio of width and height, and the thumbnail must be sit within the container div.
My C# .NET method looks like follows and I have questions about the code logic in that method.
...
using System.Drawing;
public void SaveThumbnailImageWithbestScaling(Image originalImage, int containerDivWidth, int containerDivHeight)
{
// input containerDivWidth and containerDivHeight are dynamic!
// 1. How can I calculate the scale variable?
double scale = ??? // how can I do the codes here?
// 2. Use that scale to determine the dimension of resized thumbnail image from
// the input originalImage for following variables "thumnailWidth" and "thumnailHeight"
string thumbnailFilename = "myThumnailFileName";
int thumnailWidth = ??? // how can I do the codes here?
int thumnailHeight = ??? // how can I do the codes here?
Bitmap thumnailImage = CreateThumbnail(thumbnailFilename,int thumnailWidth, int thumnailHeight);
// 3. save thumbnail
SaveThumnail(thumnailImage);
}
public void Bitmap CreateThumbnail(string lcFilename,int lnWidth, int lnHeight)
{
...
}
public void thumnailImage (Bitmap thumnail)
{
...
}
This is what I use:
public static Image Resize(
Image srcImage,
int newWidth,
int maxHeight,
int dpi = 72)
{
if(srcImage.Width<=newWidth)
{
newWidth = srcImage.Width;
}
var newHeight = srcImage.Height * newWidth / srcImage.Width;
if (newHeight > maxHeight)
{
newWidth = srcImage.Width * maxHeight / srcImage.Height;
newHeight = maxHeight;
}
var newImage = new Bitmap(newWidth, newHeight);
newImage.SetResolution(dpi, dpi);
using (var gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
}
return newImage;
}
I would have done something like this:
public void SaveThumbnailImageWithbestScaling(Image originalImage, int containerDivWidth, int containerDivHeight)
{
string thumbnailFilename = "myThumnailFileName";
int thumbnailWidth = 0;
int thumbnailHeight = 0;
float imgWidth = (float)originalImage.Width;
float imgHeight = (float)originalImage.Height;
float scale_w = imgWidth / (float)containerDivWidth;
float scale_h = imgHeight / (float)containerDivHeight;
// Compute how much each scale diverge from 1 (1 means no scaling, which is desirable)
float variance_w = Math.Abs(1.0 - scale_w);
float variance_h = Math.Abs(1.0 - scale_h);
if (variance_w > variance_h)
{
// Height ratio is closer to 1
float aspect_ratio = imgWidth / imgHeight;
thumbnailHeight = containerDivHeight;
thumbnailWidth = (int)Math.Floor(aspect_ratio * imgWidth);
}
else
{
// Width ratio is closer to 1
float aspect_ratio = imgHeight / imgWidth;
thumbnailHeight = (int)Math.Floor(aspect_ratio * imgHeight);
thumbnailWidth = containerDivWidth;
}
Bitmap thumnailImage = CreateThumbnail(thumbnailFilename,int thumnailWidth, int thumnailHeight);
// 3. save thumbnail
SaveThumnail(thumnailImage);
}
The algorithm computes the ratio of each dimension, then figures out which one varies the most from the div tag's dimension. It then proceeds to snap the dimension with the least variance to the size of the div tag, and scale the other one to respect the image's aspect ratio.
Of course there are other ways of doing it, you could for example not care about aspect ratio and just snap both dimensions to that of the div tag.

Categories