【问题标题】:Update Widgets in PDF using Podofo使用 Podofo 更新 PDF 中的小部件
【发布时间】:2013-05-26 10:26:39
【问题描述】:

我正在使用 PdfAnnotation.SetContents 来设置注释的值。如果注释的类型是 FreeText,那么只有这种方法才能正确工作,并且值会显示在 PDF 上(使用 PDF 阅读器)。如果类型是小部件,值被设置为 pdf 字典中的内容,但不显示。有没有办法设置小部件的值?

【问题讨论】:

    标签: ios pdf pdf-generation podofo


    【解决方案1】:

    我找到了解决方案,为了显示内容,必须设置外观(“AP”)字典。

    这可以用于:

    void PdfField::CreateFieldAppearance(PdfMemDocument *memDoc, const PdfString &value)
    {
      if( !m_pWidget->HasAppearanceStream() )
      {
        PdfRect pageRect;
        PdfPainter painter;
    
        PoDoFo::PdfRect rect = this->GetWidgetAnnotation()->GetRect();
        unsigned int width = rect.GetWidth();
        unsigned int height = rect.GetHeight();
    
        PdfRect pdfRect(0, 0, width, height);    
    
        PdfXObject xObj(pdfRect, memDoc);
        painter.SetPage(&xObj);
        painter.SetClipRect(pdfRect);
    
        painter.Save();
        painter.SetColor(221.0/255.0, 228.0/255.0, 1.0);
    
        painter.FillRect(0, 0, width, height);
        painter.Restore();
    
        // make rotation
    
        painter.Save();
    
        /***********************************************************************************/
    
        // Rotation Logic
    
        double angle = this->GetPage()->GetRotation();
    
        if (angle) {
            double radAngle = angle * M_PI / 180;
    
            int cosA =  (int)cos(radAngle);
            int sinA = (int)sin(radAngle);
    
            double translateY = rect.GetWidth(); // The View goes out of the bound, sits on top
            painter.SetTransformationMatrix(cosA, sinA, -sinA, cosA, translateY, 0);
        }
        /***********************************************************************************/
    
    
        PdfFont *font = memDoc->CreateFont("Helvetica", true, false);
        font->SetFontSize(15);     
    
        // Do the drawing
        painter.SetFont(font);
        painter.BeginText(10, 5);
        painter.SetStrokeWidth(20);
        painter.AddText(value);
        painter.EndText();
    
        painter.FinishPage();
    
        // This is very important. Not only does it disable the editing.
        // Also it does correct the appearance issue on Adobe Readers.
        this->SetReadOnly(true);
    
        // The Stream Object has to be saved to the annotation
        PoDoFo::PdfDictionary dict;
        dict.AddKey( "N", xObj.GetObject()->Reference() );
        this->GetFieldObject()->GetDictionary().AddKey( "AP", dict );
    }
    
    }
    

    【讨论】:

    • 以上代码运行良好。你能告诉我如何在外观上添加图像吗?@iphonic 和@qamar
    猜你喜欢
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多