PDF is a popular means of sharing
documents, especially for those formatted for print. Not coincidentally, PDF
files -when prepared properly - also provide an excellent archival file format.
Many books and other reading material are stored in the form of PDF. So there
is a need that we can somehow create a PDF document from scratch. And in
this document, I will introduce you how to do so. All you need is a free API
from here (https://freepdf.codeplex.com/). Text, picture and barcode can be
added into the PDF document using this API.
Previewscreenshot of the result PDF file.
Here is the code to create the above PDF file. Just use the
API and test the following code, PDF file will be successfully created. And I
hope this article can do you some help.
//create a pdf
document.
PdfDocument doc = newPdfDocument();
// create one page
PdfPageBase page =
doc.Pages.Add();
floatpageWidth =
page.Canvas.ClientSize.Width;
float y = 0;
//page header
PdfPen pen1 = newPdfPen(Color.LightGray, 1f);
PdfBrush brush1 = newPdfSolidBrush(Color.LightGray);
PdfTrueTypeFont font1 = newPdfTrueTypeFont(newFont("Arial", 8f, FontStyle.Italic));
PdfStringFormat format1 = newPdfStringFormat(PdfTextAlignment.Right);
String text = "Demo of Barcode";
page.Canvas.DrawString(text, font1,
brush1, pageWidth, y, format1);
SizeF size =
font1.MeasureString(text, format1);
y = y + size.Height + 1;
page.Canvas.DrawLine(pen1, 0, y, pageWidth, y);
//title
y = y + 5;
PdfBrush brush2 = newPdfSolidBrush(Color.Black);
PdfTrueTypeFont font2 = newPdfTrueTypeFont(newFont("Arial", 16f, FontStyle.Bold));
PdfStringFormat format2 = newPdfStringFormat(PdfTextAlignment.Center);
format2.CharacterSpacing = 1f;
text = "Summary
of Science";
page.Canvas.DrawString(text, font2,
brush2, pageWidth / 2, y, format2);
size = font2.MeasureString(text,
format2);
y = y + size.Height + 6;
//add barcode
PdfCode128BBarcode barcode = newPdfCode128BBarcode("Hello 00-123");
barcode.BarHeight = 20;
barcode.BarcodeToTextGapHeight =
1f;
barcode.TextDisplayLocation = TextLocation.Bottom;
barcode.TextColor = Color.Blue;
barcode.Draw(page, newPointF(0, y));
y = y + 30;
//add picture
PdfImage image = PdfImage.FromFile("Science.png");
page.Canvas.DrawImage(image, newPointF(pageWidth
- image.PhysicalDimension.Width, y));
floatimageLeftSpace
= pageWidth - image.PhysicalDimension.Width - 2;
floatimageBottom
= image.PhysicalDimension.Height + y;
//add text content
PdfStringFormat format4 = newPdfStringFormat();
text = System.IO.File.ReadAllText("ScienceText.txt");
PdfTrueTypeFont font5 = newPdfTrueTypeFont(newFont("Arial", 10f));
format4.LineSpacing = font5.Size *
1.5f;
PdfStringLayoutertextLayouter
= newPdfStringLayouter();
floatimageLeftBlockHeight
= imageBottom - y;
PdfStringLayoutResult result
= textLayouter.Layout(text, font5, format4, newSizeF(imageLeftSpace, imageLeftBlockHeight));
if
(result.ActualSize.Height<imageBottom - y)
{
imageLeftBlockHeight =
imageLeftBlockHeight + result.LineHeight;
result = textLayouter.Layout(text,
font5, format4, newSizeF(imageLeftSpace,
imageLeftBlockHeight));
}
foreach (LineInfo line inresult.Lines)
{
page.Canvas.DrawString(line.Text,
font5, brush2, 0, y, format4);
y = y + result.LineHeight;
}
PdfTextWidgettextWidget =
newPdfTextWidget(result.Remainder,
font5, brush2);
PdfTextLayouttextLayout =
newPdfTextLayout();
textLayout.Break = PdfLayoutBreakType.FitPage;
textLayout.Layout = PdfLayoutType.Paginate;
RectangleF bounds = newRectangleF(newPointF(0, y),
page.Canvas.ClientSize);
textWidget.StringFormat = format4;
textWidget.Draw(page, bounds,
textLayout);
//save pdf file.
doc.SaveToFile("result.pdf");
doc.Close();
//view the result file
System.Diagnostics.Process.Start("result.pdf");
No comments:
Post a Comment