使用C#生成PDF文件流的代码案例分享


当前第2页 返回上一页

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

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));//添加业务数据

      ////添加印章

      //iTextSharp.text.Image whyz = iTextSharp.text.Image.GetInstance(whyzPath);

      //whyz.ScalePercent(50);

      //whyz.PaddingTop = 100;

      //whyz.Alignment = Element.ALIGN_RIGHT;

      //document.Add(whyz);

      //添加日期

      Paragraph createtime = new Paragraph(new Chunk(DateTime.Now.ToLongDateString().ToString(), bodyFont));

      createtime.Alignment = Element.ALIGN_RIGHT;

      //createtime.SpacingBefore = -80;

      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;

    }

以上就是使用C#生成PDF文件流的代码案例分享的详细内容!

返回前面的内容

相关阅读 >>

C#中如何操作word的方法示例

详解C#获取本机ip地址(ipv4)的代码案例

C#开发实例-订制屏幕截图工具(二)创建项目、注册热键、显示截图主窗口

C# 日志记录类创建的源码分享

具体介绍C#线程与线程池的区别

详解C#多线程之线程同步(图文)

C#中反射是什么?

C#系列文章事件

详细介绍C#语言中字符类char的使用方法总结

简单介绍C#中数组、arraylist、list、dictionary的用法与区别

更多相关阅读请进入《C#》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...