本文整理汇总了C#中System.Windows.Forms.Document.AddTemplate方法的典型用法代码示例。如果您正苦于以下问题:C# Document.AddTemplate方法的具体用法?C# Document.AddTemplate怎么用?C# Document.AddTemplate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Forms.Document
的用法示例。
在下文中一共展示了Document.AddTemplate方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddHtmlHeader
private void AddHtmlHeader(Document document)
{
string headerAndFooterHtmlUrl = Path.Combine(Application.StartupPath, @"..\..\HeaderFooter\HeaderAndFooterHtml.htm");
//create a template to be added in the header and footer
document.Header = document.AddTemplate(document.Pages[0].ClientRectangle.Width, 60);
// create a HTML to PDF converter element to be added to the header template
HtmlToPdfElement headerHtmlToPdf = new HtmlToPdfElement(0, 0, document.Header.ClientRectangle.Width,
document.Header.ClientRectangle.Height, headerAndFooterHtmlUrl);
headerHtmlToPdf.FitHeight = true;
document.Header.AddElement(headerHtmlToPdf);
}
示例2: AddHtmlFooter
private void AddHtmlFooter(Document document, PdfFont footerPageNumberFont)
{
string headerAndFooterHtmlUrl = Path.Combine(Application.StartupPath, @"..\..\HeaderFooter\HeaderAndFooterHtml.htm");
//create a template to be added in the header and footer
document.Footer = document.AddTemplate(document.Pages[0].ClientRectangle.Width, 60);
// create a HTML to PDF converter element to be added to the header template
HtmlToPdfElement footerHtmlToPdf = new HtmlToPdfElement(0, 0, document.Footer.ClientRectangle.Width,
document.Footer.ClientRectangle.Height, headerAndFooterHtmlUrl);
footerHtmlToPdf.FitHeight = true;
document.Footer.AddElement(footerHtmlToPdf);
// add page number to the footer
TextElement pageNumberText = new TextElement(document.Footer.ClientRectangle.Width - 100, 30,
"This is page &p; of &P; pages", footerPageNumberFont);
document.Footer.AddElement(pageNumberText);
}
示例3: btnCreatePdf_Click
private void btnCreatePdf_Click(object sender, EventArgs e)
{
string pdfToModify = textBoxPdfFilePath.Text.Trim();
// create a PDF document
Document document = new Document(pdfToModify);
// set the license key
document.LicenseKey = "B4mYiJubiJiInIaYiJuZhpmahpGRkZE=";
// get the first page the PDF document
PdfPage firstPage = document.Pages[0];
string logoTransImagePath = System.IO.Path.Combine(Application.StartupPath, @"..\..\img\evologo-100-trans.png");
string logoOpaqueImagePath = System.IO.Path.Combine(Application.StartupPath, @"..\..\img\evologo-100.jpg");
// add an opaque image stamp in the top left corner of the first page
// and make it semitransparent when rendered in PDF
ImageElement imageStamp = new ImageElement(1, 1, logoOpaqueImagePath);
imageStamp.Opacity = 50;
AddElementResult addResult = firstPage.AddElement(imageStamp);
// add a border for the image stamp
RectangleElement imageBorderRectangleElement = new RectangleElement(1, 1, addResult.EndPageBounds.Width,
addResult.EndPageBounds.Height);
firstPage.AddElement(imageBorderRectangleElement);
// add a template stamp to the document repeated on each document page
// the template contains an image and a text
System.Drawing.Image logoImg = System.Drawing.Image.FromFile(logoTransImagePath);
// calculate the template stamp location and size
System.Drawing.SizeF imageSizePx = logoImg.PhysicalDimension;
float imageWidthPoints = UnitsConverter.PixelsToPoints(imageSizePx.Width);
float imageHeightPoints = UnitsConverter.PixelsToPoints(imageSizePx.Height);
float templateStampXLocation = (firstPage.ClientRectangle.Width - imageWidthPoints) / 2;
float templateStampYLocation = firstPage.ClientRectangle.Height / 4;
// the stamp size is equal to image size in points
Template templateStamp = document.AddTemplate(new System.Drawing.RectangleF(templateStampXLocation, templateStampYLocation,
imageWidthPoints, imageHeightPoints + 20));
// set a semitransparent background color for template
RectangleElement background = new RectangleElement(0, 0, templateStamp.ClientRectangle.Width, templateStamp.ClientRectangle.Height);
background.BackColor = Color.White;
background.Opacity = 25;
templateStamp.AddElement(background);
// add a true type font to the document
System.Drawing.Font ttfFontBoldItalic = new System.Drawing.Font("Times New Roman", 10,
System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point);
PdfFont templateStampTextFont = document.AddFont(ttfFontBoldItalic, true);
// Add a text element to the template. You can add any other types of elements to a template like a HtmlToPdfElement.
TextElement templateStampTextElement = new TextElement(3, 0, "This is the Stamp Text", templateStampTextFont);
templateStampTextElement.ForeColor = System.Drawing.Color.DarkBlue;
templateStamp.AddElement(templateStampTextElement);
// Add an image with transparency to the template. You can add any other types of elements to a template like a HtmlToPdfElement.
ImageElement templateStampImageElement = new ImageElement(0, 20, logoImg);
// instruct the library to use transparency information
templateStampImageElement.RenderTransparentImage = true;
templateStamp.AddElement(templateStampImageElement);
// add a border to template
RectangleElement templateStampRectangleElement = new RectangleElement(0, 0, templateStamp.ClientRectangle.Width,
templateStamp.ClientRectangle.Height);
templateStamp.AddElement(templateStampRectangleElement);
// dispose the image
logoImg.Dispose();
string outFilePath = System.IO.Path.Combine(Application.StartupPath, "PdfStamps.pdf");
// save the PDF document to disk
try
{
document.Save(outFilePath);
}
finally
{
document.Close();
}
DialogResult dr = MessageBox.Show("Open the saved file in an external viewer?", "Open Rendered File", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
try
{
System.Diagnostics.Process.Start(outFilePath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
//.........这里部分代码省略.........