/// <summary> /// PDF Creator Helper With No Page Base (Don't want to write a page or report (header or footer) /// </summary> /// <param name="PageSize">Page Size - A4 is a default page size. Use Enum iTextSharp.text.PageSize</param> /// <param name="LandscapeMode">Do you want the pdf in landscape</param> /// <param name="MarginTop">Top Margin On The Page</param> /// <param name="MarginRight">Right Margin On The Page</param> /// <param name="MarginBottom">Bottom Margin On The Page</param> /// <param name="MarginLeft">Left Margin On The Page</param> /// <param name="PageEventHandler">A Page Event Class That Will Raise Any Events You Need</param> public PDFCreator(Rectangle PageSize, bool LandscapeMode, float MarginTop, float MarginRight, float MarginBottom, float MarginLeft, PageEventsBase PageEventHandler) { //create the instance of the ITextSharpDocument Doc = new Document(PageSize, MarginLeft, MarginRight, MarginTop, MarginBottom); //let's build the memory stream now Ms = new MemoryStream(); //let's create the new writer and attach the document Writer = PdfWriter.GetInstance(Doc, Ms); //add the page events to my custom class if (PageEventHandler != null) { Writer.PageEvent = PageEventHandler; } //if you want the pdf in landscape now, then rotate it if (LandscapeMode) { Doc.SetPageSize(PageSize.Rotate()); } //let's open the document so the developer can do whatever they wan't with it Doc.Open(); }
// Set paper size and orientation public void SetPaper(string size, string orientation) { try { _size = PageSize.GetRectangle(size); if (SameText(orientation, "landscape")) // Landscape { _size = _size.Rotate(); } } catch { _size = PageSize.GetRectangle(size); } }
/** * This method tries to fit the <code>Rectangle pageSize</code> to one of the predefined PageSize rectangles. * If a match is found the pageWidth and pageHeight will be set according to values determined from files * generated by MS Word2000 and OpenOffice 641. If no match is found the method will try to match the rotated * Rectangle by calling itself with the parameter rotate set to true. * * @param pageSize the page size for which to guess the correct format * @param rotate Whether we should try to rotate the size befor guessing the format * @return <code>True</code> if the format was guessed, <code>false/<code> otherwise */ private bool GuessFormat(Rectangle pageSize, bool rotate) { if (rotate) { pageSize = pageSize.Rotate(); } if (RectEquals(pageSize, PageSize.A3)) { pageWidth = 16837; pageHeight = 23811; landscape = rotate; return true; } if (RectEquals(pageSize, PageSize.A4)) { pageWidth = 11907; pageHeight = 16840; landscape = rotate; return true; } if (RectEquals(pageSize, PageSize.A5)) { pageWidth = 8391; pageHeight = 11907; landscape = rotate; return true; } if (RectEquals(pageSize, PageSize.A6)) { pageWidth = 5959; pageHeight = 8420; landscape = rotate; return true; } if (RectEquals(pageSize, PageSize.B4)) { pageWidth = 14570; pageHeight = 20636; landscape = rotate; return true; } if (RectEquals(pageSize, PageSize.B5)) { pageWidth = 10319; pageHeight = 14572; landscape = rotate; return true; } if (RectEquals(pageSize, PageSize.HALFLETTER)) { pageWidth = 7927; pageHeight = 12247; landscape = rotate; return true; } if (RectEquals(pageSize, PageSize.LETTER)) { pageWidth = 12242; pageHeight = 15842; landscape = rotate; return true; } if (RectEquals(pageSize, PageSize.LEGAL)) { pageWidth = 12252; pageHeight = 20163; landscape = rotate; return true; } if (!rotate && GuessFormat(pageSize, true)) { int x = pageWidth; pageWidth = pageHeight; pageHeight = x; return true; } return false; }
/** * Gets the main appearance layer. * <p> * Consult <A HREF="http://partners.adobe.com/asn/developer/pdfs/tn/PPKAppearances.pdf">PPKAppearances.pdf</A> * for further details. * </p> * @return the main appearance layer * @throws DocumentException on error * @throws IOException on error */ public PdfTemplate GetAppearance() { if (IsInvisible()) { PdfTemplate t = new PdfTemplate(writer); t.BoundingBox = new Rectangle(0, 0); writer.AddDirectTemplateSimple(t, null); return t; } if (app[0] == null) { PdfTemplate t = app[0] = new PdfTemplate(writer); t.BoundingBox = new Rectangle(100, 100); writer.AddDirectTemplateSimple(t, new PdfName("n0")); t.SetLiteral("% DSBlank\n"); } if (app[1] == null && !acro6Layers) { PdfTemplate t = app[1] = new PdfTemplate(writer); t.BoundingBox = new Rectangle(100, 100); writer.AddDirectTemplateSimple(t, new PdfName("n1")); t.SetLiteral(questionMark); } if (app[2] == null) { String text; if (layer2Text == null) { StringBuilder buf = new StringBuilder(); buf.Append("Digitally signed by ").Append(PdfPKCS7.GetSubjectFields((X509Certificate)certChain[0]).GetField("CN")).Append('\n'); buf.Append("Date: ").Append(signDate.ToString("yyyy.MM.dd HH:mm:ss zzz")); if (reason != null) buf.Append('\n').Append("Reason: ").Append(reason); if (location != null) buf.Append('\n').Append("Location: ").Append(location); text = buf.ToString(); } else text = layer2Text; PdfTemplate t = app[2] = new PdfTemplate(writer); t.BoundingBox = rect; writer.AddDirectTemplateSimple(t, new PdfName("n2")); if (image != null) { if (imageScale == 0) { t.AddImage(image, rect.Width, 0, 0, rect.Height, 0, 0); } else { float usableScale = imageScale; if (imageScale < 0) usableScale = Math.Min(rect.Width / image.Width, rect.Height / image.Height); float w = image.Width * usableScale; float h = image.Height * usableScale; float x = (rect.Width - w) / 2; float y = (rect.Height - h) / 2; t.AddImage(image, w, 0, 0, h, x, y); } } Font font; if (layer2Font == null) font = new Font(); else font = new Font(layer2Font); float size = font.Size; Rectangle dataRect = null; Rectangle signatureRect = null; if (Render == SignatureRender.NameAndDescription || (Render == SignatureRender.GraphicAndDescription && this.SignatureGraphic != null)) { // origin is the bottom-left signatureRect = new Rectangle( MARGIN, MARGIN, rect.Width / 2 - MARGIN, rect.Height - MARGIN); dataRect = new Rectangle( rect.Width / 2 + MARGIN / 2, MARGIN, rect.Width - MARGIN / 2, rect.Height - MARGIN); if (rect.Height > rect.Width) { signatureRect = new Rectangle( MARGIN, rect.Height / 2, rect.Width - MARGIN, rect.Height); dataRect = new Rectangle( MARGIN, MARGIN, rect.Width - MARGIN, rect.Height / 2 - MARGIN); } } else if (render == SignatureRender.Graphic) { if (signatureGraphic == null) { throw new InvalidOperationException(MessageLocalization.GetComposedMessage("a.signature.image.should.be.present.when.rendering.mode.is.graphic.only")); } signatureRect = new Rectangle( MARGIN, MARGIN, rect.Width - MARGIN, // take all space available rect.Height - MARGIN); } else { dataRect = new Rectangle( MARGIN, MARGIN, rect.Width - MARGIN, rect.Height * (1 - TOP_SECTION) - MARGIN); } if (Render == SignatureRender.NameAndDescription) { string signedBy = iTextSharp.text.pdf.PdfPKCS7.GetSubjectFields(this.certChain[0]).GetField("CN"); Rectangle sr2 = new Rectangle(signatureRect.Width - MARGIN, signatureRect.Height - MARGIN ); float signedSize = FitText(font, signedBy, sr2, -1, runDirection); ColumnText ct2 = new ColumnText(t); ct2.RunDirection = runDirection; ct2.SetSimpleColumn(new Phrase(signedBy, font), signatureRect.Left, signatureRect.Bottom, signatureRect.Right, signatureRect.Top, signedSize, Element.ALIGN_LEFT); ct2.Go(); } else if (Render == SignatureRender.GraphicAndDescription) { ColumnText ct2 = new ColumnText(t); ct2.RunDirection = runDirection; ct2.SetSimpleColumn(signatureRect.Left, signatureRect.Bottom, signatureRect.Right, signatureRect.Top, 0, Element.ALIGN_RIGHT); Image im = Image.GetInstance(SignatureGraphic); im.ScaleToFit(signatureRect.Width, signatureRect.Height); Paragraph p = new Paragraph(); // must calculate the point to draw from to make image appear in middle of column float x = 0; // experimentation found this magic number to counteract Adobe's signature graphic, which // offsets the y co-ordinate by 15 units float y = -im.ScaledHeight + 15; x = x + (signatureRect.Width - im.ScaledWidth) / 2; y = y - (signatureRect.Height - im.ScaledHeight) / 2; p.Add(new Chunk(im, x + (signatureRect.Width - im.ScaledWidth) / 2, y, false)); ct2.AddElement(p); ct2.Go(); } else if (render == SignatureRender.Graphic) { ColumnText ct2 = new ColumnText(t); ct2.RunDirection = runDirection; ct2.SetSimpleColumn(signatureRect.Left, signatureRect.Bottom, signatureRect.Right, signatureRect.Top, 0, Element.ALIGN_RIGHT); Image im = Image.GetInstance(signatureGraphic); im.ScaleToFit(signatureRect.Width, signatureRect.Height); Paragraph p = new Paragraph(); // must calculate the point to draw from to make image appear in middle of column float x = 0; // experimentation found this magic number to counteract Adobe's signature graphic, which // offsets the y co-ordinate by 15 units float y = -im.ScaledHeight + 15; x = x + (signatureRect.Width - im.ScaledWidth) / 2; y = y - (signatureRect.Height - im.ScaledHeight) / 2; p.Add(new Chunk(im, x, y, false)); ct2.AddElement(p); ct2.Go(); } if (render != SignatureRender.Graphic) { if (size <= 0) { Rectangle sr = new Rectangle(dataRect.Width, dataRect.Height); size = FitText(font, text, sr, 12, runDirection); } ColumnText ct = new ColumnText(t); ct.RunDirection = runDirection; ct.SetSimpleColumn(new Phrase(text, font), dataRect.Left, dataRect.Bottom, dataRect.Right, dataRect.Top, size, Element.ALIGN_LEFT); ct.Go(); } } if (app[3] == null && !acro6Layers) { PdfTemplate t = app[3] = new PdfTemplate(writer); t.BoundingBox = new Rectangle(100, 100); writer.AddDirectTemplateSimple(t, new PdfName("n3")); t.SetLiteral("% DSBlank\n"); } if (app[4] == null && !acro6Layers) { PdfTemplate t = app[4] = new PdfTemplate(writer); t.BoundingBox = new Rectangle(0, rect.Height * (1 - TOP_SECTION), rect.Right, rect.Top); writer.AddDirectTemplateSimple(t, new PdfName("n4")); Font font; if (layer2Font == null) font = new Font(); else font = new Font(layer2Font); float size = font.Size; String text = "Signature Not Verified"; if (layer4Text != null) text = layer4Text; Rectangle sr = new Rectangle(rect.Width - 2 * MARGIN, rect.Height * TOP_SECTION - 2 * MARGIN); size = FitText(font, text, sr, 15, runDirection); ColumnText ct = new ColumnText(t); ct.RunDirection = runDirection; ct.SetSimpleColumn(new Phrase(text, font), MARGIN, 0, rect.Width - MARGIN, rect.Height - MARGIN, size, Element.ALIGN_LEFT); ct.Go(); } int rotation = writer.reader.GetPageRotation(page); Rectangle rotated = new Rectangle(rect); int n = rotation; while (n > 0) { rotated = rotated.Rotate(); n -= 90; } if (frm == null) { frm = new PdfTemplate(writer); frm.BoundingBox = rotated; writer.AddDirectTemplateSimple(frm, new PdfName("FRM")); float scale = Math.Min(rect.Width, rect.Height) * 0.9f; float x = (rect.Width - scale) / 2; float y = (rect.Height - scale) / 2; scale /= 100; if (rotation == 90) frm.ConcatCTM(0, 1, -1, 0, rect.Height, 0); else if (rotation == 180) frm.ConcatCTM(-1, 0, 0, -1, rect.Width, rect.Height); else if (rotation == 270) frm.ConcatCTM(0, -1, 1, 0, 0, rect.Width); frm.AddTemplate(app[0], 0, 0); if (!acro6Layers) frm.AddTemplate(app[1], scale, 0, 0, scale, x, y); frm.AddTemplate(app[2], 0, 0); if (!acro6Layers) { frm.AddTemplate(app[3], scale, 0, 0, scale, x, y); frm.AddTemplate(app[4], 0, 0); } } PdfTemplate napp = new PdfTemplate(writer); napp.BoundingBox = rotated; writer.AddDirectTemplateSimple(napp, null); napp.AddTemplate(frm, 0, 0); return napp; }
/// <summary> /// Writes the report. /// </summary> /// <param name="report">The report.</param> /// <param name="destination">The destination.</param> public void WriteReport(Report report, Stream destination) { var pageSize = new Rectangle(report.RenderHints.PageSize.Width, report.RenderHints.PageSize.Height); if (report.RenderHints.Orientation == ReportOrientation.Landscape) { pageSize = pageSize.Rotate(); } var margins = report.RenderHints.Margins; var doc = new Document(pageSize, margins.Width, margins.Width, margins.Height, margins.Height); using (PdfWriter.GetInstance(doc, destination)) { doc.Open(); var globalTable = new PdfPTable(1) { HeaderRows = 1, WidthPercentage = 100 }; // Render the header RenderHeader(globalTable, report.TextFields, report.RenderHints); // Render all the rows int fieldsCount = report.DataFields.Count(f => !f.Hidden); var table = new PdfPTable(fieldsCount) { HeaderRows = 1, WidthPercentage = 100 }; globalTable.AddCell(new PdfPCell(table) { Border = 0 }); foreach (ReportRow row in report.GetRows()) { foreach (RowField field in row.Fields.Where(f => !f.Hidden)) { PdfPCell cell; if (row.RowType == ReportRowType.HeaderRow) { cell = CreateTextCell(field.HeaderStyle, report.RenderHints[FontFamily] as string, field.HeaderText); } else { var url = row.GetUrlString(field); cell = url == null ? CreateTextCell(field.DataStyle, report.RenderHints[FontFamily] as string, row.GetFormattedValue(field)) : CreateHyperLinkCell(url, row.GetFormattedValue(field)); } cell.Border = 0; cell.Padding = 5; table.AddCell(cell); } } // Render the footer RenderFooter(globalTable, report.TextFields, report.RenderHints); doc.Add(globalTable); doc.Close(); } }
/// <summary> /// implement for PDFTemplate /// this will call when initialize PDFTemplate object! /// </summary> protected override void SetPageDefWidthHeight() { // this will call from PDFTemplate._buildPageDef() pageSize = PDFDrawItextSharpHelper.PageSize( Helper.GetAttributeValue("pagesize", PageDefinition.PageDefAttrs, "A4")); if (Helper.GetAttributeValue( "pageorientation", PageDefinition.PageDefAttrs, "landscape").ToUpper() == "LANDSCAPE") { pageSize = pageSize.Rotate(); } PageDefinition.Width = pageSize.Width; PageDefinition.Height = pageSize.Height; }