C#.NET 中 iTextSharp 中的自定义页面大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/17079021/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:31:06  来源:igfitidea点击:
Custom page size in iTextSharp in C#.NET
提问by senps
I want to create a custom page size which is (5"X2") PDF using iTextSharp in C#. Is there any way to do this?
我想在 C# 中使用 iTextSharp 创建一个自定义页面大小(5“X2”)PDF。有没有办法做到这一点?
Document doc = new Document(iTextSharp.text.PageSize.A4, 15, 15, 0, 0);
采纳答案by Anto
Below code will demonstrate how to implement the custom PDF using PDF coordinates in C#.net. For this task you must aware about the pdf coordinates.
下面的代码将演示如何在 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);
  }

