private void DilekceDoldur()
{
try
{
Cursor.Current = Cursors.WaitCursor;
Object oMissing = System.Reflection.Missing.Value;
Object oTrue = true;
Object oFalse = false;
Word.Application oWord = new Word.Application();
oWord.Visible = true;
// Dosya yolunu dogru sekilde ayarlayin
string templatePath = System.Windows.Forms.Application.StartupPath + "\\Belgeler\\Velizindilekcesi.docx";
string connectionString = @"Data Source=Database\gezievrak2541.sqlite";
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
{
conn.Open();
// Ögrenciler için sorgu
string query1 = @"
SELECT sinifi, adi || ' ' || soyadi AS adsoyad, ono
FROM gezilistemiz25
WHERE ono IS NOT NULL";
using (var cmd1 = new SQLiteCommand(query1, conn))
{
using (SQLiteDataReader dr1 = cmd1.ExecuteReader())
{
// Sablon dosyasinin kopyasini olustur
string tempDocPath = Path.Combine(Path.GetTempPath(), "TempVeliDilekcesi.docx");
File.Copy(templatePath, tempDocPath, true);
Word.Document tempDoc = oWord.Documents.Open(tempDocPath, ReadOnly: false);
int studentIndex = 1; // Ögrenci indeksini baslat
while (dr1.Read())
{
string sinifi = dr1["sinifi"].ToString();
string adsoyad = dr1["adsoyad"].ToString();
string ono = dr1["ono"].ToString();
// Basligi ekleyin
Word.Paragraph titlePara = tempDoc.Content.Paragraphs.Add(ref oMissing);
titlePara.Range.Text = "VELI IZIN BELGESI";
titlePara.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
titlePara.Range.Font.Bold = 1; // Baslik kalin olacak
titlePara.Range.Font.Size = 14; // Baslik yazi tipi boyutu
titlePara.Range.InsertParagraphAfter(); // Basliktan sonra bir paragraf ekleyin
// Gezibilgileri25 tablosundan veri çekme
string query2 = @"
SELECT gtarihi, dtarihi, gyeri
FROM gezibilgileri25
WHERE EXISTS (
SELECT 1
FROM gezilistemiz25
WHERE gezilistemiz25.sinifi = @sinifi
)";
using (var cmd2 = new SQLiteCommand(query2, conn))
{
cmd2.Parameters.AddWithValue("@sinifi", sinifi);
using (SQLiteDataReader dr2 = cmd2.ExecuteReader())
{
while (dr2.Read())
{
string gtarihi = Convert.ToDateTime(dr2["gtarihi"]).ToShortDateString();
string dtarihi = Convert.ToDateTime(dr2["dtarihi"]).ToShortDateString();
string gyeri = dr2["gyeri"].ToString();
// Içerigi ekleyin
Word.Paragraph newPara = tempDoc.Content.Paragraphs.Add(ref oMissing);
newPara.Range.Font.Bold = 0; // Kalin degil
newPara.Range.Font.Size = 12; // Varsayilan yazi tipi boyutu
newPara.Range.Text = $"Velisi bulundugum {sinifi} sinifi {ono} nolu {adsoyad} isimli ögrencinin {gtarihi} - {dtarihi} tarihleri arasinda planlanan {gyeri} çalismasina/gezisine/toplum hizmetine katilmasinda herhangi bir sakinca görmedigimi bildiririm.\n";
newPara.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft; // Sola yasli
newPara.Range.InsertAfter("\n"); // Basliktan sonra bir satir bosluk ekleyin
// Imza kismini ekleyin
Word.Paragraph Newimza = tempDoc.Content.Paragraphs.Add(ref oMissing);
Newimza.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; // Saga yasli
Newimza.Range.Font.Bold = 0; // Kalin degil
Newimza.Range.Font.Size = 12; // Yazi tipi boyutu
Newimza.Range.Text = $"{DateTime.Now.ToShortDateString()}\n" +
"(Imza)\n" +
"Veli\n" +
"Adi Soyadi\n";
Newimza.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; // Saga yasli
// Her ögrencinin verisinden sonra bir bos paragraf ekleyin
Word.Paragraph spacerPara = tempDoc.Content.Paragraphs.Add(ref oMissing);
Newimza.Range.InsertAfter("\n"); // Basliktan sonra bir satir bosluk ekleyin
//spacerPara.Range.InsertParagraphAfter();
}
}
}
studentIndex++;
}
// Belgeyi kaydet
string savePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "VeliDilekcesi.docx");
tempDoc.SaveAs2(savePath);
// Geçici belgeyi kapat
tempDoc.Close(ref oFalse, ref oMissing, ref oMissing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(tempDoc);
conn.Close();
}
}
oWord.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
oWord = null;
GC.WaitForPendingFinalizers();
GC.Collect();
Cursor.Current = Cursors.Default;
MessageBox.Show("Sözlesme Belgeleri Word formatinda masaüstüne kaydedildi!");
this.Close();
}
}
catch (Exception hata)
{
MessageBox.Show("Islem Sirasinda Hata Olustu: " + hata.Message);
}
}
I'm using C# to transfer text to Word through a desktop form. However, I'm experiencing issues with the text formatting. The title should be centered, the paragraph should be left-aligned, and the date, signature, and name sections should be right-aligned. Despite adjusting the settings, I can't get the format to appear as I want. Can you help me?

Prasad RaveendranPosted Aug 4, 2024, 4:38 PM
Here is the updated code. Hope this would work
Key Changes:Mehmet FatihPosted Aug 4, 2024, 8:51 AM
The result is the same as mine.
Prasad RaveendranPosted Aug 4, 2024, 2:56 AM
These changes should help resolve the alignment issues in the generated Word document. If further adjustments are needed, consider inspecting the resulting document to see if specific formatting tweaks are required.