내 응용 프로그램에서 콘텐츠의 PDF를 생성하는 기능을 추가하고 싶습니다 (단순화를 위해 텍스트 전용).PDFsharp가 자동으로 여러 페이지로 문자열을 분할 할 수 있습니까?
한 페이지에 들어가는 내용의 양을 자동으로 계산하거나 두 번째 (세 번째, 네 번째 등) 페이지를 만들기 위해 한 페이지를 넘치는 내용을 얻는 방법이 있습니까?
텍스트 블록을 쉽게 처리 할 수 있습니다. 텍스트를 여러 문자로 나누어 문자열 배열로 만든 다음 차례대로 각 페이지를 인쇄합니다.하지만 텍스트에 공백이 많고 문자가 많이 나오면, 이 작동하지 않습니다.
어떤 조언이 필요합니까?
현재 코드 :
는public void Generate(string title, string content, string filename)
{
PdfDocument document = new PdfDocument();
PdfPage page;
document.Info.Title = title;
XFont font = new XFont("Verdana", 10, XFontStyle.Regular);
List<String> splitText = new List<string>();
string textCopy = content;
int ptr = 0;
int maxCharacters = 3000;
while (textCopy.Length > 0)
{
//find a space in the text near the max character limit
int textLength = 0;
if (textCopy.Length > maxCharacters)
{
textLength = maxCharacters;
int spacePtr = textCopy.IndexOf(' ', textLength);
string startString = textCopy.Substring(ptr, spacePtr);
splitText.Add(startString);
int length = textCopy.Length - startString.Length;
textCopy = textCopy.Substring(spacePtr, length);
}
else
{
splitText.Add(textCopy);
textCopy = String.Empty;
}
}
foreach (string str in splitText)
{
page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
XTextFormatter tf = new XTextFormatter(gfx);
XRect rect = new XRect(40, 100, 500, 600);
gfx.DrawRectangle(XBrushes.Transparent, rect);
tf.DrawString(str, font, XBrushes.Black, rect, XStringFormats.TopLeft);
}
document.Save(filename);
}