skorpion wrote
> Yes i found it here, but don't store the data. I write here what I've
> done:

The code you posted does not even compile:

> dpi.addPieceInfo(os, reader, appName, dataName, new PdfString("prova"));

That method does not accept an OutputStream.

The major conceptional error is, though, that you 

a) use the same OutputStream for the PdfStamper as you used for the
PdfWriter without any re-initialization of that variable:

> writer = PdfWriter.getInstance(document, os);
> [...]
> PdfStamper stamper = new PdfStamper(reader, os);

b) don't close the PdfStamper before checking the result.

Here a simple junit test case for the DocumentPieceInfo helper class:

public class TestDocumentPieceInfo
{
    PdfName appName = new PdfName("MYAPP");
    PdfName dataName = new PdfName("Hash");
    String data = "Some test data.";

    @Test
    public void test() throws IOException, DocumentException
    {
        DocumentPieceInfo dpi = new DocumentPieceInfo();

        new File("target/test-outputs").mkdirs();

        InputStream resourceStream =
getClass().getResourceAsStream("test.pdf");
        try
        {
            PdfReader reader = new PdfReader(resourceStream);

            dpi.addPieceInfo(reader, appName, dataName, new
PdfString(data));

            PdfStamper stamper = new PdfStamper(reader, new
FileOutputStream(
                    "target/test-outputs/test-with-piece-info.pdf"));
            stamper.close();
        }
        finally
        {
            if (resourceStream != null)
                resourceStream.close();
        }

        PdfReader reader = new
PdfReader("target/test-outputs/test-with-piece-info.pdf");
        PdfObject myData = dpi.getPieceInfo(reader, appName, dataName);
        Assert.assertEquals(data, ((PdfString)myData).toString());
    }
}

As you see I load an existing PDF file. (I use a PDF from the project
resources, it could also have been any PDF from the file system.)

Then I add information using the class under test and then store the result
in the file system as "target/test-outputs/test-with-piece-info.pdf".

Then I load that newly written file and check for the data in question, and
hey, they are there!




--
View this message in context: 
http://itext-general.2136553.n4.nabble.com/Insert-custom-dictionary-in-pdf-file-tp4659026p4659031.html
Sent from the iText - General mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to