问题
I am using the following code:
PdfReader PDFReader = new PdfReader("c:\\file.pdf");
FileStream Stream = new FileStream("c:\\new.pdf", FileMode.Create, FileAccess.Write);
PdfStamper PDFStamper = new PdfStamper(PDFReader, Stream);
for (int iCount = 0; iCount < PDFStamper.Reader.NumberOfPages; iCount++)
{
iTextSharp.text.Rectangle PageSize = PDFReader.GetCropBox(iCount + 1);
PdfContentByte PDFData = PDFStamper.GetOverContent(iCount + 1);
BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
PDFData.BeginText();
PDFData.SetColorFill(CMYKColor.RED);
PDFData.SetFontAndSize(baseFont, 20);
PDFData.ShowTextAligned(PdfContentByte.ALIGN, SAMPLE DOCUMENT", (PageSize.Right + PageSize.Left) / 2, (PageSize.Top + PageSize.Bottom) / 2, 45);
PDFData.EndText();
}
PDFStamper.Close();
PDFReader.Close();
But sometimes the overlaid text exceeds the page size because I have hard coded the font as 20. So, is there any way to know if the overlaid text will exceed the page size? Because I want to use code like I will then use:
if(pagesize exceeds)
reduce font size by 1 point and check again .....
If the above doesn't work, then my next step is to use a PNG image that has the overlaid text in it and its background as transparent. then resize the image according to the pagesize and then overlay it.
however, I will prefer the first part. if not, then I will go for the second option.
回答1:
After doing some minor calculations, this method should calculate the maximum font size to use for such a vertical text and apply it:
void Stamp(PdfContentByte cb, Rectangle rect, BaseFont bf, string text)
{
int altitude = Math.Max(bf.GetAscent(text), bf.GetDescent(text));
int width = bf.GetWidth(text);
double horizontalFit = Math.Sqrt(2.0) * 1000 * (rect.Left + rect.Right) / (width + 2 * altitude);
double verticalFit = Math.Sqrt(2.0) * 1000 * (rect.Bottom + rect.Top) / (width + 2 * altitude);
double fit = Math.Min(horizontalFit, verticalFit);
cb.BeginText();
cb.SetColorFill(CMYKColor.RED);
cb.SetFontAndSize(bf, (float) fit);
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, text, (rect.Right + rect.Left) / 2, (rect.Top + rect.Bottom) / 2, 45);
cb.EndText();
}
You can call it like this:
using (PdfReader reader = new PdfReader(source))
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create, FileAccess.Write)))
{
for (int iCount = 0; iCount < reader.NumberOfPages; iCount++)
{
Rectangle PageSize = reader.GetCropBox(iCount + 1);
PdfContentByte PDFData = stamper.GetOverContent(iCount + 1);
BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
Stamp(PDFData, PageSize, baseFont, "SAMPLE DOCUMENT");
}
}
(By the way, I used your BaseFont
but you should be aware that iText(Sharp) will ignore BaseFont.EMBEDDED
for standard 14 fonts like BaseFont.HELVETICA
.)
The result looks like this:
PS: If you (as expressed in your question) really don't want to use a font size above 20, you have to Min
the fit
value again with 20.
来源:https://stackoverflow.com/questions/34470511/how-to-know-if-text-exceeds-the-fill-box-in-itextsharp