这篇文章主要为大家详细介绍了C#生成PDF文件流的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要为大家详细介绍了C#生成PDF文件流的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了C#生成PDF文件流的具体代码,供大家参考,具体内容如下
1、设置字体
?
|
1
2
3
4
5
|
static BaseFont FontBase = BaseFont.CreateFont("C:\WINDOWS\FONTS\STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
static iTextSharp.text.Font bodyFont = new iTextSharp.text.Font(FontBase, 12);
static iTextSharp.text.Font titleFont = new iTextSharp.text.Font(FontBase, 18);
static iTextSharp.text.Font paragraphFont = new iTextSharp.text.Font(FontBase, 15);
static iTextSharp.text.Font linkFont = new iTextSharp.text.Font(FontBase, 12, Font.UNDERLINE, BaseColor.BLUE);
|
2.生成PDF文件流返回byte数组
-
public byte[] DocCreate(System.Drawing.Image image, List<TreeNodes> list)
-
{
-
MemoryStream file = new MemoryStream();
-
-
string fileName = string.Empty;
-
Rectangle page = PageSize.A4;
-
float y = page.Height;
-
Document document = new Document(page, 15, 15, 30, 30);
-
float docWidth = page.Width - 15 * 2;
-
float docHeight = page.Height - document.BottomMargin - document.TopMargin;
-
PdfWriter writer = PdfWriter.GetInstance(document, file);
-
writer.CloseStream = false;
-
writer.Open();
-
PdfContentByte cb = writer.DirectContent;
-
document.Open();
-
-
Paragraph title = new Paragraph(new Chunk("标题", titleFont));
-
title.Alignment = Element.ALIGN_CENTER;
-
document.Add(title);
-
-
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(image, ImageFormat.Png);
-
float widthSzie = (page.Width - 30) / img.Width;
-
if (widthSzie < 1)
-
{
-
img.ScalePercent(widthSzie * 100);
-
}
-
document.Add(img);
-
-
Paragraph p2 = new Paragraph(new Chunk("出处", paragraphFont));
-
p2.IndentationLeft = indentationLeft;
-
document.Add(p2);
-
InitData(list);
-
CreateSteps(list, document, list.FirstOrDefault(it => it.PID == 0));
-
-
-
-
-
-
-
-
Paragraph createtime = new Paragraph(new Chunk(DateTime.Now.ToLongDateString().ToString(), bodyFont));
-
createtime.Alignment = Element.ALIGN_RIGHT;
-
-
createtime.PaddingTop = 200;
-
-
document.Add(createtime);
-
-
-
document.Close();
-
file.Position = 0;
-
MemoryStream newfile = SetWaterMark(file, "水印内容", docWidth, docHeight);
-
newfile.Position = 0;
-
byte[] bytes = new byte[newfile.Length];
-
newfile.Read(bytes, 0, bytes.Length);
-
return bytes;
-
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持米米素材网。
发表评论