Hi Team,
Below is the code snippet wrote to add a simple text to PDF file using Aspose.Pdf contained classes. Though the file is created in the location, there is no content(i.e) assigned text in the output pdf file.
Pdf m_pdf = new Pdf();
Section section = m_pdf.Sections.Add();
Aspose.Pdf.Table pdfTable = new Aspose.Pdf.Table(section);
Aspose.Pdf.Row pdfRow = pdfTable.Rows.Add();
pdfRow.Cells.Add();
Aspose.Pdf.Cell pdfCell = new Aspose.Pdf.Cell(pdfTable);
Text pdfText = new Text();
TextInfo textInfo = pdfText.TextInfo;
Segment segment = pdfText.Segments.Add();
string tempString = "some text";
segment.Content = tempString;
textInfo = segment.TextInfo;
if (pdfText != null) pdfCell.Paragraphs.Add(pdfText);
section.Paragraphs.Add(pdfTable);
MemoryStream stream = new MemoryStream();
m_pdf.Save(@"c:\temp\test.pdf");
Please correct if i am missing any thing here.
Thanks,
Divya
Hi Divya,
Thanks for contacting support.
The reason you are getting a blank PDF file is because you have mixed two namespaces i.e. Aspose.Pdf and Aspose.Pdf.Generator. The m_pdf
object is created from Aspose.Pdf.Generator.Pdf class, whereas the table object is created using Aspose.Pdf.Table class. The Text class is present under Aspose.Pdf.Generator namespace and you have tried adding it to cells objects created using Aspose.Pdf.Cell class. In order to generate the correct output, try using either namespace i.e. Aspose.Pdf or Aspose.Pdf.Generator. Please take a look at the following code snippet which generates the correct output.
See attached sample PDF file.
C#
Aspose.Pdf.Document m_pdf = new Aspose.Pdf.Document();
// Pdf m_pdf = new Pdf();
// Aspose.Pdf.Generator.Section section = m_pdf.Sections.Add();
m_pdf.Pages.Add();
Aspose.Pdf.Table pdfTable = new Aspose.Pdf.Table();
pdfTable.ColumnWidths = "100";
pdfTable.Border = new
Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Blue);
Aspose.Pdf.Row pdfRow = pdfTable.Rows.Add();
Aspose.Pdf.Cell pdfCell = new Aspose.Pdf.Cell();
pdfRow.Cells.Add(pdfCell);
Aspose.Pdf.Text.TextFragment pdfText = new Aspose.Pdf.Text.TextFragment("some text");
// TextInfo textInfo = pdfText.TextInfo;
// Segment segment = pdfText.Segments.Add();
// string tempString = "some text";
// segment.Content = tempString;
// textInfo = segment.TextInfo;
pdfCell.Paragraphs.Add(pdfText);
// section.Paragraphs.Add(pdfTable);
m_pdf.Pages[1].Paragraphs.Add(pdfTable);
// MemoryStream stream = new MemoryStream();
m_pdf.Save(@"c:\pdftest\SampleFile.pdf");
Hi Nayyer,
The code you gave was useful though we are using Aspose PDF dll V4.0.0.0 and Aspose PDF Kit dll v3.4.0.0. The Classes Document and TextFragment are not available in those version. Could you please give me a sample snippet suporting the version which we are using?
Thanks,
Divya
Hi Divya,
The versions of API’s which you are using are quite old. However, you may consider using the following code snippet to generate the correct output.
Please note that the reasons table object is not appearing in the PDF document is because you have instantiated pdfCell
object and have added the pdfText
object to paragraphs collection of pdfCell
. But you have not added the cell to cells collection of row object of table. In order to generate the correct output, please try using the following code snippet.
It’s recommended to set the column width for the table object.
Pdf m_pdf = new Pdf();
Section section = m_pdf.Sections.Add();
Aspose.Pdf.Generator.Table pdfTable = new Aspose.Pdf.Generator.Table();
pdfTable.ColumnWidths = "60";
pdfTable.Border = new Aspose.Pdf.Generator.BorderInfo(
(int)Aspose.Pdf.Generator.BorderSide.All,
new Aspose.Pdf.Generator.Color("#Red"));
Aspose.Pdf.Generator.Row pdfRow = pdfTable.Rows.Add();
Aspose.Pdf.Generator.Cell pdfCell = new Aspose.Pdf.Generator.Cell(pdfRow);
pdfRow.Cells.Add(pdfCell);
Aspose.Pdf.Generator.Text pdfText = new Aspose.Pdf.Generator.Text();
TextInfo textInfo = pdfText.TextInfo;
Segment segment = pdfText.Segments.Add();
string tempString = "some text";
segment.Content = tempString;
textInfo = segment.TextInfo;
pdfCell.Paragraphs.Add(pdfText);
section.Paragraphs.Add(pdfTable);
m_pdf.Save(@"c:\pdftest\LegacyProduct_test.pdf");
Hi Nayyer,
Many thanks for the code snnipet shared. It was very helpul and worked as i expected.
Thanks,
Divya
Hi Divya,
We are glad to hear that your problem is resolved. Please continue using our API and in the event of any further query, please feel free to contact.