Itextsharp"令牌类型无法识别"

时间:2013-04-03 07:35:00

标签: c# pdf-generation itextsharp tiff

我正在从tiff文件生成pdf。使用Acrobat 7时,我在打开时遇到错误,无法识别令牌类型。 我只看到全文中的一些词语,而不是这个pdf中的图片!

使用acrobat reader X,我正确地看到了pdf。当我向下滚动时,我收到一条消息,指出此文件中有错误,但没有更多信息。

任何人都知道问题在哪里?我在Itext中尝试了许多PDF版本而没有其他结果!

以下是一些代码:

        Clock.hocr.Elements.hDocument hdoc = new Clock.hocr.Elements.hDocument();
        hdoc.AddFile("out3.html");
        Clock.hocr.PDFSettings pdfset = new Clock.hocr.PDFSettings();
        pdfset.ImageType = Clock.hocr.PdfImageType.JPeg;
        pdfset.ImageQuality = 50;
        //pdfset.Subject = "Fulltext";
        //pdfset.Title = "ft";
        //pdfset.Language = "german";
        //pdfset.Author = "datapool";
        pdfset.Dpi=300;
        //pdfset.Keywords="...";
        //pdfset.PdfOcrMode =Clock.hocr.OcrMode.Tesseract;
        pdfset.WriteTextMode = Clock.hocr.WriteTextMode.Word;

        //pdfset.Dpi = 300;

        Clock.hocr.PdfCreator pdfcreat = new Clock.hocr.PdfCreator(pdfset,"test.pdf");
        List<Image> pages = GetAllPages("test.tif");



        for (int i = 0; i<hdoc.Pages.Count;i++)
        {
            Clock.hocr.Elements.hPage hpage = hdoc.Pages[i];
            pdfcreat.AddPage(hpage, pages[i]);


        }


        pdfcreat.SaveAndClose();

您可以在http://hocrtopdf.codeplex.com/

上找到Clock.hocr模块
public void AddPage(hPage page, System.Drawing.Image pageImage)
    {
        doc.NewPage();
        doc.AddHeader("COPYRIGHT", "DATAPOOL GmbH");
        AddImage(pageImage);
        WriteUnderlayContent(page);

    }

doc来自itextsharp.text.document类型

public void AddImage(System.Drawing.Image image)
    {
        try
        {
            if (OnProcessImage != null)
            {
                AddImage(OnProcessImage(image));
                return;
            }

            iTextSharp.text.Image i = GetImageForPDF(ImageProcessor.GetAsBitmap(image));

            i.SetAbsolutePosition(0, 0);
            // doc.SetPageSize(new iTextSharp.text.Rectangle(i.Width, i.Height));
            i.ScaleAbsolute(doc.PageSize.Width, doc.PageSize.Height);
            doc.Add(i);

        }
        catch (Exception x)
        {
            Debug.WriteLine(x.Message);
            throw x;
        }
    }

...

    private void WriteUnderlayContent(hPage page)
    {
        string pageText = page.Text;
        foreach (hParagraph para in page.Paragraphs)
        {
            foreach (hLine line in para.Lines)
            {
                if (pdfSettings.WriteTextMode == WriteTextMode.Word)
                {
                    line.AlignTops();
                    foreach (hWord c in line.Words)
                    {
                        c.CleanText();
                        BBox b = BBox.ConvertBBoxToPoints(c.BBox, pdfSettings.Dpi);
                        BBox lineBox = BBox.ConvertBBoxToPoints(line.BBox, pdfSettings.Dpi);
                        PdfContentByte cb = cb = writer.DirectContentUnder;

                        BaseFont base_font = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, false);
                        iTextSharp.text.Font font = new iTextSharp.text.Font(base_font);

                        float h = (int)Math.Ceiling(b.Height);
                        int font_size = (int)Math.Ceiling(h);
                        if (font_size == 0)
                            font_size = 2;

                        cb.BeginText();
                        cb.SetFontAndSize(base_font, (float)font_size);
                        cb.SetTextMatrix(b.Left, doc.PageSize.Height - b.Top - b.Height);
                        //float spacing = (c.BBox.Width / (c.Text.ToCharArray().Count())) / 100;
                        float spacing = (c.BBox.Width / (c.Text.ToCharArray().Count())) / 100 ;
                        cb.SetCharacterSpacing(spacing);
                        cb.ShowText(c.Text + " ");
                        cb.EndText();
                    }
                }
                else
                {
                    line.CleanText();
                    BBox b = BBox.ConvertBBoxToPoints(line.BBox, pdfSettings.Dpi);
                    BBox lineBox = BBox.ConvertBBoxToPoints(line.BBox, pdfSettings.Dpi);
                    PdfContentByte cb = cb = writer.DirectContentUnder;

                    BaseFont base_font = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, false);
                    iTextSharp.text.Font font = new iTextSharp.text.Font(base_font);

                    float h = (int)Math.Ceiling(b.Height);
                    int font_size = (int)Math.Ceiling(h);
                    if (font_size == 0)
                        font_size = 2;

                    cb.BeginText();
                    cb.SetFontAndSize(base_font, (float)font_size);
                    cb.SetTextMatrix(b.Left, doc.PageSize.Height - b.Top - b.Height);
                    float spacing = (line.BBox.Width / (line.Text.ToCharArray().Count())) / 100;
                    cb.SetCharacterSpacing(spacing);
                    cb.ShowText(line.Text);
                    cb.EndText();
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

解决了问题!错误是错误的CharacterSpacing ... 将其固定为-0.0025并且pdf正在运行!