Save Plain text to PDF/TIFF

Hi,

Really I dont know under whcih category I have to post this query.

I want to convert the plain text to PDF/TIFF using Aspose. Please share if any sample code available.

Thanks,

Ponraj.M

Hi Ponraj,


Thanks for your inquiry. You can convert plain text file into Pdf/Tiff file format by using Aspose.Words. You can use following code snippet to achieve your requirements.


// Read whole txt file into string.<o:p></o:p>

string txt = File.ReadAllText(“in.txt”);<o:p></o:p>

// Insert string into document.<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder();<o:p></o:p>

builder.Write(txt);<o:p></o:p>

// Save output.<o:p></o:p>

builder.Document.Save(“out.pdf”);

builder.Document.Save(“out.tiff”);



Please read supported features of plain text (txt) import/export from here:

Hope this helps you. Please let us know if you have any more queries.

Hi Ponraj,


Thanks for your inquiry. In addition to what Tahir has stated, you can now load a text file directly into Aspose.Words’ DOM as follows:

Aspose.Words.LoadOptions
loadOptions =
new Aspose.Words.LoadOptions();

loadOptions.LoadFormat = LoadFormat.Text;

Document doc = new Document(@"C:\Temp\input.txt", loadOptions);


Secondly, you can easily render any Microsoft Word document (e.g. DOC, DOCX, RTF see LoadFormat enumeration) to PDF format. I would suggest you please read the following article for more details:

Moreover, you can also convert your Word documents to various image formats e.g. to TIFF by using the ways suggested in the following articles:

http://www.aspose.com/docs/display/wordsnet/How+to++Save+Document+as+a+Multipage+TIFF

http://www.aspose.com/docs/display/wordsnet/ImageSaveOptions+Class


Best regards,

Thanks for your sample code. It is working fine.

Now I could able to save file as Multipage TIFF/PDF. Is it possible to save the file as Searilized TIFF/PDF. For example if the palin text exceeds more than one page it should automatically save as seperate files.

Hi Ponraj,

Thanks for your inquiry.

For example, in order to save all pages to separate PNG image files, you can use the following code snippet:

Aspose.Words.LoadOptions loadOptions = new Aspose.Words.LoadOptions();

loadOptions.LoadFormat = LoadFormat.Text;

Document document = new Document(@"C:\test\In.txt", loadOptions);

ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Png);
options.PageCount = 1;

// Save each page of the document as Png.
for (int i = 0; i < document.PageCount; i++)
{
options.PageIndex = i;
document.Save(string.Format(@"C:\test\out_{0}.png", i), options);
}

I hope, this helps.

Best regards,

Thanks for the updates.

Now if I want to save only the Speaker Notes as PDF, I have use Apose.Slides to extract the notes then I have to use Aspose.words to save the text DocumentBuilder to convert to PDF. I lost all the formats which I applied for Notes. For example font style,name,size etc.

Kindly let me know is there any way to save the notes with the actual format which I applied for notes in PPT,even by using aspose.words is OK for me.

Also Is there any direct method to split the Slides and Notes to PDF/TIFF without using Aspose.words.

Hi Ponraj,

Thanks for your inquiry. You can achieve your requirements by using Aspose.Words and Aspose.Slides. Please use the following sample code snippet to extract notes from a slide and write note's contents into PDF with same font. Hope this helps you. Please let us know if you have any more queries.


Aspose.Slides.Presentation pres = new Aspose.Slides.Presentation(MyDir + "in.ppt");

//Access the first slide

Aspose.Slides.Slide sld = pres.Slides[0];

//Creating Slide note instance

Aspose.Slides.Notes note = sld.Notes;

Aspose.Slides.FontEntity noteFont = pres.Fonts[sld.Notes.Paragraphs[0].Portions[0].FontIndex];

// Insert string into document.

DocumentBuilder builder = new DocumentBuilder();

builder.Font.Name = noteFont.FontName;

builder.Font.Size = sld.Notes.Paragraphs[0].Portions[0].FontHeight;

builder.Font.Bold = sld.Notes.Paragraphs[0].Portions[0].FontBold;

builder.Font.Italic = sld.Notes.Paragraphs[0].Portions[0].FontItalic;

builder.Write(note.Text);

// Save output.

builder.Document.Save(MyDir + "out.pdf");

builder.Document.Save(MyDir + "out.tiff");


Hi ,

I tried with the above code. I'm facing an issue like I have 2 lines Bold and 2 lines Italic an d 2 lines underline..But its applying only Bold all the 4 lines of text...it is not applying Italic and Underline....its because of evalution vrsion..

I guess its not generic...if I apply bold it will apply to all the text in that notes...

pls let me know how to attach the sample file.

Also kindly let me know whther all the formats of MS slide notes are supported by Aspose?

Hi Ponraj,

Thanks for your inquiry. The code shared in my last post is not generic code. It works only with first note of first slide. The following code snippet works for a PPT file. The code example do the followings:

  • Iterate through all notes
  • Get the font information of a note
  • Write the contents with same font information into Pdf file by using Aspose.Words

Hope this helps you.


// Insert string into document.

DocumentBuilder builder = new DocumentBuilder();

Aspose.Slides.Presentation pres = new Aspose.Slides.Presentation(MyDir + "in.ppt");

//Access the first slide

Aspose.Slides.Slide sld = pres.Slides[0];

//Creating Slide note instance

Aspose.Slides.Notes note = sld.Notes;

//Iterate through all slides

foreach (Aspose.Slides.Slide slide in pres.Slides)

{

//Iterate through all note's Paragraphs

foreach (Aspose.Slides.Paragraph notePara in slide.Notes.Paragraphs)

{

//Iterate through all Portion

foreach (Aspose.Slides.Portion portion in notePara.Portions)

{

Aspose.Slides.FontEntity noteFont = pres.Fonts[portion.FontIndex];

builder.Font.Name = noteFont.FontName;

builder.Font.Size = portion.FontHeight;

builder.Font.Bold = portion.FontBold;

builder.Font.Italic = portion.FontItalic;

builder.Write(note.Text);

}

}

}

// Save output.

builder.Document.Save(MyDir + "out.pdf");


ponrajcm:

Also kindly let me know whther all the formats of MS slide notes are supported by Aspose?

I am moving this thread to Aspose.Total forum. My colleagues from Aspose.Slides team will reply you in more detail about Aspose.Slides queries.
ponrajcm:

pls let me know how to attach the sample file.

You can attach a file by clicking "Reply" button. Clicking on it brings you to an expanded form of an answer. There you can attach the file. Please see the attached image for detail.

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.



Hi Ponraj,


I have observed the following query from you:

Also kindly let me know whther all the formats of MS slide notes are supported by Aspose?

Can you please share that by this question what do you mean. In my suggestion, both PPT and PPTX presentations have slide notes. Please also use the following sample code for an idea about how to use extract PPTX slide notes.

public static void GetPptxFont()
{
Aspose.Slides.Pptx.PresentationEx pres = new Aspose.Slides.Pptx.PresentationEx(MyDir + “in.pptx”);
// Insert string into document.
DocumentBuilder builder = new DocumentBuilder();
Aspose.Slides.Pptx.NotesSlideEx note;

foreach (SlideEx sld in pres.Slides)
{
//Getting notes slides
note = sld.NotesSlide;

if (note != null)
{
builder.Write(“Slide Notes for Slide:” +sld.SlideNumber.ToString()+"\n\n");
foreach (ShapeEx shape in note.Shapes)
{
if (shape is AutoShapeEx)
{
AutoShapeEx ashp = (AutoShapeEx)shape;

if (ashp.TextFrame != null)
{
foreach (ParagraphEx para in ashp.TextFrame)
{
foreach (PortionEx portion in para.Portions)
{

FontDataEx noteFont = portion.PortionFormat.LatinFont;
builder.Font.Name = noteFont.FontName;
builder.Font.Size = portion.PortionFormat.FontHeight;
builder.Font.Bold = portion.PortionFormat.FontBold;
builder.Font.Italic = portion.PortionFormat.FontItalic;
}
}
}
}
}

}//End if
}//End for
// Save output.
builder.Document.Save(MyDir + “out.pdf”);
builder.Document.Save(MyDir + “out.tiff”);
}

Many Thanks,

Thanks for your response.

Let me explain clearly. I have attached the 2 sample file with 2 slides. One with normal format and another one with NotesPage format.

I want to save only the notes of 2 files and not the slide as a seperate pdf/tiff file . The format which I have applied should be reflected same in PDF/TIFF.

Kindly let me know if you have any questions.

Hi Ponraj,

Thanks for sharing the details. First of all, please note that Aspose.Words tries to mimic the same behavior as MS Word do. Please read Aspose.Words LoadFormat and SaveFormat form following documentation links.

Moreover, Microsoft Word and Microsoft PowerPoint documents are completely different file formats. So, it is hard to achieve exact requirements with 100% fidelity. However, you can extract note’s detail by using Aspose.Slides and write these notes into MS Word/Pdf document by using Aspose.Words. Pleas use the following code snippet read following documentation links for your kind reference.

Aspose.Slides.Presentation pres = new
Aspose.Slides.Presentation(MyDir + “Sample_NotesPage.ppt”);


// Insert string into document.

Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder();

Aspose.Slides.Notes note;

Aspose.Slides.FontEntity noteFont;


foreach (Aspose.Slides.Slide sld in pres.Slides)

{

//Getting notes slides

note = sld.Notes;


if (note != null)

{

builder.Write("Slide Notes for Slide:" + sld.SlidePosition.ToString() + "\n\n");


foreach (Aspose.Slides.Shape shape in note.Shapes)

{


if (shape.TextFrame != null)

{

foreach (Aspose.Slides.Paragraph para in shape.TextFrame.Paragraphs)

{


//Set list formats

if (para.HasBullet && builder.ListFormat.List == null)

{

if (para.NumberedBulletStyle == Aspose.Slides.NumberedBulletStyle.BulletRomanLCPeriod)

builder.ListFormat.List = builder.Document.Lists.Add(ListTemplate.NumberLowercaseRomanDot);

else if (para.NumberedBulletStyle == Aspose.Slides.NumberedBulletStyle.BulletArabicPeriod)

builder.ListFormat.List = builder.Document.Lists.Add(ListTemplate.BulletSquare);

}


foreach (Aspose.Slides.Portion portion in para.Portions)

{

noteFont = pres.Fonts[portion.FontIndex];

builder.Font.Name = noteFont.FontName;

builder.Font.Size = portion.FontHeight;

builder.Font.Bold = portion.FontBold;

builder.Font.Italic = portion.FontItalic;

builder.Font.Color = portion.FontColor;


if (portion.FontUnderline)

builder.Font.Underline = Aspose.Words.Underline.Single;

else

builder.Font.Underline = Underline.None;


builder.Write(portion.Text);

}

builder.Writeln();

}

}

}


}//End if

builder.ParagraphFormat.ClearFormatting();

}//End for

// Save output.

builder.Document.Save(MyDir + "outWord.docx");

builder.Document.Save(MyDir + "outWord.pdf");

Hope this helps you. Please let us know if you have any more queries.

Thanks for your response. Your code is working perfect. But some * symbol is coming at the end of the document.

Is ther any way to delete or avoid this junk character..

Hi Ponraj,


Thanks for your inquiry. Please find the modified code in attachment and let us know if you have any more queries.

Still I 'm getting the juck character at the end.(*). kindly let me know in which line you have modified in the attached file.

Hi

I have used the below code snippet to retrive the all bullets formats in MS powerpoint.

if (para.HasBullet )

{

if (para.NumberedBulletStyle == NumberedBulletStyle.BulletRomanLCPeriod)

builder.ListFormat.List = builder.Document.Lists.Add(ListTemplate.NumberLowercaseRomanDot);

else if (para.NumberedBulletStyle == NumberedBulletStyle.BulletArabicPeriod)

builder.ListFormat.List = builder.Document.Lists.Add(ListTemplate.BulletSquare);

}

During run time I could able to get BulletArabicPeriod as bullet type for all type of bullets attached in the file.

Please let me know how to get the equivalent MS PPT bullet in aspose.

Hi Ponraj,


Thanks for your inquiry. I am working over your query and will update you asap.

Ok Thanks. Meanwhile can you please let me know in which line of code you have modified for removing the junk character(*) in the attached note file.

Hi Ponraj,


Thanks for your inquiry. The character (*) is removed by following highlighted condition. Actually, this is related to metadata information in text frame. The metadata is slide number.


<span style=“font-size:
10.0pt;font-family:“Courier New”;color:blue;background:yellow;mso-highlight:
yellow;mso-no-proof:yes”>if<span style=“font-size:10.0pt;font-family:
“Courier New”;background:yellow;mso-highlight:yellow;mso-no-proof:yes”>
(portion.Field == null)<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”><o:p></o:p>

{

noteFont = pres.Fonts[portion.FontIndex];

builder.Font.Name = noteFont.FontName;

builder.Font.Size = portion.FontHeight;

builder.Font.Bold = portion.FontBold;

builder.Font.Italic = portion.FontItalic;

builder.Font.Color = portion.FontColor;

if (portion.FontUnderline)

builder.Font.Underline = Aspose.Words.Underline.Single;

else

builder.Font.Underline = Underline.None;

builder.Write(portion.Text);

}

Thanks! It is working fine.

Any updates about Bullets and Numbering format.