【问题标题】:How to write in a specific location the zapfdingbatslist in a pdf document using iTextSharp如何使用 iTextSharp 在 pdf 文档中的特定位置写入 zapfdingbatslist
【发布时间】:2013-05-04 03:57:23
【问题描述】:

我想在 iTextSharp 中的 特定位置 中使用 zapfdingbatslist 在我的 pdf 文档上打上复选标记。

到目前为止,我可以做的是显示复选标记,但它都在文档的一侧,而不是在我想要的特定 X、Y 坐标上。我在下面有一个代码,希望能让你明白我在说什么。

    String outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
    System.IO.FileStream fs;
    Document doc = new Document(PageSize.A4, 20, 20, 10, 10);
    PdfContentByte cb;
    PdfWriter writer;

    fs = new System.IO.FileStream(outputFile, System.IO.FileMode.Create);
    writer = PdfWriter.GetInstance(doc, fs);
    doc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
    writer.AddViewerPreference(PdfName.PICKTRAYBYPDFSIZE, PdfBoolean.PDFTRUE);

    doc.Open();
    cb = writer.DirectContent;

    List myList = new ZapfDingbatsList(52); // Check mark symbol from Dingbatslist

    // I have some more code here but it is not needed for the problem
    // and thererfore not shown


    // I could shohw the dingbatslist check mark here
    myList.Add(" ");
    doc.Add(myList); // However I want to show it in a specific X, Y position

    doc.Close();
    writer.Close();
    fs.Close();

有什么想法吗?

【问题讨论】:

    标签: c# pdf itextsharp


    【解决方案1】:

    顺便说一句,我是大部分 iText documentation 的作者(以及 iText 的原始开发者,包括 DocumentPdfWriterZapfdingbatsList、... 类),并且我'如果您花一些时间阅读 iText 文档,将不胜感激。

    让我们从chapter 2 开始,看看引入Font 类的some C# examples

    例如:

    Font font = new Font(Font.FontFamily.ZAPFDINGBATS, 12);
    

    一旦你有了一个 Font 对象,你就可以创建一个Phrase(也在第 2 章中解释过):

    Phrase phrase = new Phrase(zapfstring, font); 
    

    zapfstring 是一个 string,其中包含您想要的任何 Zapfdingbats 字符。

    要在绝对位置添加此短语,您需要阅读第 3 章。查看the examples 以获得灵感,例如FoobarFilmfestival.cs

    PdfContentByte canvas = writer.DirectContent;
    ColumnText.ShowTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 500, 0); 
    

    其中200500 是X 和Y 坐标,0 是以度数表示的角度。除了ALIGN_CENTER,您还可以选择ALIGN_RIGHTALIGN_LEFT

    在您的代码示例中,您使用列表将 Zapfdingbats 字形添加到文档中。请花点时间把自己放在我的位置上。是不是觉得你是参加红辣椒音乐会的袜子的发明者?

    【讨论】:

    • 这是我喜欢的地方,所以你可以培养出许多聪明的头脑。有多少人会直接从一本优质书籍的作者那里得到答案?估计不会太多。谢谢@BrunoLowagie 先生,我非常感谢您的正确回复/解决方案。上帝保佑。
    • 我的部分工作是回答问题,但参与 SO 很有趣 ;-)
    猜你喜欢
    • 2014-12-31
    • 1970-01-01
    • 2012-01-21
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    相关资源
    最近更新 更多