Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

c# - How to write in a specific location the zapfdingbatslist in a pdf document using iTextSharp

I want to put a check mark using zapfdingbatslist on my pdf document in iTextSharp in a specific location.

What I could do so far is I could show the check mark but it's all on the side of the document and not on a specific X, Y coordinate that I want it to be. I have a code below that would hopefully give you the idea of what I am talking about.

    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();

Any idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Incidentally, I'm the author of most of the iText documentation (as well as the original developer of iText, including the Document, PdfWriter, ZapfdingbatsList,... classes), and I'd appreciate it if you took some time to read the iText documentation.

Let's start with chapter 2 and take a look at some C# examples that introduce the Font class.

For instance:

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

Once you have a Font object, you can create a Phrase (also explained in chapter 2):

Phrase phrase = new Phrase(zapfstring, font); 

Where zapfstring is a string containing any Zapfdingbats character you want.

To add this Phrase at an absolute position, you need to read chapter 3. Take a look at the examples for inspiration, for instance FoobarFilmfestival.cs:

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

Where 200 and 500 are an X and Y coordinate and 0 is an angle expressed in degrees. Instead of ALIGN_CENTER, you can also choose ALIGN_RIGHT or ALIGN_LEFT.

In your code sample you were adding a Zapfdingbats glyph to a document using a list. Please take a moment to put yourself in my place. Doesn't that feel as if you're the inventor of the sock attending a Red Hot Chili Peppers' concert?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...