//////////////////////////////////////////////////////////////////// // Default constructor // Default page size is letter (height 11”, width 8.5”) // Page orientation is portrait // Unit of measure is points (scale factor 1.0) //////////////////////////////////////////////////////////////////// public PdfDocument() { // initialize object array ObjectArray = new List <PdfObject>(); // PDF document root object the Catalog object PdfObject Catalog = new PdfObject(this, false, "/Catalog"); // add viewer preferences Catalog.AddToDictionary("/ViewerPreferences", "<</PrintScaling/None>>"); // Parent object for all pages PagesObject = new PdfObject(this, false, "/Pages"); // add indirect reference to pages within the catalog object Catalog.AddToDictionary("/Pages", PagesObject); // create standard proc set ProcSetObject = new PdfObject(this, false); ProcSetObject.ContentsString = new StringBuilder("[/PDF/Text/ImageB/ImageC/ImageI]"); // save page default size PageSize = new SizeD(8.5 * 72.0, 11.0 * 72.0); // scale factor ScaleFactor = 1.0; // exit return; }
//////////////////////////////////////////////////////////////////// /// <summary> /// Calculates image size to preserve aspect ratio. /// </summary> /// <param name="InputSize">Image display area.</param> /// <returns>Adjusted image display area.</returns> /// <remarks> /// Calculates best fit to preserve aspect ratio. /// </remarks> //////////////////////////////////////////////////////////////////// public SizeD ImageSize ( SizeD InputSize ) { return(ImageSizePos.ImageSize(WidthPix, HeightPix, InputSize.Width, InputSize.Height)); }
//////////////////////////////////////////////////////////////////// // Constructor // Default page is Letter, Legal or A4 // Default page orientation is portrait or landscape // Unit of measure is: Point, Inch, cm, mm //////////////////////////////////////////////////////////////////// public PdfDocument ( PaperType PaperType, // Letter, Legal, A4 Boolean Landscape, // if true width > height, if false height >= width UnitOfMeasure UnitOfMeasure // Point, Inch, Cm, mm ) { // initialize object array InitialObjectArray(); // set scale factor (user units to points) ScaleFactor = UnitInPoints[(Int32)UnitOfMeasure]; // get standard paper size PageSize = new SizeD(PaperTypeSize[(Int32)PaperType].Width, PaperTypeSize[(Int32)PaperType].Height); // for landscape swap width and height if (Landscape) { Double Temp = PageSize.Width; PageSize.Width = PageSize.Height; PageSize.Height = Temp; } // exit return; }
//////////////////////////////////////////////////////////////////// // set image density //////////////////////////////////////////////////////////////////// public SizeD ImageSizeAndDensity ( double Width, double Height, double Density ) { // make sure width and height have the same ratio as original image SizeD Size = ImageSize(Width, Height); // convert to pixels Int32 NewWidth = (Int32)(Density * Size.Width + 0.5); Int32 NewHeight = (Int32)(Density * Size.Height + 0.5); //if new size is greater than original do nothing if (NewWidth >= WidthPix || NewHeight >= HeightPix) { this.NewWidthPix = 0; this.NewHeightPix = 0; this.NewDensity = 0; } else { this.NewWidthPix = NewWidth; this.NewHeightPix = NewHeight; this.NewDensity = Density; } return(Size); }
//////////////////////////////////////////////////////////////////// /// <summary> /// Calculates image size to preserve aspect ratio and sets position. /// </summary> /// <param name="InputSize">Image display area</param> /// <param name="Alignment">Content alignment</param> /// <returns>Adjusted image size and position within area.</returns> /// <remarks> /// Calculates best fit to preserve aspect ratio and adjust /// position according to content alignment argument. /// </remarks> //////////////////////////////////////////////////////////////////// public PdfRectangle ImageSizePosition ( SizeD InputSize, ContentAlignment Alignment ) { return(ImageSizePos.ImageArea(WidthPix, HeightPix, 0.0, 0.0, InputSize.Width, InputSize.Height, Alignment)); }
//////////////////////////////////////////////////////////////////// // Constructor // PageSize override the default page size //////////////////////////////////////////////////////////////////// public PdfPage ( PdfDocument Document, SizeD PageSize ) : base(Document, false, "/Page") { PdfPageConstructor(ToPt(PageSize.Width), ToPt(PageSize.Height)); return; }
//////////////////////////////////////////////////////////////////// /// <summary> /// Constructor /// </summary> /// <param name="Document">Parent PDF document object</param> /// <param name="PageSize">Paper size for this page</param> /// <remarks> /// PageSize override the default page size /// </remarks> //////////////////////////////////////////////////////////////////// public PdfPage ( PdfDocument Document, SizeD PageSize ) : base(Document, ObjectType.Dictionary, "/Page") { ConstructorHelper(ToPt(PageSize.Width), ToPt(PageSize.Height)); return; }
//////////////////////////////////////////////////////////////////// /// <summary> /// Constructor /// </summary> /// <param name="Document">Parent PDF document object</param> /// <param name="PageSize">Paper size for this page</param> /// <remarks> /// PageSize override the default page size /// </remarks> //////////////////////////////////////////////////////////////////// public PdfPage ( PdfDocument Document, SizeD PageSize ) : base(Document, ObjectType.Dictionary, "/Page") { Width = ScaleFactor * PageSize.Width; Height = ScaleFactor * PageSize.Height; ConstructorHelper(); }
//////////////////////////////////////////////////////////////////// /// <summary> /// Create eliptical arc /// </summary> /// <param name="ArcStart">Arc start point</param> /// <param name="ArcEnd">Arc end point</param> /// <param name="Radius">RadiusX as width and RadiusY as height</param> /// <param name="Rotate">X axis rotation angle in radians</param> /// <param name="Type">Arc type enumeration</param> /// <returns>Array of points.</returns> //////////////////////////////////////////////////////////////////// public static PointD[] CreateArc ( PointD ArcStart, PointD ArcEnd, SizeD Radius, double Rotate, ArcType Type ) { PointD[] SegArray; var ScaleX = Radius.Width / Radius.Height; // circular arc if (Math.Abs(ScaleX - 1.0) < 0.000001) { SegArray = CircularArc(ArcStart, ArcEnd, Radius.Height, Type); } // eliptical arc else if (Rotate == 0.0) { var ScaleStart = new PointD(ArcStart.X / ScaleX, ArcStart.Y); var ScaleEnd = new PointD(ArcEnd.X / ScaleX, ArcEnd.Y); SegArray = CircularArc(ScaleStart, ScaleEnd, Radius.Height, Type); foreach (var Seg in SegArray) { Seg.X *= ScaleX; } } // eliptical arc rotated else { var CosR = Math.Cos(Rotate); var SinR = Math.Sin(Rotate); var ScaleStart = new PointD((CosR * ArcStart.X - SinR * ArcStart.Y) / ScaleX, SinR * ArcStart.X + CosR * ArcStart.Y); var ScaleEnd = new PointD((CosR * ArcEnd.X - SinR * ArcEnd.Y) / ScaleX, SinR * ArcEnd.X + CosR * ArcEnd.Y); SegArray = CircularArc(ScaleStart, ScaleEnd, Radius.Height, Type); foreach (var Seg in SegArray) { var X = Seg.X * ScaleX; Seg.X = CosR * X + SinR * Seg.Y; Seg.Y = -SinR * X + CosR * Seg.Y; } } // replace start and end with original points to eliminate rounding errors SegArray[0].X = ArcStart.X; SegArray[0].Y = ArcStart.Y; SegArray[SegArray.Length - 1].X = ArcEnd.X; SegArray[SegArray.Length - 1].Y = ArcEnd.Y; return(SegArray); }
//////////////////////////////////////////////////////////////////// // Default constructor // Default page size is letter (height 11”, width 8.5”) // Page orientation is portrait // Unit of measure is points (scale factor 1.0) //////////////////////////////////////////////////////////////////// public PdfDocument() { // initialize object array InitialObjectArray(); // scale factor ScaleFactor = 1.0; // save page default size PageSize = new SizeD(8.5 * 72.0, 11.0 * 72.0); // exit return; }
//////////////////////////////////////////////////////////////////// // Initial Object Array //////////////////////////////////////////////////////////////////// private void ConstructorHelper ( Double Width, // page width Double Height, // page height Double ScaleFactor, // scale factor from user units to points (i.e. 72.0 for inch) String FileName ) { // set scale factor (user units to points) this.ScaleFactor = ScaleFactor; // save page default size PageSize = new SizeD(Width, Height); // PDF document root object the Catalog object CatalogObject = new PdfObject(this, ObjectType.Dictionary, "/Catalog"); // add viewer preferences CatalogObject.Dictionary.Add("/ViewerPreferences", "<</PrintScaling/None>>"); // Parent object for all pages PagesObject = new PdfObject(this, ObjectType.Dictionary, "/Pages"); // add indirect reference to pages within the catalog object CatalogObject.Dictionary.AddIndirectReference("/Pages", PagesObject); // document id DocumentID = RandomByteArray(16); // save file name this.FileName = FileName; // convert stream to binary writer PdfFile = new PdfBinaryWriter(new FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.None)); // write PDF version number PdfFile.WriteString("%PDF-1.7\n"); // add this comment to tell compression programs that this is a binary file PdfFile.WriteString("%\u00b5\u00b5\u00b5\u00b5\n"); // exit return; }
//////////////////////////////////////////////////////////////////// // Calculate best fit to preserve aspect ratio //////////////////////////////////////////////////////////////////// public SizeD ImageSize ( SizeD InputSize ) { SizeD OutputSize = new SizeD(); OutputSize.Height = HeightFromWidth(InputSize.Width); if (OutputSize.Height <= InputSize.Height) { OutputSize.Width = InputSize.Width; } else { OutputSize.Width = WidthFromHeight(InputSize.Height); OutputSize.Height = InputSize.Height; } return(OutputSize); }
//////////////////////////////////////////////////////////////////// // Constructor // Default page size is Width and Height in user unit of measure // Default page orientation is portrait if Height > Width landscape if Height < Width // Scale factor from user unit of measure to points (i.e. 72.0 for inch) //////////////////////////////////////////////////////////////////// public PdfDocument ( Double Width, // page width Double Height, // page height Double ScaleFactor // scale factor from user units to points (i.e. 72.0 for inch) ) { // initialize object array InitialObjectArray(); // set scale factor (user units to points) this.ScaleFactor = ScaleFactor; // save page default size PageSize = new SizeD(Width * ScaleFactor, Height * ScaleFactor); // exit return; }
//////////////////////////////////////////////////////////////////// // Constructor // Default page size is Width and Height in user unit of measure // Default page orientation is portrait if Height > Width landscape if Height < Width // Unit of measure is: Point, Inch, cm, mm //////////////////////////////////////////////////////////////////// public PdfDocument ( Double Width, // page width Double Height, // page height UnitOfMeasure UnitOfMeasure // unit of measure: Point, Inch, cm, mm ) { // initialize object array InitialObjectArray(); // set scale factor (user units to points) ScaleFactor = UnitInPoints[(Int32)UnitOfMeasure]; // save page default size PageSize = new SizeD(Width * ScaleFactor, Height * ScaleFactor); // exit return; }
//////////////////////////////////////////////////////////////////// // Calculate best fit to preserve aspect ratio //////////////////////////////////////////////////////////////////// public SizeD ImageSize ( Double Width, Double Height ) { SizeD OutputSize = new SizeD(); OutputSize.Height = HeightFromWidth(Width); if (OutputSize.Height <= Height) { OutputSize.Width = Width; } else { OutputSize.Width = WidthFromHeight(Height); OutputSize.Height = Height; } return(OutputSize); }
//////////////////////////////////////////////////////////////////// /// <summary> /// Calculate best fit to preserve aspect ratio /// </summary> /// <param name="ImageWidthPix">Image width in pixels.</param> /// <param name="ImageHeightPix">Image height in pixels.</param> /// <param name="DrawAreaWidth">Drawing area width.</param> /// <param name="DrawAreaHeight">Drawing area height.</param> /// <returns>Image size in user units.</returns> //////////////////////////////////////////////////////////////////// public static SizeD ImageSize ( Int32 ImageWidthPix, Int32 ImageHeightPix, Double DrawAreaWidth, Double DrawAreaHeight ) { SizeD OutputSize = new SizeD(); OutputSize.Height = DrawAreaWidth * ImageHeightPix / ImageWidthPix; if (OutputSize.Height <= DrawAreaHeight) { OutputSize.Width = DrawAreaWidth; } else { OutputSize.Width = DrawAreaHeight * ImageWidthPix / ImageHeightPix; OutputSize.Height = DrawAreaHeight; } return(OutputSize); }
//////////////////////////////////////////////////////////////////// // Initial Object Array //////////////////////////////////////////////////////////////////// private void ConstructorHelper( Double Width, // page width Double Height, // page height Double ScaleFactor, // scale factor from user units to points (i.e. 72.0 for inch) String FileName, Stream OutputStream ) { // set scale factor (user units to points) this.ScaleFactor = ScaleFactor; // save page default size PageSize = new SizeD(Width, Height); // PDF document root object the Catalog object CatalogObject = new PdfObject(this, ObjectType.Dictionary, "/Catalog"); // add viewer preferences CatalogObject.Dictionary.Add("/ViewerPreferences", "<</PrintScaling/None>>"); // Parent object for all pages PagesObject = new PdfObject(this, ObjectType.Dictionary, "/Pages"); // add indirect reference to pages within the catalog object CatalogObject.Dictionary.AddIndirectReference("/Pages", PagesObject); // document id DocumentID = RandomByteArray(16); // create file using file name if(FileName != null) { // save file name this.FileName = FileName; // constructor helper PdfFile = new PdfBinaryWriter(new FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.None)); } // write to caller's file or memory stream else { PdfFile = new PdfBinaryWriter(OutputStream); } // write PDF version number PdfFile.WriteString("%PDF-1.7\n"); // add this comment to tell compression programs that this is a binary file PdfFile.WriteString("%\u00b5\u00b5\u00b5\u00b5\n"); // exit return; }
//////////////////////////////////////////////////////////////////// // Initial Object Array //////////////////////////////////////////////////////////////////// private void ConstructorHelper ( double Width, // page width double Height, // page height double ScaleFactor, // scale factor from user units to points (i.e. 72.0 for inch) string FileName, Stream OutputStream ) { // set scale factor (user units to points) this.ScaleFactor = ScaleFactor; // set epsilon (1/300 of an inch in user units) Epsilon = 72.0 / (300.0 * ScaleFactor); // save page default size PageSize = new SizeD(Width, Height); // PDF document root object the Catalog object CatalogObject = new PdfObject(this, ObjectType.Dictionary, "/Catalog"); // add viewer preferences CatalogObject.Dictionary.Add("/ViewerPreferences", "<</PrintScaling/None>>"); // Parent object for all pages PagesObject = new PdfObject(this, ObjectType.Dictionary, "/Pages"); // add indirect reference to pages within the catalog object CatalogObject.Dictionary.AddIndirectReference("/Pages", PagesObject); // create trailer dictionary TrailerDict = new PdfDictionary(this); // add /Root TrailerDict.AddIndirectReference("/Root", CatalogObject); // document id DocumentID = RandomByteArray(16); // add /ID TrailerDict.AddFormat("/ID", "[{0}{0}]", ByteArrayToPdfHexString(DocumentID)); // create file using file name if (FileName != null) { // save file name this.FileName = FileName; // constructor helper PdfFile = new PdfBinaryWriter(new FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.None)); } // write to caller's file or memory stream else { PdfFile = new PdfBinaryWriter(OutputStream); } // write PDF version number PdfFile.WriteString("%PDF-1.7\n"); // add this comment to tell compression programs that this is a binary file PdfFile.WriteString("%\u00b5\u00b5\u00b5\u00b5\n"); // exit }