2015年9月19日土曜日

注釈の外観辞書を作成する

外観辞書とは「注釈の外観(見た目)を定義している辞書」とのことです。注釈の数だけ存在します。

具体的には /AP /N の辞書です。

仕様は、
Portable document format — Part 1 - Adobe
  12 Interactive Features
    12.5 Annotations
      12.5.5 Appearance Streams で詳しく触れられています。

PDFXplorer で確認すると、このようになっています:



サンプル PDF はこちら
 
この外観辞書で表現している外観です:


外観辞書がないとどうなる?
  • Adobe Reader などでは裏で作って表示するようです。印刷しても、きちんと印刷されます。
  • PDF 印刷専用ソフトなどでは、印刷されない場合有り ← これが問題

そこで、外観辞書を自作する必要がでてきます。

画像一枚をベタで描画する注釈のサンプルです:
http://itext.2136553.n4.nabble.com/Stamp-annotations-td2150068.html

/// <summary>
/// CreateStampAnnotation
/// </summary>
/// <remarks>
/// How to compute width and height in pt.:
/// 
/// ```cs
/// var w = image.Width * 72f / image.DpiX;
/// var h = image.Height * 72f / image.DpiY;
/// ```
/// 
/// How to use:
/// 
/// ```cs
/// pdfStamper.AddAnnotation(CreateStampAnnotation(...), 1);
/// ```
/// </remarks>
/// <param name="writer">pdfStamper.Writer or such.</param>
/// <param name="annotContents">The body part of annotation tooltip.</param>
/// <param name="stampHintName">Such as "Approved" suitable for file name.</param>
/// <param name="stampAuthor">The bolded headline part of annotation tooltip suitable for author or title.</param>
/// <param name="stampTitle">The invisible title can be confirmed at annotation property.</param>
public static PdfAnnotation CreateStampAnnotation(
    PdfWriter writer,
    string annotContents,
    string stampHintName,
    string stampAuthor,
    string stampTitle,
    iTextSharp.text.Image image,
    iTextSharp.text.Rectangle rect,
    int annotFlags = PdfAnnotation.FLAGS_PRINT
)
{
    var annot = PdfAnnotation.CreateStamp(writer, rect, annotContents, stampHintName);
    {
        {
            // "/N" is a normal appearance should be stored in appearance dictionary.
            var N = PdfAppearance.CreateAppearance(writer, image.Width, image.Height);
            image.SetAbsolutePosition(0, 0);
            N.AddImage(image);
            new PdfStream(N.ToPdf(N.PdfWriter));
            annot.SetAppearance(PdfName.N, N);
        }
        if (stampAuthor != null)
        {
            annot.Title = stampAuthor;
        }
        annot.Name = Guid.NewGuid().ToString();
        annot.Flags = annotFlags;
        if (stampTitle != null)
        {
            annot.Put(new PdfName("Subj"), new PdfString(stampTitle, PdfString.TEXT_UNICODE));
        }
    }
    return annot;
}


CreateAppearance と SetAppearance という物を初めて知りました。

0 件のコメント:

コメントを投稿