Hi -
On Sun, Sep 04, 2011 at 11:53:58AM -0700, Larry Doolittle wrote:
> Now I looked more carefully at FormTest, that shows how to do a sizable
> fraction of [filling out PDF forms]. I'm whacking away on that code now,
> trying to get FillTextField() to also add the XObject containing the
> rendering of the value.
I have nominally succeeded. Here's the patch relative to FormTest.cc
as included in PoDoFo-0.9.1. I'd appreciate it if someone checks it
over for sanity.
I cheat a little on the font handling. I assume that the document already
has a font named TT0 that I want to use in the XObject, and this program
just has to find it.
A minor question is if I can walk my way back from a PdfTextField to
its ancestor PdfDocument. For the moment in FormTest, I am able to
push doc as an extra argument into the FillTextField() argument list.
But that seems somehow unclean.
This work is not easy for me. I understand PDF files pretty well, and
C++ in theory at least. But the mapping between reality and the PoDoFo
class hierarchy is still mostly opaque to me.
- Larry
--- FormTest.cpp 2011-09-03 17:23:19.000000000 -0700
+++ FormTestDraw.cpp 2011-09-04 16:35:47.000000000 -0700
@@ -201,8 +201,15 @@
listBox.SetSelectedItem( 2 );
}
-void FillTextField( PdfTextField & rField )
+void FillTextField( PdfTextField & rField, PdfDocument *doc, PdfDictionary *fdict )
{
+ PdfAnnotation* ann = rField.GetWidgetAnnotation();
+ PdfRect r0 = ann->GetRect();
+ std::cout << " Rectangle: origin (" << r0.GetLeft() << "," << r0.GetBottom() <<
+ ") size (" << r0.GetWidth() << "," << r0.GetHeight() << ")" << std::endl;
+ PdfRect r(0.0, 0.0, r0.GetWidth(), r0.GetHeight());
+ std::cout << " HasAppearanceStream: " << (ann->HasAppearanceStream() ? "yes" : "no") << std::endl;
+
const char* pszCur = rField.GetText().GetString();
std::cout << " Current value:" << (pszCur ? pszCur : "") << std::endl;
@@ -213,6 +220,18 @@
if( value.length() )
{
rField.SetText( value );
+ PdfXObject xo(r,doc);
+ xo.GetResources()->GetDictionary().AddKey(PoDoFo::PdfName("Font"), *fdict);
+ std::ostringstream strs;
+ strs << "/Tx BMC q 0 0 " << r.GetWidth() << " " << r.GetHeight() << " re W n 0 g BT /Helv 10 Tf 2 7.51 Td (" << value << ")Tj ET Q EMC";
+ const char *ss=strdup(strs.str().c_str());
+#ifdef NO_COMPRESSION
+ TVecFilters tv;
+ xo.GetContentsForAppending()->GetStream()->Set ( ss, strlen(ss), tv );
+#else
+ xo.GetContentsForAppending()->GetStream()->Set ( ss );
+#endif
+ ann->SetAppearanceStream(&xo);
}
}
@@ -247,6 +266,10 @@
for( int i=0;i<nPageCount;i++ )
{
pPage = doc.GetPage( i );
+ PdfObject* pFont = pPage->GetFromResources( PdfName("Font"), "TT0" );
+ std::cout << "pFont for TT0 found? " << (pFont ? "yes" : "no") << std::endl;
+ PdfDictionary fdict;
+ fdict.AddKey(PdfName("Helv"),pFont->Reference());
nFieldCount = pPage->GetNumFields();
std::cout << "Page " << i + 1 << " contains " << nFieldCount << " fields." << std::endl;
@@ -274,7 +297,7 @@
{
std::cout << "TextField" << std::endl;
PdfTextField text( field );
- FillTextField( text );
+ FillTextField( text, &doc, &fdict );
break;
}
case ePdfField_ComboBox:
@@ -304,6 +327,7 @@
}
}
+ // doc.SetWriteMode(ePdfWriteMode_Clean);
doc.Write( pszOutput );
}
------------------------------------------------------------------------------
Special Offer -- Download ArcSight Logger for FREE!
Finally, a world-class log management solution at an even better
price-free! And you'll get a free "Love Thy Logs" t-shirt when you
download Logger. Secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsisghtdev2dev
_______________________________________________
Podofo-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/podofo-users