c# - C#.NET 中 iTextSharp 中的自定义页面大小

标签 c# pdf itextsharp

我想在 C# 中使用 iTextSharp 创建自定义页面大小(5"X2")PDF。有什么办法吗?

Document doc = new Document(iTextSharp.text.PageSize.A4, 15, 15, 0, 0);

最佳答案

下面的代码将演示如何在 C#.net 中使用 PDF 坐标实现自定义 PDF。对于此任务,您必须了解 pdf 坐标。

  BaseFont f_cn;
  string  poath = Server.MapPath("~/fonts/CALIBRI.TTF");

 f_cn = BaseFont.CreateFont(poath, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

using (System.IO.FileStream fs = new FileStream(Server.MapPath("~/TempPdf") + "\\" + "download.pdf", FileMode.Create))
            {
 Document document = new Document(PageSize.A4, 25, 25, 30, 30);
                PdfWriter writer = PdfWriter.GetInstance(document, fs);                
                Paragraph p = new Paragraph();
                // Add meta information to the document
                document.AddAuthor("Mikael Blomquist");
                document.AddCreator("Sample application using iTestSharp");
                document.AddKeywords("PDF tutorial education");
                document.AddSubject("Document subject - Describing the steps creating a PDF document");
                document.AddTitle("The document title - Amplified Resource Group");
                // Open the document to enable you to write to the document
                document.Open();
                // Makes it possible to add text to a specific place in the document using 
                // a X & Y placement syntax.
                PdfContentByte cb = writer.DirectContent;
                cb.SetFontAndSize(f_cb, 16);
                // First we must activate writing
                cb.BeginText();
                // Add an image to a fixed position from disk
                iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(Server.MapPath("~/images/arg.png"));
                png.SetAbsolutePosition(200, 738);
                cb.AddImage(png);
                writeText(cb, "Header", 30, 718, f_cb, 14);
}
 private void writeText(PdfContentByte cb, string Text, int X, int Y, BaseFont font, int Size)
    {
        cb.SetFontAndSize(font, Size);
        cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, Text, X, Y, 0);
  }

关于c# - C#.NET 中 iTextSharp 中的自定义页面大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17079021/

相关文章:

java - itext 绝对定位文字

c# - 使用 Mono Touch 的横向 iPad 应用程序

c# - 静态锁是否适用于不同的子类?

c# - 如何将 SonarLint 分析结果从 Visual Studio 发送到 SonarQube 服务器

java - 确定 PDF 页面中的行数

java - 使用 Java 编辑 PDF 文本

.net - ItextSharp 5.4.5 是否支持 256 位加密?

c# - EF Core 在调用中获取字符串的一部分

javascript - 如何使用 Javascript 生成 PDF?

c# - 在 C# 中使用 iTextSharp 阅读 pdf 内容