-1

我正在从 tiff 文件生成 pdf。使用 Acrobat 7 打开时出现无法识别令牌类型的错误。我只看到全文中的一些文字,但没有看到这个pdf中的图片!

使用 acrobat reader XI 可以正确查看 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();

您可以在以下位置找到 Clock.hocr 模块:http: //hocrtopdf.codeplex.com/

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();
                }
            }
        }
    }
4

1 回答 1

0

解决了问题!错误出现在错误的 CharacterSpacing... 将其修复为 -0.0025 并且 pdf 正在工作!

于 2013-04-04T09:30:43.187 回答