如何提高iTextSharp的测量精度?

时间:2014-07-21 09:23:42

标签: c# itextsharp itext

我想将pdf的大小调整为特定大小,但是当我使用缩放时,它会失去准确性,因为float会使值四舍五入。有没有办法可以调整给定宽度和高度的PDF格式?这是我到目前为止所尝试的:

public void PDFScalingTest11(string FileIn, string FileOut)            
    {
        // The following code opens a pdf and place it 20 times on a new pdf page
        // I want to resize the pdf before adding it

          int iQuantity = 20;
          int iCol = 3;
          int iRow = 0; 
          float fTileSpacing = 0;

          float fPrintWidth = 1200f; // Page Width 
          float fPrintHeight = 4158f; // PageHeight

          float fWidth = 400f; // output size
          float fHeight =  594f;

          float fPdfWidth = 210f;// current pdf size
          float fPdfHeight = 297f;

          float fScalingWidth = fWidth / fPdfWidth; // scaling (this value should be (1.904761904761905) but is rounded to (1.90476191)
          float fScalingHeight = fHeight / fPdfHeight; // this value is correct  

          fPrintWidth = iTextSharp.text.Utilities.MillimetersToPoints(fPrintWidth); // change mm to points         
          fPrintHeight = iTextSharp.text.Utilities.MillimetersToPoints(fPrintHeight);

          fWidth = iTextSharp.text.Utilities.MillimetersToPoints(fWidth);
          fHeight = iTextSharp.text.Utilities.MillimetersToPoints(fHeight);
          fTileSpacing = iTextSharp.text.Utilities.MillimetersToPoints(fTileSpacing);

        float x = 0;
        float y = (((fHeight + fTileSpacing) * iRow) - (fTileSpacing + fHeight));

        using (var doc = new Document(new Rectangle(fPrintWidth, fPrintHeight)))
        {
            using (var fs = new FileStream(FileOut, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (var writer = PdfWriter.GetInstance(doc, fs))
                {
                    doc.Open();
                    doc.NewPage();
                    iDraw = 0;
                    PdfReader reader = new PdfReader(FileIn);
                    PdfContentByte canvas = writer.DirectContent;
                    PdfTemplate tmp = writer.GetImportedPage(reader, 1);

                    for (int i = 0; i < iQuantity; i++)  
                        {
                            canvas.AddTemplate(tmp, fScalingWidth, 0, 0, fScalingHeight, x, y);
                            x += fTileSpacing + fWidth;
                            iDraw++;

                            if (iDraw == iCol)
                            {
                                y -= fHeight + fTileSpacing;
                                x = 0;
                                iDraw = 0;
                            }
                        }
                    doc.Close();
                    }                        
            }
        }
            System.Diagnostics.Process.Start(FileOut);
    }

    // The width of each pdf added to the new pdf page is 399mm instead of 400

1 个答案:

答案 0 :(得分:1)

ByteBuffer类有一个名为HIGH_PRECISION的公共静态变量。默认情况下,它设置为false。您可以将其设置为true,以便在舍入数字时获得6位小数:

iTextSharp.text.pdf.ByteBuffer.HIGH_PRECISION = true;

这将花费你一些性能(但也许你几乎没有注意到),结果文件将有更多的字节(但测量会更准确)。