Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.Text;
using Datalogics.PDFL;
/*
*
* This program demonstrates how to import images into a PDF file. The program runs without
* prompting you, and creates two PDF files, demonstrating how to import graphics from image files
* into a PDF file.
*
* Copyright (c) 2007-2017, Datalogics, Inc. All rights reserved.
*
* For complete copyright information, refer to:
* http://dev.datalogics.com/adobe-pdf-library/license-for-downloaded-pdf-samples/
*
*/
namespace ImageImportSample
{
class ImageImportSample
{
static Rect CalculateMarginRect(Rect basisRect, double leftMargin, double bottomMargin, double rightMargin, double topMargin)
{
return new Rect(basisRect.Left + leftMargin,
basisRect.Bottom + bottomMargin,
basisRect.Right - rightMargin,
basisRect.Top - topMargin);
}
static Matrix CalcImageFitToRect(Image img,Rect rectToFit)
{
var imgBBox = img.BoundingBox;
double scaleFactorH = rectToFit.Width/ imgBBox.Width;
double scaleFactorV = rectToFit.Height / imgBBox.Height;
double scaleFactor = Math.Min(scaleFactorH, scaleFactorV);
double imgNewWidth = imgBBox.Width * scaleFactor;
double imgNewHeight = imgBBox.Height * scaleFactor;
double horizDisp = rectToFit.Left + (0.5 * (rectToFit.Width - imgNewWidth));
double vertDisp = rectToFit.Bottom +(0.5 * (rectToFit.Height - imgNewHeight));
return new Matrix(imgNewWidth,0,0,imgNewHeight,horizDisp,vertDisp);
}
static Matrix CalcImagePlaceToClip(Image img,double clipLeftPct, double clipBottomPct,double clipRightPct,double clipTopPct, Rect destRect)
{
var imgBBox = img.BoundingBox;
double clipWidthPct = clipRightPct - clipLeftPct;
double clipHeightPct = clipTopPct - clipBottomPct;
double clipWidth = imgBBox.Width * clipWidthPct / 100.0;
double clipHeight = imgBBox.Height * clipHeightPct / 100.0;
double scaleFactorH = destRect.Width / clipWidth;
double scaleFactorV = destRect.Height / clipHeight;
double scaleFactor = Math.Min(scaleFactorH, scaleFactorV);
double imgNewWidth = imgBBox.Width * scaleFactor;
double imgNewHeight = imgBBox.Height * scaleFactor;
double horizDisp = destRect.Left - (imgNewWidth * clipLeftPct / 100.0);
double vertDisp = destRect.Bottom - (imgNewHeight * clipBottomPct / 100.0);
return new Matrix(imgNewWidth, 0, 0, imgNewHeight, horizDisp, vertDisp);
}
static Clip rectClip(Rect destRect)
{
Path clipPath = new Path();
clipPath.AddRect(new Point(destRect.LLx, destRect.LLy), destRect.Width, destRect.Height);
var destClip = new Clip();
destClip.AddElement(clipPath);
return destClip;
}
static Clip ellipseClip(Rect ellipseRect)
{
const double kappa = 0.552;
const double thetaIncr = 0.5 * Math.PI;
double dx = (0.5 * ellipseRect.Width) - 1;
double dy = (0.5 * ellipseRect.Height) - 1;
double kdx = kappa * dx;
double kdy = kappa * dy;
double centerX = 0.5 * (ellipseRect.URx + ellipseRect.LLx);
double centerY = 0.5 * (ellipseRect.URy + ellipseRect.LLy);
Path clipPath = new Path();
Point start = new Point(centerX + dx * Math.Cos(0), centerY + dy * Math.Sin(0));
clipPath.MoveTo(start);
start.Dispose();
for (int i = 0; i < 4; i++)
{
double theta = i * thetaIncr;
double cosTheta = Math.Cos(theta);
double sinTheta = Math.Sin(theta);
Point c1 = new Point(centerX + (cosTheta * dx - sinTheta * kdx), centerY + (cosTheta * kdy + sinTheta * dy));
Point c2 = new Point(centerX + (cosTheta * kdx - sinTheta * dx), centerY + (cosTheta * dy + sinTheta * kdy));
Point end = new Point(centerX + dx * Math.Cos(theta + thetaIncr), centerY + dy * Math.Sin(theta + thetaIncr));
clipPath.AddCurve(c1, c2, end);
c1.Dispose();
c2.Dispose();
end.Dispose();
}
clipPath.ClosePath();
var destClip = new Clip();
destClip.AddElement(clipPath);
return destClip;
}
static void Main(string[] args)
{
Console.WriteLine("Import Images Sample:");
using (Library lib = new Library())
{
Console.WriteLine("Initialized the library.");
Document doc = new Document();
String sInput = "../../Resources/Sample_Input/ducky.jpg";
String sOutput = "../ImageImport-out1.pdf";
if (args.Length > 0)
sInput = args[0];
if (args.Length > 1)
sOutput = args[1];
Console.WriteLine("Reading image file" + sInput + " and writing " + sOutput);
using (Image origImage = new Image(sInput, doc))
{
var newimage = origImage.Clone();
var letterRect = new Rect(0, 0, 612, 792);
var marginRect = CalculateMarginRect(letterRect, 36, 72, 36, 72);
Page docpage = doc.CreatePage(Document.BeforeFirstPage, letterRect);
// Center the image on the page
newimage.Matrix = CalcImageFitToRect(newimage, marginRect);
docpage.Content.AddElement(newimage);
double[,] posArray = new double[,]{
{22.05882353, 54.27509294, 30.88235294, 69.51672862},
{25.88235294, 70.63197026, 33.82352941, 83.64312268},
{73.52941176, 70.63197026, 80.88235294, 83.64312268},
{71.47058824, 79.92565056, 78.23529412, 91.44981413}
};
for (int i = 0; i < posArray.GetLength(0); i++)
{
var subImg1 = origImage.Clone();
double clipWidth = 144;
double clipOffset = (i * clipWidth + 12);
var subImg1Rect = new Rect(36 + clipOffset, 72, 132 + clipOffset, 216);
//Clip subImg1Clip = rectClip(subImg1Rect);
Clip subImg1Clip = ellipseClip(subImg1Rect);
subImg1.Matrix = CalcImagePlaceToClip(subImg1, posArray[i,0], posArray[i,1], posArray[i,2], posArray[i,3], subImg1Rect);
subImg1.Clip = subImg1Clip;
docpage.Content.AddElement(subImg1);
}
docpage.UpdateContent();
}
doc.Save(SaveFlags.Full, sOutput);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.